The public methods need to be virtual (as well as the properties) because 
there may be code in the body of the method that attempts to access 
lazy-loaded properties via their (usually private) backing field.  This 
means that invoking a public method could (possibly) result in an attempt 
to read values from a field that has not yet been filled with its necessary 
values.

Because of this scenario, the proxied type needs to be able to trigger its 
lazy-load on _any_ attempt to interact with it from the outside, whether a 
method or a property accessor.

In theory, of course, this constraint wouldn't be necessary if you could 
(somehow) ensure that public methods would only access members of the class 
via their public property accessors.  However, since there's no way to 
ensure developers would ever adhere to that constraint, the decision was 
made some time ago to extend the must-be-virtual requirement to all public 
members of persisted types rather than merely their property accessors. 

HTH,

-Steve B.



On Thursday, December 18, 2014 3:35:01 PM UTC-5, Suhas Chatekar wrote:
>
> On hindsight, I feel I should have put my question in more clear words. I 
> understand the dynamic proxy part and lazy loading and why properties needs 
> to be virtual for some NH features to work. My question was more around why 
> public methods need to be marked as virtual?
>
> Thanks
> Suhas
>
> On Wednesday, 17 December 2014 23:05:33 UTC, Suhas Chatekar wrote:
>>
>> I am trying to map my domain classes directly without having separate 
>> DAOs. My domain classes have some methods on it for business logic. But 
>> when I map the domain entity, NH complains that these methods should also 
>> me marked as virtual. Is it not strange when NH should not be even looking 
>> at those methods?
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to