如何使复合键枚举在流畅的 nhibernate 中使用 int 并遵循约定?
我有一个复合键实体,其中一个属性是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像我的其他答案这里仅进行反射
like my other answer here only with reflection