Domain Model with Nhibernate design issue

c#, domain-driven-design, nhibernate

Solution

You can define field access strategy in your mapping for the IsBlocked property. Basically, you would say to NHibernate to use underlying private field (_isBlocked) instead of property and hence, your setter logic in IsBlocked property won't be executed.

This SO question has a good answer on access strategies.

Official NHibernate documentation.

If you are using Fluent NHibernate for mapping, this is how you could define it:

Map(x => x.IsBlocked).Access.CamelCaseField(Prefix.Underscore);

Problem

I´m trying to get started in the “DDD with C#” world. I use NHibernate as my ORM tool, thus trying to develop a PI(Persistence Ignorance) model. However, in some of my entities (which are being represented as POCOS) I have business rules in the setters of my properties. For example, I have a “User” entity which have a flag that indicates if this user is blocked or not, when this flag is true a second field called “Block Date” must be automatically filled whith the current date. Everything seems very clear and simple, but the problem arises in the moment that I´m recovering users that has already persisted in the database, even though the blocked users will have their “Blocked Dates” update to the current date, according whit this logic. Initially I thought in a second flag “isLoaded” that would indicates that the object is being hydrated by NHibernate and then this logic wouldn´t be launched, however this didn´t seem like PI. Any suggestion on how to improve this?

Original source

Related problems