在 Fluent NHibernate 中,我将如何映射以下域模型?

发布于 2024-09-06 03:19:06 字数 862 浏览 2 评论 0原文

我有一个用户类,看起来像这样

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 技术交流群。

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

发布评论

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

评论(1

最笨的告白 2024-09-13 03:19:06

嗯,这是 NHibernate 无法处理的一些逻辑(ValueB 覆盖 ValueA),但您可以让它使用引用而不是使用 Id 进行查找。

public class User
{
    public virtual int Id { get; set; }
    public virtual UserClass ValueA { get; set; }
    public virtual UserClass ValueB { get; set; }
    public virtual UserClass UserClass { get { return ValueB ?? ValueA; } }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("MyUserTable");
        Id(user => user.Id).GeneratedBy.Identity();
        References(user => user.ValueA).Not.Nullable();
        References(user => user.ValueB).Nullable();
    }
}

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.

public class User
{
    public virtual int Id { get; set; }
    public virtual UserClass ValueA { get; set; }
    public virtual UserClass ValueB { get; set; }
    public virtual UserClass UserClass { get { return ValueB ?? ValueA; } }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("MyUserTable");
        Id(user => user.Id).GeneratedBy.Identity();
        References(user => user.ValueA).Not.Nullable();
        References(user => user.ValueB).Nullable();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文