C# – Why are all public methods in an entity class declared as virtual when in use with NHibernate

centitynhibernate

Why are the entity class methods declared as public virtual, when the class is to be mapped with a table using NHibernate.

Is the answer that NHibernate will be able to override these methods at runtime?

Best Answer

Yes. NHibernate will return a proxy to your object. In the simplest case, this will be fully uninitialized, and just aware of its type and id. Thus NHibernate will need to intercept method calls on the object and initialize it before the body of the message is called.