Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Component Mapping VS IUserType

Tags:

nhibernate

Hi I would like to know the difference between the two and why should you use one over the other and when?

like image 627
Blair Avatar asked Nov 06 '22 08:11

Blair


1 Answers

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.

like image 140
jonnii Avatar answered Nov 15 '22 08:11

jonnii