Reputation: 14145
I have interface IArticle
which should be implement by all articles. ArticleBase
abtract class defines common properties for every article and ArticleA
, ArticleB
further defines specific propertis of those articles. This situation is described more here.
To accomplish situation where every image can have any type of article and since every article implements IArticle
I decided to use IArticle type of data inside Image class.
Since I'm having hard time figuring out how to map those relationships with interface, I tried to map IArticle using nhibernate mapping by code and all other classes which implements IArticle to map using SubclassMapping with DiscriminatorValue
public class ArticleAMap : SubclassMapping<ArticleA>
{
public ArticleAMap()
{
DiscriminatorValue("ArticleA");
... mapping other ArticleA specific properties ...
}
}
public class IArticleMap : ClassMapping<IArticle>
{
public IArticleMap()
{
Property(x => x.Category, m =>
{
m.NotNullable(true);
});
....
}
}
Since my IArticle
doesn’t have access to Entity class which is responsible for creating unique identifier I cannot map Id. On the other hand I can use ArticleBase
which implements Entity.cs
and IArticle
but I then I will lose access to IArticle
which can be used in DI
.
So question is: Which is the best way to map interface in nhibernate using mapping by code?
Upvotes: 1
Views: 358
Reputation: 28097
Because ArticleBase
implements IArticle
and all your *Article
s inherit from ArticleBase
you shouldn't have any problem dependency injection wise with mapping your ArticleBase
as the base class:
public class ArticleBaseMap : ClassMapping<ArticleBase>
{
public ArticleBaseMap()
{
Property(x => x.Category, m =>
{
m.NotNullable(true);
});
// ...
}
}
Upvotes: 1