映射接口或抽象类组件 [英] Mapping interface or abstract class component
问题描述
请考虑以下简单用例:
pre $ $ $ $ $ $ $ $ $ $ $ $ Id {get;保护组}
public virtual IBar Bar {get;组; }
}
public interface IBar
{
string Text {get;组; }
}
public class Bar:IBar
{
public virtual string Text {get;组; }
}
以及流利的nhibernate地图类:
public class FooMap:ClassMap< Foo>
{
public FooMap()
{
Id(x => x.Id);
Component(x => x.Bar,m =>
{
m.Map(x => x.Text);
});
当运行配置查询时,例外:
$ b
NHibernate.InstantiationException :
不能实例化抽象类或接口:NHMappingTest.IBar
NHibernate试图实例化一个 IBar
对象,而不是 Bar
具体类。如何让Fluent-NHibernate知道当属性返回一个接口或抽象基类时要实例化哪个具体类?
$ b 编辑:明确指定通过编写 Component< Bar>
(如Sly所建议的),组件的类型不起作用,并导致相同的异常。
EDIT2:感谢vedklyv和Paul Batum:这样的映射应该很快 现在成为可能。 b $ b
解决方案我没有尝试过这个,但是我看到了一个流利的nh源代码,lambda被转换为具体的类:
public class FooMap:ClassMap< Foo>
{
public FooMap()
{
Id(x => x.Id);
Component(x =>(Bar)x.Bar,m =>
{
m.Map(x => x.Text);
});
编辑:显然这与Sly的建议完全一样所以没有任何好处。我测试了它与流利nh的主干版本,它没有工作。如果你使用多对一的映射,它可以工作: public class FooMap:类映射<富>
{
public FooMap()
{
Id(x => x.Id);
参考< Bar>(x => x.Bar).Cascade.All();
}
}
公共类BarMap:ClassMap< Bar>
{
public BarMap()
{
Id(x => x.Id);
Map(x => x.Text);
更新
这实际上是一个简单的修复。我已经提交了一个补丁(链接文本)使狡猾的解决方案工作。
Please consider the following simple use case:
public class Foo
{
public virtual int Id { get; protected set; }
public virtual IBar Bar { get; set; }
}
public interface IBar
{
string Text { get; set; }
}
public class Bar : IBar
{
public virtual string Text { get; set; }
}
And the fluent-nhibernate map class:
public class FooMap : ClassMap<Foo>
{
public FooMap()
{
Id(x => x.Id);
Component(x => x.Bar, m =>
{
m.Map(x => x.Text);
});
}
}
While running any query with configuration, I get the following exception:
NHibernate.InstantiationException:
"Cannot instantiate abstract class or interface: NHMappingTest.IBar"
It seems that NHibernate tries to instantiate an IBar
object instead of the Bar
concrete class. How to let Fluent-NHibernate know which concrete class to instantiate when the property returns an interface or an abstract base class?
EDIT: Explicitly specify the type of component by writing Component<Bar>
(as suggested by Sly) has no effect and causes the same exception to occur.
EDIT2: Thanks to vedklyv and Paul Batum: such a mapping should be soon is now possible.
解决方案 I haven't tried this myself but i saw an example in the fluent nh source where the lambda is cast to the concrete class:
public class FooMap : ClassMap<Foo>
{
public FooMap()
{
Id(x => x.Id);
Component(x => (Bar) x.Bar, m =>
{
m.Map(x => x.Text);
});
}
}
EDIT: Obviously that's exactly the same result as Sly's suggestion so that did no good. I tested it against the trunk version of fluent nh and it didn't work. It does work though if you use a many-to-one mapping:
public class FooMap : ClassMap<Foo>
{
public FooMap()
{
Id(x => x.Id);
References<Bar>(x => x.Bar).Cascade.All();
}
}
public class BarMap : ClassMap<Bar>
{
public BarMap()
{
Id(x => x.Id);
Map(x => x.Text);
}
}
UPDATE
This was actually an easy fix. I have submitted a patch (link text) that makes Sly's solution to work.
这篇关于映射接口或抽象类组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!