I've got a class something like this:
public class Account
{
public virtual int Id { get; private set; }
public virtual string Username { get; set; }
[EditorBrowsable( EditorBrowsableState.Never )]
public virtual string Password { get; private set; }
public void SetPassword( string password ){ ... }
public bool CheckPassword( string password ) { ... }
}
I've set it up this way since I don't ever want the Password
property used directly in code that uses the Account
type. The Account map looks something like this:
public class AccountMap : ClassMap<Account>
{
public AccountMap()
{
Id( x => x.Id );
Map( x => x.Username );
Map( x => x.Password );
}
}
When I actually use this with NHibernate I get an InvalidProxyTypeException
NHibernate.InvalidProxyTypeException: The following types may not be used as proxies:
Fringine.Users.Account: method SetPassword should be virtual
Fringine.Users.Account: method CheckPassword should be virtual
I understand that NHibernate is trying to create a proxy class to support lazy loading and that I can either mark the methods as virtual add a Not.LazyLoad() to the map to resolve the exception. But - I don't want to do either of those. I want to support lazy loading but I don't see why those methods need to be virtual.
Does NHibernate (or Castle internally) evaluate the contents of the method to determine which fields are used and optimize the lazy loading for those properties? If not, why does the method need to be virtual if all the properties are and they'll be lazy-loaded when they're referenced by the method.
Is there a way to exclude certain methods from the virtual requirement?