NGilead has been designed to be pretty straightforward, so it will only requires you two steps to send your NHibernate POCO wherever you want :
  • LightEntity inheritance
  • PersistentBeanManager initialization


NGilead needs that your Domain classes inherit from the LightEntity one. This technical inheritance is required to store NHibernate persistence information intp a regular, serializable compatible form.
If your Domain classes already have a technical inheritance, you can just implement the ILightEntity interface, since the LightEntity class only manage 2 properties (you can of source read the LightEntity source code to implement the ILightEntity interface).

Note that this technical inheritance is not a fatality : the Java counterpart also implement a stateful mode, where the persistence information is stored in the user HTTP session instead of the POCO himself, so the LightEntity inheritance is not needed anymore. We hope to implement this feature very soon...


The PersistentBeanManager class is the NGilead facade and the main entry point. It handles the two basic operations:
  • The Clone operation is used to create a "neutral clone" of the NHibernate POCO, usable by any serialization process : lazy but not loaded associations are replaced by null, and persistent collections by their C# regular counterparts.
  • The Merge operation transforms the "neutral clone" back to a NHibernate POCO. Note that this operation has nothing to do with NHibernate Merge one, and that it does not propagate modifications to the database.

The first and only step to initialize the PersistentBeanManager is to provide it your SessionFactory :
PersistentBeanManager.Instance.PersistentUtil = new NHibernatePersistentUtil(yourSessionFactory);

(Do not care about the NHibernatePersistentUtil class : it is just a NHibernate encapsulation, so NGilead can be reused with another ORM library).


Once the PersistentBeanManager have bean initialized and persistent POCO inherit from LightEntity, just call the Clone method before sending persistent data to the client side (Silverlight, Web Services,...) and the Merge one for any persistent input parameter :

public MyEntity saveEntity(MyEntity entity)
// Merge entity
MyEntity mergedEntity = PersistentBeanManager.Instance.Merge(entity) as MyEntity;

// Save with NHibernate

// Clone saved entity before sending it
return (MyEntity) PersistentBeanManager.Instance.Clone(mergedEntity);

Last edited Apr 6, 2010 at 8:01 PM by bmarchesson, version 3


No comments yet.