Adler
Adler

Reputation: 1492

NHibernate - how to resolve casting problem from nhibernate cache?

What i have is User class and say there are 2 more sub classes vipUser and regularUser. in the login page i wanna check authentication and i don't know if it is vipUser or regularUser.. if it's vip the redirect is to one location and if it's regular to another location. the authentication method must be on the User class for some resone - like this:

 Function Authenticate(ByVal username As String, ByVal password As String) As User Implements IMindriUserDao.Authenticate
        Return MyBase.GetUniqueByCriteria(Restrictions.Where(Of User)(Function(x) x.Username = username AndAlso x.Password = password))
 End Function

the issue is that after i get from the nhibernate the authentication with the User now i wanna check if he is vip or regular but the user is already in the cache as a User without a casting option to check what type the User is... any suggestions?!

Hope i was clear enough..

Thanks!

Upvotes: 0

Views: 96

Answers (2)

Jamie Ide
Jamie Ide

Reputation: 49261

Are you sure? The user object should be the correct concrete type and you should be able to use the is or as operators to check.

var user = Authenticate("userid", "password");
var vipUser = user as vipUser;
if (vipUser != null) { RedirectToChampagneRoom(); }

That said, it's much easier to work with role properties that subclassing, i.e. User.UserType.

Upvotes: 0

Firo
Firo

Reputation: 30813

2 options: polymorphism or any-mapping

Polymorphism (sorry for being c# but im not fluent in VB.NET)

class User
{
    public virtual IsVip { get { return false; } }
}

class VipUser
{
    public override IsVip { get { return true; } }
}

Any-Mapping: everywhere where you have lazyloaded reference to User

public EntityMap() { ReferencesAny(x => x.User) }

Upvotes: 1

Related Questions