流畅的 NHibernate 将来自接口的属性映射到一处
如果许多类实现相同的接口,是否可以将这些接口属性映射到一处?还有更多代码@ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 C# 具有完整的多重继承而不仅仅是多重接口继承,那么这将很容易。看起来最接近的是为映射基类创建一个包装接口来保存公共元素。然后,您可以创建从其继承的表特定映射类。这段代码的内容大致如下:
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: