NHibernate - 如何解决 nhibernate 缓存的转换问题?

发布于 2024-12-01 09:21:49 字数 586 浏览 5 评论 0原文

我所拥有的是 User 类,并且还有 2 个子类 vipUser 和 RegularUser。 在登录页面中,我想检查身份验证,但我不知道它是 vipUser 还是常规用户。如果是 vip,则重定向到一个位置,如果是常规到另一个位置。 身份验证方法必须在 User 类上才能实现某些目的 - 像这样:

 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

问题是,在我从 nhibernate 获得用户身份验证后,我想检查他是 vip 还是普通用户,但用户已经在缓存中了没有强制转换选项的用户来检查用户是什么类型... 有什么建议吗?

希望我说得足够清楚..

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼疼热情 2024-12-08 09:21:49

2 个选项:多态性任意映射

多态性(抱歉,我是 C#,但我对 VB.NET 不太熟练)

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

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

任意映射:任何对 User

public 有延迟加载引用的地方实体映射()
{
ReferencesAny(x => x.User)
}

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)
}

恋竹姑娘 2024-12-08 09:21:49

你确定吗?用户对象应该是正确的具体类型,并且您应该能够使用 isas 运算符进行检查。

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

也就是说,使用子类化的角色属性(即 User.UserType)要容易得多。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文