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?
Asked
Active
Viewed 668 times
2 Answers
4
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);

Community
- 1
- 1

Miroslav Popovic
- 12,100
- 2
- 35
- 47
4
In addition to Miroslavs solution for the NHibernate problem, I'd really recommend moving away from putting logic behind property setters, especially when other fields need to be changed.
public void Block()
{
_isBlocked = true;
_blockedDate = DateTime.Now;
}
See answers to this question for why.

Community
- 1
- 1

David Masters
- 8,069
- 2
- 44
- 75