带有接口引用的FluentNHibernate AutoPersistenceModel [英] FluentNHibernate AutoPersistenceModel with Interface references
问题描述
pre $
public interface IFoo {}
public Interface IBar {IFoo Foo {get ;组; }}
$ b $ public class Foo:IFoo {}
$ b $ public class Bar:IBar
{
public IFoo Foo {get;组; }
}
我会有以下映射:
public class BarMapping:ClassMap< Bar>
{
public AnswerMapping()
{
引用< Foo>(x => x.Foo).Column(FooId).Cascade.None();
$ b $ p $如何使用AutoPersistenceModel实现相同?我简要的看了一下Conventions和ReferenceConvention的构建器,但没有任何文档或者例子,我一直在挣扎。
编辑
我现在已经把它和我的其他的一起转到了这个集合映射到接口到博客文章:
http://bronumski.blogspot.com/2011/01/making-fluent-nhibernate-automapper.html
解决方案经过一点点的挖掘,我想出了一个解决方案。有一个IReferenceConvention接口,我已经看到在其他例子中使用,但不包括这种情况。通过实现这个接口并进行一个自定义的约定,我已经能够用AutoMapStyleModel实现与ClassMap一样的功能了。
public class ReferenceConvention:IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
Type instanceType = instance.Class.GetUnderlyingSystemType();
if(instanceType == typeof(IFoo))
{
instance.CustomClass< Foo>();
}
instance.Cascade.All();
$ / code $ / pre
更通用的方法可能是:
$ $ $ $ $ $ $ $ $ $ $ $ $ {$ b $ $ $ $ $ $ {$ b $ $类型instanceType = instance.Class.GetUnderlyingSystemType();
if(instanceType.IsInterface)
{
//假设类型以一个类开头我得到具体类的名字
string className = instanceType.Name .Substring(1);
instance.CustomClass(instanceType.Assembly.GetType(
instanceType.FullName.Replace(instanceType.Name,className)));
}
instance.Cascade.All();
$ / code $ / pre
还有一个ReferenceConventionBuilder,我没有看过但是,这可能是一个更干净的解决方案。
I am trying out the FluentNHibernate AutoPersistenceModel for the first time. It was very simple to get a basic example working but I am having a problem making it fit with how I work. I normally work to interfaces so that my entities all implement an interface and reference all related entities by their interface, not their concrete type. Given the following classes:
public Interface IFoo { }
public Interface IBar { IFoo Foo { get; set; } }
public Class Foo : IFoo { }
public Class Bar : IBar
{
public IFoo Foo { get; set; }
}
I would have the following mapping:
public class BarMapping : ClassMap<Bar>
{
public AnswerMapping()
{
References<Foo>( x => x.Foo ).Column( "FooId" ).Cascade.None();
}
}
How would I achieve the same with the AutoPersistenceModel? I have had a brief look at Conventions and the ReferenceConvention builder but without any documentation or examples I have been floundering.
EDIT
I have now turned this along with my other SO post on collection mapping to interfaces into a blog post:
http://bronumski.blogspot.com/2011/01/making-fluent-nhibernate-automapper.html
解决方案 After a little digging around I have come up with a solution. There is an IReferenceConvention interface which I have seen used in other examples but don't cover this scenario. By implementing the interface and doing a custom convention I have been able to achieve the same thing with the AutoPersistenceModel that I was with ClassMap.
public class ReferenceConvention : IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
Type instanceType = instance.Class.GetUnderlyingSystemType();
if (instanceType == typeof(IFoo))
{
instance.CustomClass<Foo>();
}
instance.Cascade.All();
}
}
A more generic approach might be:
public class ReferenceConvention : IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
Type instanceType = instance.Class.GetUnderlyingSystemType();
if (instanceType.IsInterface)
{
// Assuming that the type starts with an I get the name of the concrete class
string className = instanceType.Name.Substring( 1 );
instance.CustomClass(instanceType.Assembly.GetType(
instanceType.FullName.Replace( instanceType.Name, className )));
}
instance.Cascade.All();
}
}
There is also a ReferenceConventionBuilder which I have not looked at yet but this could be a cleaner solution.
这篇关于带有接口引用的FluentNHibernate AutoPersistenceModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!