将字段作为查找的 NHibernate 对象
我是 NHibernate 新手。假设您有一个像这样的 Customer 类
public class Customer {
public virtual int CustomerID { get; set; }
public virtual string Name { get; set; }
public virtual CustomerType CustomerType { get; set; }
}
,其中 CustomerType
是一个查找类,它只是将值作为
- 内部客户
- 外国客户
- 处理...
因此 CustomerType
值不会经常更改,并且在某些情况下,它们根本不会改变(例如,一个人的性别)
哪种是处理这种情况的最佳方法?
如果我像在本示例中那样映射查找类,当我尝试插入新客户时,我应该首先在数据库中找到正确的 CustomerType 吗?或者对于这些情况使用枚举类更好?
I am a NHibernate newbie. Suppose you have a Customer class like this
public class Customer {
public virtual int CustomerID { get; set; }
public virtual string Name { get; set; }
public virtual CustomerType CustomerType { get; set; }
}
where CustomerType
is a lookup class that simply handles values as
- Internal Customer
- Foreign Customer
- ...
So the CustomerType
values will not change very often and in some cases they dont change at all (eg. the gender of a person)
Which is the best way to handle this scenario?
If I map the lookup class as I did in this sample when I try to insert a new customer should I find the correct CustomerType on database first? Or is better to use Enum classes for these cases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以做任何一个 - 这并不是一个真正的 NHibernate 问题。
枚举显然在简单性方面提供了巨大的回报,但随着围绕它们的业务逻辑变得更加复杂,枚举将开始令人烦恼。
对于实体,业务规则可以作为实体的属性或方法包含在内。使用枚举时,业务逻辑必须存储在其他地方进行处理,这可能会导致代码更加复杂。
以后可以从枚举转换为实体,但不是直接的。
You can do either - its not really an NHibernate issue.
Enums obviously offer a big payoff in terms of simplicity, but will start to grate as the business logic surrounding them becomes more complex.
With an entity, business rules can be included as properties or methods of the entity. With an enum, the business logic has to be stored handled elsewhere, which can make for more complicated code.
It is possible to convert from enums to entities at a later date, but not straight forward.
枚举的更好替代方案是 WellKnownInstanceType,来自uNhAddIns。
它将帮助您实现几乎不添加任何噪音的策略模式。
A better alternative to enums is WellKnownInstanceType from uNhAddIns.
It will help you implement a strategy pattern with almost no added noise.