如何使复合键枚举在流畅的 nhibernate 中使用 int 并遵循约定?

发布于 2024-12-25 02:40:09 字数 1218 浏览 1 评论 0原文

我有一个复合键实体,其中一个属性是 int,另一个属性是枚举。枚举当前是按字符串映射的,但它需要是 int。我有一个 IUserTypeConvention 已经做到了这一点,但它不适用于复合键。

我有一个 Accept() 方法,可以正确定位其中包含枚举的复合键,但我无法弄清楚 Apply() 代码。

public class CompositeKeyEnumConvention : ICompositeIdentityConvention, ICompositeIdentityConventionAcceptance
{
    public void Apply(ICompositeIdentityInstance instance)
    {
    }

    public void Accept(IAcceptanceCriteria<ICompositeIdentityInspector> criteria)
    {
        criteria.Expect(x => HasEnumKey(x));
    }

    private bool HasEnumKey(ICompositeIdentityInspector x)
    {
        if (x.KeyProperties.Count() > 0)
        {
            foreach (IKeyPropertyInspector inspector in x.KeyProperties)
            {
                if (inspector.Type.GenericArguments.Count() != 1)
                    continue;
                if (EnumConvention.IsInt32EnumType(inspector.Type.GenericArguments.First()))
                    return true;
            }
        }

        return false;
    }
}

有效的枚举约定的代码是

    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType(instance.Property.PropertyType);
    }

我无法弄清楚如何对组合键执行此操作。

谢谢!

I have a composite key entity where one property is an int, and the other is an enum. The enum is currently mapping by string, but it needs to be int. I have an IUserTypeConvention that already does this, but it doesn't work for composite keys.

I have an Accept() method that correctly locates composite keys with enums in it, but I cannot figure out the Apply() code.

public class CompositeKeyEnumConvention : ICompositeIdentityConvention, ICompositeIdentityConventionAcceptance
{
    public void Apply(ICompositeIdentityInstance instance)
    {
    }

    public void Accept(IAcceptanceCriteria<ICompositeIdentityInspector> criteria)
    {
        criteria.Expect(x => HasEnumKey(x));
    }

    private bool HasEnumKey(ICompositeIdentityInspector x)
    {
        if (x.KeyProperties.Count() > 0)
        {
            foreach (IKeyPropertyInspector inspector in x.KeyProperties)
            {
                if (inspector.Type.GenericArguments.Count() != 1)
                    continue;
                if (EnumConvention.IsInt32EnumType(inspector.Type.GenericArguments.First()))
                    return true;
            }
        }

        return false;
    }
}

The code for the enum convention that works is

    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType(instance.Property.PropertyType);
    }

I just can't figure out how to do it for a composite key.

Thanks!

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

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

发布评论

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

评论(1

月竹挽风 2025-01-01 02:40:09

就像我的其他答案这里仅进行反射

public class CompositeKeyEnumConvention : ICompositeIdentityConvention
{
    public void Apply(ICompositeIdentityInstance instance)
    {
        // when instance.KeyProperties. Count == 0 nothing happens
        foreach (IKeyPropertyInstance inspector in instance.KeyProperties)
        {
            if (inspector.Type.GenericArguments.Count() != 1)
                continue;
            if (EnumConvention.IsInt32EnumType(inspector.Type.GenericArguments.First()))
            {
                var keymapping = (KeyPropertyMapping)inspector.GetType()
                    .GetField("mapping", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic)
                    .GetValue(inspector);

                keymapping.Type = inspector.Type;
            }
        }
    }
}

like my other answer here only with reflection

public class CompositeKeyEnumConvention : ICompositeIdentityConvention
{
    public void Apply(ICompositeIdentityInstance instance)
    {
        // when instance.KeyProperties. Count == 0 nothing happens
        foreach (IKeyPropertyInstance inspector in instance.KeyProperties)
        {
            if (inspector.Type.GenericArguments.Count() != 1)
                continue;
            if (EnumConvention.IsInt32EnumType(inspector.Type.GenericArguments.First()))
            {
                var keymapping = (KeyPropertyMapping)inspector.GetType()
                    .GetField("mapping", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic)
                    .GetValue(inspector);

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