This is an Nhibernate question that has bothered me for some time…
If I model a simple order entry domain as:
public class Order: BaseEntity
{
public virtual Customer Customer {get; set;}
public Order(Customer customer)
{
...
}
}
public class Customer: BaseEntity
{
public virtual string Name {get; set;}
public virtual Order CreateOrder()
{
return new Order(this);
}
}
While the above code works to create an Order instance, the newly created instance will not be persisted to the DB unless either:
- The BaseEntity or the derived class is aware of the NHibernate
session (which breaks POCO) - There is a service layer (or repository) that calls ISession.Save() on the
newly created Order object that needs to be NHibernate-aware
So, this leads me to believe that my NHibernate POCO classes themselves should not contain any of our business rules (and should be kept to only properties and constructors), but the service layer ‘above’ the domain model should be where the logic should live. Presumably, this service layer would receive its persistence functionality through dependency injection.
Anyone care to confirm/deny my assertion that business methods should NOT exist within the POCO model?
Thanks,
David
Business logic in the domain model is the whole point of having the model in the first place.
You can use Nhibernate’s cascade feature to make your order become automatically persisted if the customer owning it is persistent.