在 Fluent NHibernate 中,我将如何映射以下域模型?
我有一个用户类,看起来像这样
public class User
{
public virtual int Id { get; set; }
public virtual long ValueA { get; set; }
public virtual int? ValueB { get; set; }
}
ValueA
由系统自动分配。它用于映射到UserClass
的查找。但是,如果 ValueB 的值存在,那么它将以不同的方式查找 UserClass
。
现在我处理它的方法是获取用户,然后每次执行单独的查找。
return user.ValueB.HasValue ? Find(user.ValueB.Value) : Find(user.ValueA);
有没有办法让 Fluent NHibernate 为我做这件事,这样我就可以将 UserClass 作为 User 类的属性,而不必单独进行查找?我正在考虑 ComponentMap,但我不确定如何使其考虑两个可能的查找值。
现在我能想到的唯一其他解决方案是将每个返回语句包装在我的 UserService 类中,以在返回用户之前分配用户级别,这不是我想继续使用的解决方案。
public User Find(long valueA)
{
// Get the user
return AssignUserLevel(user);
}
public User AssignUserLevel(User user)
{
// set the user level of the user
}
I have a user class that looks something like this
public class User
{
public virtual int Id { get; set; }
public virtual long ValueA { get; set; }
public virtual int? ValueB { get; set; }
}
ValueA
is automatically assigned by the system. It is used in a lookup that would map to UserClass
. However, if a value for ValueB exists, then it would do the lookup for UserClass
in a different way.
Right now the way I handle it is to get the User and then perform a separate lookup each time.
return user.ValueB.HasValue ? Find(user.ValueB.Value) : Find(user.ValueA);
Is there any way to make Fluent NHibernate do this for me so I can have UserClass as a property on the User class instead of having to do the lookup separately? I was thinking of the ComponentMap but I'm not sure how to make it account for the two possible lookup values.
Right now the only other solution I can think of is to wrap each return statement in my UserService class to assign the user level before returning the user, which isn't a solution I want to continue t ouse.
public User Find(long valueA)
{
// Get the user
return AssignUserLevel(user);
}
public User AssignUserLevel(User user)
{
// set the user level of the user
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,这是 NHibernate 无法处理的一些逻辑(ValueB 覆盖 ValueA),但您可以让它使用引用而不是使用 Id 进行查找。
Well, that's a bit of logic that NHibernate cannot handle (ValueB overrides ValueA), but you can get it to use references rather than use Ids to lookup.