将字段作为查找的 NHibernate 对象

发布于 2024-10-01 00:09:55 字数 539 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日情怀 2024-10-08 00:09:55

你可以做任何一个 - 这并不是一个真正的 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.

策马西风 2024-10-08 00:09:55

枚举的更好替代方案是 WellKnownInstanceType,来自uNhAddIns

它将帮助您实现几乎不添加任何噪音的策略模式。

A better alternative to enums is WellKnownInstanceType from uNhAddIns.

It will help you implement a strategy pattern with almost no added noise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文