I'm currently working on a web solution using NHibernate for persistence and Castle Windsor for IoC/DI.
Castle is taking care of providing services to the UI (a form of MVP) including business logic, actions to perform CRUD and validation. NHibernate is mapping POCOs into the database.
This is coming together nicely, but it seems that I have architectured my solution using the "anemic domain model" anti-pattern. I.e. my POCOs are dumb value objects being passed around the application, rather than encapsulating business rules.
The reason I've ended up with this anemic model is that NHibernate is not the only back-end consumer of the domain objects, and the business rules and actions have differing implementations for different consumers. E.g. I might get objects from a TIBCO ESB with one implementation and a DB via NHibernate with another.
Consequently I can't code business actions and validations directly into the POCO as they vary (which is where Castle Windsor enters the scene). Same action or rule, different implementation.
It works, but can I do better?
So, I'm wondering if NHibernate supports the loading of entities through the Castle container? Then I can move business logic (actions) and validation into (closer?) to the domain objects (i.e. NHibernate will delegate object creation to Castle, which will satisfy dependencies specified as constructor arguments or properties). NHibernate could then fill in mapped data. I'm kinda hoping for an ICustomLoader interface!
I admit I haven't really thought this through. Does it seem a reasonable idea? And if NH doesn't currently support this type of behaviour can someone point me to likely namespaces (NHibernate.Loader and NHibernate.Persister look promising) and any doco on the way NH builds objects? Would lazy initialisation cause problems?
Am I making sense? Any input appreciated!
Regards,
Andy.
|