Mapping interface or abstract class component

Posted by Yann Trevin on Stack Overflow See other posts from Stack Overflow or by Yann Trevin
Published on 2009-10-10T19:23:04Z Indexed on 2010/06/07 19:52 UTC
Read the original article Hit count: 648

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about nhibernate