流畅的 NHibernate 将来自接口的属性映射到一处

发布于 2024-10-19 17:02:10 字数 1317 浏览 2 评论 0原文

如果许多类实现相同的接口,是否可以将这些接口属性映射到一处?还有更多代码@ pastebin

你可以在这里看到类有一些公共接口(但不是全部都是公共基类)我不得不重复映射。

public class PostMapping : SubclassMap<Post>
{
    public PostMapping()
    {
        Map(x => x.Text, "Text");
        // coming from IMultiCategorizedPage
        HasManyToMany(x => x.Categories).Table("PageCategories").ParentKeyColumn("PageId").ChildKeyColumn("CategoryId").Cascade.SaveUpdate();
        // coming from IMultiTaggedPage
        HasManyToMany(x => x.Tags).Table("PageTags").ParentKeyColumn("PageId").ChildKeyColumn("TagId").Cascade.SaveUpdate();
        // coming from ISearchablePage
        Map(x => ((ISearchablePage)x).SearchIndex, "SearchIndex").LazyLoad();
    }
}
public class ArticleMapping : SubclassMap<Article>
{
    public ArticleMapping()
    {
        Map(x => x.Text, "Text");
        // coming from ISingleCategorizedPage
        References(x => x.Category, "CategoryId");
        // coming from IMultiTaggedPage
        HasManyToMany(x => x.Tags).Table("PageTags").ParentKeyColumn("PageId").ChildKeyColumn("TagId").Cascade.SaveUpdate();
        // coming from ISearchablePage
        Map(x => ((ISearchablePage)x).SearchIndex, "SearchIndex").LazyLoad();
    }
}

If many classes implement the same interface(s), is it possible to map those interface properties in one place? There's more code @ pastebin

You can see here that classes have some common interfaces (but not all to make common base class) and I had to repeat mappings.

public class PostMapping : SubclassMap<Post>
{
    public PostMapping()
    {
        Map(x => x.Text, "Text");
        // coming from IMultiCategorizedPage
        HasManyToMany(x => x.Categories).Table("PageCategories").ParentKeyColumn("PageId").ChildKeyColumn("CategoryId").Cascade.SaveUpdate();
        // coming from IMultiTaggedPage
        HasManyToMany(x => x.Tags).Table("PageTags").ParentKeyColumn("PageId").ChildKeyColumn("TagId").Cascade.SaveUpdate();
        // coming from ISearchablePage
        Map(x => ((ISearchablePage)x).SearchIndex, "SearchIndex").LazyLoad();
    }
}
public class ArticleMapping : SubclassMap<Article>
{
    public ArticleMapping()
    {
        Map(x => x.Text, "Text");
        // coming from ISingleCategorizedPage
        References(x => x.Category, "CategoryId");
        // coming from IMultiTaggedPage
        HasManyToMany(x => x.Tags).Table("PageTags").ParentKeyColumn("PageId").ChildKeyColumn("TagId").Cascade.SaveUpdate();
        // coming from ISearchablePage
        Map(x => ((ISearchablePage)x).SearchIndex, "SearchIndex").LazyLoad();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍孤女 2024-10-26 17:02:10

如果 C# 具有完整的多重继承而不仅仅是多重接口继承,那么这将很容易。看起来最接近的是为映射基类创建一个包装接口来保存公共元素。然后,您可以创建从其继承的表特定映射类。这段代码的内容大致如下:

public class BasePageMapping : SubclassMap<IPage> //IPage could inherit: IMultiTaggedPage, ISearchablePage
{
    public BasePageMapping()
    {
        Map(x => x.Text, "Text");
        // coming from IMultiTaggedPage
        HasManyToMany(x => x.Tags).Table("PageTags").ParentKeyColumn("PageId").ChildKeyColumn("TagId").Cascade.SaveUpdate();
        // coming from ISearchablePage
        Map(x => ((ISearchablePage)x).SearchIndex, "SearchIndex").LazyLoad();
    }
}

public class PostMapping : BasePageMapping
{
    public PostMapping()  // don't need to specify : base() because it happens automatically
    {
        Table("the specific table");

        HasManyToMany(x => x.Categories).Table("PageCategories").ParentKeyColumn("PageId").ChildKeyColumn("CategoryId").Cascade.SaveUpdate();

        //Other table specific mappings:
    }
}

If C# had complete multiple inheritance instead of just multiple interface inheritance then this would be easy. It seems the closest would be creating a single wrapping interface for a mapping base class to hold your common elements. Then, you could create table specific mapping classes that inherit from it. Something along the lines of this code:

public class BasePageMapping : SubclassMap<IPage> //IPage could inherit: IMultiTaggedPage, ISearchablePage
{
    public BasePageMapping()
    {
        Map(x => x.Text, "Text");
        // coming from IMultiTaggedPage
        HasManyToMany(x => x.Tags).Table("PageTags").ParentKeyColumn("PageId").ChildKeyColumn("TagId").Cascade.SaveUpdate();
        // coming from ISearchablePage
        Map(x => ((ISearchablePage)x).SearchIndex, "SearchIndex").LazyLoad();
    }
}

public class PostMapping : BasePageMapping
{
    public PostMapping()  // don't need to specify : base() because it happens automatically
    {
        Table("the specific table");

        HasManyToMany(x => x.Categories).Table("PageCategories").ParentKeyColumn("PageId").ChildKeyColumn("CategoryId").Cascade.SaveUpdate();

        //Other table specific mappings:
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文