Try our conversational search powered by Generative AI!

Is it not correct to put helper properties on a model class?

Vote:
 

Is it incorrect to put helper properties on models?  (By "model," I mean a class with a "ContentType" that maps to a page or block type.)

For instance, should I be able to put a property in my model called "Children" which returns a List<PageData> of the children of that page?  This C# property would NOT map to an EPiServer property on the page or block type.

What we're finding is that when we save or publish a page, EPiServer is iterating all the properties on the object, including properties that do not map to EPiServer properties.  Clearly, this is a problem when we're returning PageData objects or collections of such -- we're dropping into infinite loops.  (Interestingly, this didn't seem to happen in 7.0/7.1.  It only became a problem when we upgraded the site to 7.6.)

So, is there any way to get EPiServer to leave these properties alone and not execute them on save or publish?

We could move the properties into a helper class, or we could create extension methods, but that's a lot of refactoring.

#84331
Apr 01, 2014 8:36
Vote:
 

Have you tried adding the [Ignore] attribute? This will tell the system to ignore the property.

#84335
Apr 01, 2014 9:10
Vote:
 

You need to instruct episerver not serialize your helper properties. As Per Magne mentioned, add [Ignore] will definitely work in your situtation. 

#84336
Apr 01, 2014 9:18
Vote:
 

So, we were doing this, and it wasn't working, which is why I was posting.

Now it is working.

We're not sure what happened -- we may have had an old version of the assembly cached or something.  I'm tempted to just delete this post, but I'm going to leave it in case someone else has the same problem and needs the solution.

#84337
Apr 01, 2014 9:20
Vote:
 

I have reported abug that it should be possible to decorate a property with an attribute (perhaps Ignore) so it does not is taken into consideration during validation.

#84338
Apr 01, 2014 9:26
Vote:
 

So, I don't know that "Ignore" works great.

I had a brand new installation of 7.5.  I added three properties to the base class (actually the SitePageData out of Alloy): Children, Ancestors, and Parent.  All three properties return either a single SitePageData (Parent) or a collection of SitePageData (the other two).

All three properties had the "Ignore" attribute.

I immediately started having problems. Publishing got slower, then it wouldn't publish at all.  You could press the publish, and it would think for a while, but then just come back and say it wasn't published. Then I started seeing errors about how I wasn't connected to the server.  A couple times, the entire w3p.exe process shut down.

So I took those three methods out of the base class, and moved them to extension methods instead.  Didn't change any code internal to the getters, just made them extension methods in a static class, rather than properties on the base class.

Instantly, every problem was fixed. The installation completely reverted to normal with no problems.

So, I suspect "Ignore" might get, uh, ignored in some cases. Safer to use extension methods, I think, which I don't like because I feel like properties on the class is more architectural correct. But, extension methods apparently have less problems.

#84708
Apr 04, 2014 16:47
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.