Question

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?

Was it helpful?

Solution

Because ArticleBase implements IArticle and all your *Articles 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);
        });
        // ...
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top