与NHibernate的设计问题域模型 [英] Domain Model with Nhibernate design issue

查看:142
本文介绍了与NHibernate的设计问题域模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试获得了DDD与C#的世界开始。
我用的NHibernate作为我的ORM工具,从而试图建立一个PI(持久性无知)模型。
然而,在我的一些实体(被扯到POCOS)我有我的属性的制定者业务规则。
为例,我有一个用户的实体,有一个标志,指示,如果该用户被阻塞或没有,当这个标志为true阻止日期
。第二场必须自动填充蒙山当前日期。
似乎一切都非常简单明了的,但在I'm恢复一个已经在数据库中的用户坚持的那一刻出现问题,即使
被阻止用户将有自己的被阻止的日期更新当前的日期,按照白衣这个逻辑。
起初我以为在第二个标志isLoaded,将表明对象正由NHibernate的水合,然后这个逻辑也不会转回去推出,
然而,这didn't看起来PI。关于如何提高这任何建议?

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?

推荐答案

您可以在映射的IsBlocked属性定义字段访问策略。基本上,你会说NHibernate的使用底层的私有字段(_isBlocked),而不是财产,因此,在IsBlocked财产您的setter逻辑将不会被执行。

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.

<一个HREF =http://stackoverflow.com/questions/2339264/property-access-strategies-in-nhibernate>这太问题对接入战略的一个很好的答案。

This SO question has a good answer on access strategies.

NHibernate的官方的文档

Official NHibernate documentation.

如果您在使用功能NHibernate映射,这是你如何定义它:

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

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

这篇关于与NHibernate的设计问题域模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆