NHibernate Component Mapping VS IUserType
Hi I would like to开发者_开发知识库 know the difference between the two and why should you use one over the other and when?
Your object model doesn't necessarily map one to one to your database model and in most cases will be richer. Components are a way of enriching your database model by encapsulating functionality in your object model. For example lets say you have two tables, people
and companies
. Both of these tables have the fields required for an address
, but the database schema, for whatever reason, doesn't have a third table for addresses
. In your application you might want to model addresses as a separate entity even though there is no logical database table for it. Here you would use a component which would allow you to project the database fields for an address.
IUserType
is a way of mapping a type to a column using a custom serialization. For example if you were to map a mongodb ObjectId
(which is nothing more than a guid), you could write a custom IUserType
to do the mapping. Other examples could be mapping a bit mask to an array of rich user types or encoding/decoding an encrypted field.
精彩评论