Domain Model with Nhibernate Design Problem

I am trying to get started in the "DDD with C #" world. I am using NHibernate as an ORM tool, trying to develop a PI (Persistence Notorance) model. However, in some of my entities (which are represented as POCOS), I have business rules in my properties settings. For example, I have a โ€œUserโ€ object that has a flag indicating whether this user is blocked or not, when this flag is a true second field โ€œBlock Dateโ€, should be automatically filled with the current date. Everything seems very clear and simple, but the problem arises at the moment when I restore users who are already stored in the database, although blocked users will update their "Blocked dates" to the current date, according to this logic. Initially, I thought that the second flag is "isLoaded", which indicates that the object is hydrated by NHibernate, and then this logic will not start, however it did not seem to PI. Any suggestion on how to improve this?

+6
source share
2 answers

You can define a field access strategy in your mapping for the IsBlocked property. Basically, you would tell NHibernate to use a base private field (_isBlocked) instead of a property, and therefore your setter logic in the IsBlocked property will fail.

This SO question has a good answer to access strategies.

Official NHibernate documentation .

If you use Fluent NHibernate for matching, you can define it:

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

In addition to Miroslavs solution for the NHibernate problem, I would recommend that you refuse to set the logic for property developers, especially when you need to change other fields.

 public void Block() { _isBlocked = true; _blockedDate = DateTime.Now; } 

See the answers to this question for what.

+4
source

Source: https://habr.com/ru/post/917035/


All Articles