Friday, May 20, 2005

[Opf3] ICustomPersister: What a strange interface?!

What is ICustomPersister? First: it's an interface. Second: it allows to create complex objects which hold a bunch of persistent objects.

Perhaps you think now: WHAT?

ICustomPersister contains two methods: Load, Persist. An object that implements this interface can manage the load and persist process by itself. When an object implementing this interface is persistet using the ObjectContext:

context.PersistChanges(objectImplementingICustomPersister);

The ObjectContext returns the control to the object. The object can now save it's parts by using the ObjectContext. The same happens while loading the object.

When is something like this useful?
Immagine you have a complex object. For example a list of objects (the ObjectSet<T> implements this interface) or a car object. The car, for example, has two doors and an engine. It holds internally those three objects and knows how to save or load them from the database. When you load the Car object from the storage you have simply to set an instance of the Car object as argument of the GetObject method of the ObjectContext.

Car car = context.GetObject<Car>(new Car(), "Name = {0}", "Bugatti");

The ObjectContext returns the control of the load process to the instance of the Car object. The Car object gets the query and the instance of the ObjectContext that redirected the control. This allows the object to load the parts by itself.

While saving:

context.PersistChanges(car);

the object has also full control over its persisting behaviour. The car object can save first the doors and then the engine or first the engine and then the doors. The ObjectSet<T> saves for example all persistent objects in the main list and deletes all persistent objects that are found in the RemovedList property of the ObjectSet<T>.

3 Comments:

Anonymous Anonymous said...

Wow, I really like this one. I have a website that talks mostly about seo consultancy You should check it out sometime.

10/30/2005 10:51 AM  
Anonymous Anonymous said...

Leave me your blog address at no fax payday loan and I'll be sure to add
it to my blogroll and come visit you. It's just over there on the right - just click
to add your pin to the map.

6/27/2006 12:09 PM  
Anonymous Anonymous said...

Hi people
I do not know what to give for Christmas of the to friends, advise something ....

12/03/2006 3:08 PM  

Post a Comment

<< Home