ObjectFactory - read/load data

ObjectFactory - read/load data

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


theGov posted on Monday, September 28, 2009

I’d like to implement the DataPortal using the ObjectFactory. However, due to the business object encapsulation, there doesn’t seem to be a convenient method to Load or Read the properties. I’m thinking about implementing a DTO to accomplish this. I’ve noticed that for web services, it implies the framework uses a DTO. Is there some way I can use the same methods ustilize for web services to implement a ObjectFactory DTO?

 

I'd also like to know what techniques other people are using to read/load properties from an ObjectFactory.

JonnyBee replied on Monday, September 28, 2009

Hi,

There are several projects to look at:
The key is fro your factory classes to inherit from the Csla.Server.ObjectFactory base class. This base class exposes several necessary methods to Read/Load properties and setting correct state in your BOs.

RockfordLhotka replied on Monday, September 28, 2009

What may be confusing is that a factory object can be really any object that has public Create(), Fetch(), Update() and Delete() methods that match the required signatures.

If you just start with a normal class like this, you have no way to read/load properties.

This is why the ObjectFactory base class exists - to provide you with a set of protected methods that enable your factory code to read/load business object property values, as well as various metastate values (like IsNew, IsDirty, DeletedList, etc).

However, it is totally realistic to not use the ObjectFactory base class, as long as you are willing to do some up-front work of your own.

For example, you could define your own custom base classes (for example subclass BusinessBase<T> with MyBusinessBase<T>) and in those base classes you could define methods for getting and setting the state of the object (and management of the metatate) for use by your factory objects. In other words, you could shift the power/control back into the business class and make the factory objects less intrusive.

There's something to be said for this. The ObjectFactory base class is a way of breaking encapsulation. A way to allow the factory object to modify what should be private values within the business object. I find this somewhat distasteful - but from a pragmatic perspective it is pretty useful.

The most elegant solution would be one where the business object and factory object are partners in the persistence process. Where all maniplation of private fields and metastate is inside the business class, and is invoked or triggered at the request of the factory object. This would be a very intimate set of interactions between the objects, but it could be done in a way that preserves encapsulation.

It would also be more complex, and would split the persistence responsibilities between two classes. So while it might be more elegant and "correct", I rather suspect most people would find the complexity to be disturbing and offputting.

Copyright (c) Marimer LLC