Business objects in inheritance tree don't serialize/deserialize correctly.

Business objects in inheritance tree don't serialize/deserialize correctly.

Old forum URL: forums.lhotka.net/forums/t/6768.aspx


jeremy.wiebe posted on Wednesday, April 08, 2009

I got bit by an interesting problem the other day which may not be entirely a CSLA bug. In our domain model we have a few instances where we have business objects that inherit from other business objects, which then inherit from BusinessBase(Of T). In the base class we have defined some properties using the newer-style managed properties. These properties are registered using a Shared initializer (ie. Private MyProp as PropertyInfo(Of String) = RegisterProperty(...))

What I found is that after one of the inherited objects crosses a process boundary (we're using CSLA with the WCF portal) the property info cache in the PropertyInfoManager is not re-created properly. What happens is that the Shared PropertyInfo()'s in the base class are not created prior to the OnDeserialized handler being called, which means that all of the PropertyInfo's in the base class are not indexed (they all remain with an INdex of -1).

My solution is that in these few instances where we have inheritance in our business objects we create a Shared Sub New in the inherited class which accesses one of the shared PropertyInfo()'s in the base class. This forces all of the Shared initializers in the base class to run and ends up happening prior to the re-indexing of properties.

So, finally, to my questions.
Are we doing something wrong?
Is anybody else using inheritance in their business objects?

One other solution I thought might work (but haven't tried) is to put all of the RegisterPRoperty calls into a Shared Sub New. This would mean alot of changes to our existing code so I haven't investigated it.

ajj3085 replied on Wednesday, April 08, 2009

What version of Csla are you using?  If you're using 3.6.2, I think this shouldn't be a problem.  If you're using anything prior, I think you'll need to search the forum for the _forceInit trick.

jeremy.wiebe replied on Wednesday, April 08, 2009

We're using 3.6.0. We'd been thinking of upgrading to the most recent release anyways, but if we don't at least I have an idea where to look.

Thanks.

JoeFallon1 replied on Wednesday, April 08, 2009

Andy is right.
Rocky finally addressed the issue in 3.6.2. So it really pays to upgrade - since you are on 3.6.0 the upgrade is extremely easy. I highly recommend you do so.

The flip side is implementing the forceInit trick *in every class* in your entire hierarchy!

Much easier to upgrade!!

Joe

jeremy.wiebe replied on Thursday, April 09, 2009

I've had to implement my fix I described above twice already and that's once to many! :-)

We'll probably upgrade shortly.

Thanks for the info.

swegele replied on Tuesday, May 12, 2009

Joe, Andy, et al:

I am glad I saw this post before following the advice in Expert C# 2008 (page 248) and adding _forceinit to all my classes!

I didn't notice on the 3.6.2 change logs that Rocky had fixed this or how.  So to confirm, you are saying I can now inherit from my own IRBBusinessBase<T> which itself inherits from BusinessBase<T> without any special stuff to do?

This is good news if it is the case because it made me nervous not having a place where I could put custom business logic that applied to all my classes.  My old company edited the CSLA objects directly.  UGH I was kicking myself for having done that because it made upgrading to newer CSLA releases too costly.

There are probably a bunch of people out there that bit the bullet and edited all of their classes to implement the _forceinit trick from the book.  Was there a blog or something that lets them know this is no longer the case?

Thanks a million guys for your work!

Sean

JoeFallon1 replied on Wednesday, May 13, 2009

As I recall there were a few threads about it. I was always opposed to it. Luckily I was a bit behind on the "upgrade path" so I got to complain enough times that Rocky finally decided to address it.

But I am sure there are some caveats to his fix. I can't recall them off the top of my head though. Maybe having to use Managed Properties was one of them so that Silverlight would work correctly. But that is just a guess.

My big objection to the forceInit trick was that I have a deep hierarchy and I would have to implement it on every level of the hierarchy in every class. That was just unacceptable.

Joe

Copyright (c) Marimer LLC