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?
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.
A better alternative to enums is WellKnownInstanceType from uNhAddIns.
It will help you implement a strategy pattern with almost no added noise.
精彩评论