Custom entity creation (construction) in NHibernate
I have Order class and SubOrder: Order descendant. Now, Order has .Item and SubOrder (obviously) has it, too, but SubOrder needs SubItem to be there. Which is easy when created:
Item = new SubItem((ISomeInterface)this);
but how do I tell NHibernate to create SubItem instead of Item when it loads SubOrder from database (Session.Get<SubOrder>(id))? The type of the item is not in the database (and I don't need and don't want it to b开发者_开发知识库e there); it's always "Item for Order, SubItem for SubOrder". Moreover, it needs to be created with (ISomeInterface)SubOrder as constructor parameter.
In short, how do I manage custom entity create in NHibernate to achieve things described above? Is it possible to have a reference to SubOrder when creating SubItem (I can live with property "injection" if not)?
I can probably use IUserType but it seems to be an overkill; and I don't see how to apply IUserType to SubOrder.Item only (same for IInterceptor). Also I don't see how to get a reference to the parent order, except to use OnLoad event for the SubOrder and set SubItem properties there... which is awkward.
"OnLoad event for the SubOrder and set SubItem properties there" -> there's one solution.
Otherwise you could try writing a custom ReflectionOptimizer.
精彩评论