我怎样才能使复杂的关键枚举使用流利的nhibernate int与约定? [英] How can I make composite key enums use int in fluent nhibernate with a convention?

查看:145
本文介绍了我怎样才能使复杂的关键枚举使用流利的nhibernate int与约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有一个Accept()方法,正确地定位组合键与枚举,但我找不到Apply()代码。

  public class CompositeKeyEnumConvention:ICompositeIdentityConvention,ICompositeIdentityConventionAcceptance 
{
public void Apply(ICompositeIdentityInstance instance)
{
}
$ b public void Accept(IAcceptanceCriteria< ICompositeIdentityInspector> criteria)
{
criteria.Expect x => HasEnumKey(x));


bool HasEnumKey(ICompositeIdentityInspector x)
{
if(x.KeyProperties.Count()> 0)
{
foreach(在x.KeyProperties中的IKeyPropertyInspector检查器)
{
if(inspector.Type.GenericArguments.Count()!= 1)
continue;
if(EnumConvention.IsInt32EnumType(inspector.Type.GenericArguments.First()))
return true;
}
}

return false;




$ b

枚举约定的工作原理是公共无效应用(IPropertyInstance实例)

实例类型(instance.Property.PropertyType);
}

我只是不知道如何为复合键执行此操作。

谢谢!

解决方案

这里只有反思

  public class CompositeKeyEnumConvention:ICompositeIdentityConvention 
{
public void Apply(ICompositeIdentityInstance instance)
{
// instance.KeyProperties。计数== 0没有反应
foreach(instance.KeyProperties中的IKeyPropertyInstance检查器)
{
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;
}
}
}
}


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!

解决方案

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

这篇关于我怎样才能使复杂的关键枚举使用流利的nhibernate int与约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆