NHibernate requires not only settable properties of your domain to be virtual but also get-only properties and methods. Does anyone know what the reason for this is?
I cannot imagine a possible use.
NHibernate requires not only settable properties of your domain to be virtual but also get-only properties and methods. Does anyone know what the reason for this is?
I cannot imagine a possible use.
I'm not an NHibernate expert, but from reading Oren's blogs, and from what I've learned of NH, the basic pattern of use is to proxy the objects for the ORM. This means, among other things, that the only things you'll be able to map are going to be things that are made virtual, otherwise NH would have to use a different strategy to redefine the implementations under the hood.
Because you may want to access your settable properties from there, and maybe in some fancy indirect or reflection way. So it's to be 100% sure that when your entity is used, it's initialized. Example:
public string GetSmth
{
get
{
// NHibernate will not know that you access this field.
return _name;
}
}
private string _name;
public virtual string Name { get { return _name; } set { _name = value; } }
Here's Ayende explaing this in relation to Entity Framework: http://ayende.com/Blog/archive/2009/05/29/why-defer-loading-in-entity-framework-isnrsquot-going-to-work.aspx
AddProduct is a non virtual method call, so it cannot be intercepted. Accessing the _products field also cannot be intercepted.
The only reason I see why one would want method execution without messing with NH proxy (i.e. loading data) is when you have method that do not access your class' data. But in this case, if this method does not use your class' data, what does it belong to that class at all?
The reason is lazy loading. In order to make lazy loading possible, a proxy class is created. It must intercept every call from "outside" in order to load your entity before actual method/property is executed. If some methods/properties were not virtual it would be impossible to intercept these calls and entity wouldn't be loaded.