使用 NHibernate 转变传统价值

发布于 2024-10-21 01:05:59 字数 711 浏览 2 评论 0原文

我们有一些带有遗留模式的旧表,我们发现它们很难使用。

是否可以使用 NHibernate 将其中一些数据转换为更好的模型?

例如,我们可能有一个整数状态列,我们希望将其分解为多个属性。

Legacy status: 0 - Active, 1 - Inactive, 2 - TemporarilyInactive, 3 - etc

我们想要这样的东西:(

bool IsActive { get; set; }
Status Status { get; set; }

其中 Status 是一个枚举)

我知道我们可以使用一个 protected 字段来获取状态,然后定义 getter额外的属性根据支持字段返回适当的值,但我很确定这将禁用基于这些属性进行查询的能力。

然而,通过这种方式,我们将无法执行诸如 .Query(p => p.IsActive) 之类的查询并将其转换为 SQL,例如 where status = 0代码>,对吗?

是通过自定义 IUserTypes 的唯一方法吗?如果是这样,是否有任何帮助程序框架可以使使用 IUserType 更轻松地实现这一目标?

其他人如何处理这个问题?

We have some old tables with legacy schemas that we find it hard to work with.

Is it possible to use NHibernate to transform some of this data into a nicer model?

For example we might have an integer status column that we want to break down into several properties.

Legacy status: 0 - Active, 1 - Inactive, 2 - TemporarilyInactive, 3 - etc

We'd want to have something like:

bool IsActive { get; set; }
Status Status { get; set; }

(where Status is an enum)

I know that we can use a protected field that can take the status and then define the getters for the extra properties to return the appropriate value based on the backing field, but I'm pretty sure that this will disable the ability to query based on these properties.

Through this however, we wouldn't be able to do queries such as .Query(p => p.IsActive) and get that translated to SQL such as where status = 0, right?

Is the only way through custom IUserTypes? If so, is there any helper framework that makes working with IUserType easier in order to achieve this?

How do other people handle this?

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

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

发布评论

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

评论(1

寻梦旅人 2024-10-28 01:05:59

您可以创建自己的“枚举”类。

public class Status
{
    //The numeric (legacy) code
    public int Code{ get;private set; }
    //The human readable name
    public string Name{ get; private set; }
    //If this status is an active status
    public bool IsActive { get; private set; }

    private Status(int aCode, string aName, bool anIsActive)
    {
        Code = aCode;
        Name = aName;
        IsActive = anIsActive;
    }

    public static Status ACTIVE = new Status(0, "Active");
    public static Status INACTIVE = new Status(1, "Inactive");
    //Other status here...
    private static Status[] STATUSES = {Active,Inactive,...};

    //Returns a status based on the passed in code
    public static Status GetByCode(int aCode)
    {
        return STATUSES.FirstOrDefault(aStatus => aStatus.Code == aCode);
    }
}

然后,您可以让 NHibernate 使用旧值设置一个私有变量,并使用一个 getter/setter 在枚举和旧值之间进行转换。

private int theLegacyStatus;  //This is the value that NHibernate sets
public Status
{
    get
    {
        return Status.GetStatusByCode(theLegacyStatus);
    }
    set
    {
        theLegacyStatus = value.Code;
    }
}

然后,您可以在 NHibernate 查询中使用此枚举:.Query(p => p.Status.Code)

You can create your own "enumeration" class.

public class Status
{
    //The numeric (legacy) code
    public int Code{ get;private set; }
    //The human readable name
    public string Name{ get; private set; }
    //If this status is an active status
    public bool IsActive { get; private set; }

    private Status(int aCode, string aName, bool anIsActive)
    {
        Code = aCode;
        Name = aName;
        IsActive = anIsActive;
    }

    public static Status ACTIVE = new Status(0, "Active");
    public static Status INACTIVE = new Status(1, "Inactive");
    //Other status here...
    private static Status[] STATUSES = {Active,Inactive,...};

    //Returns a status based on the passed in code
    public static Status GetByCode(int aCode)
    {
        return STATUSES.FirstOrDefault(aStatus => aStatus.Code == aCode);
    }
}

Then you can have NHibernate set a private variable with the legacy value and have a getter/setter that converts between the enumeration and the legacy value.

private int theLegacyStatus;  //This is the value that NHibernate sets
public Status
{
    get
    {
        return Status.GetStatusByCode(theLegacyStatus);
    }
    set
    {
        theLegacyStatus = value.Code;
    }
}

You can then use this enumeration in NHibernate queries: .Query(p => p.Status.Code)

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