gpt4 book ai didi

c# - Fluent NHibernate 映射属性来自一个地方的接口(interface)

转载 作者:太空宇宙 更新时间:2023-11-03 14:21:50 24 4
gpt4 key购买 nike

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

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

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();
}
}

最佳答案

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

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:
}
}

关于c# - Fluent NHibernate 映射属性来自一个地方的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5142687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com