To prevent that changes to your objects end up accidently into your database, NHibernate supports type immutability.
This is something you can set at the ClassMap level(if you are using the mapping by code):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductMapping : ClassMapping<Product> | |
{ | |
public ProductMapping() | |
{ | |
Mutable(false); | |
//Removed all other properties | |
} | |
} |
With Fluent Nhibernate, you have to use the ReadOnly property:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductMap : ClassMap<Product> | |
{ | |
public ProductMap() | |
{ | |
ReadOnly(); | |
// Mappings | |
} | |
} |
Remark: Using this property makes sure that the objects that you retrieve are read-only, so you cannot UPDATE them. However, it does NOT prevent the creation of new records or even the deletion of existing records in the database.