使用实体框架和存储库模式实现每个层次结构的表
我已经使用抽象文档实体和多个派生实体(博客、页面等)实现了每个层次结构的表实体数据模型。我有使用 Document 实体的带有方法签名的存储库接口,像这样
public Document Load(Guid firmId, int prettyId)
{
// the OfType<> can be OfType<Page>, OfType<Blog>, ...
var instance = (from c in _ctx.Documents.OfType<X>() where c.firm_id == firmId && c.PrettyId == prettyId select c).FirstOrDefault();
...
}
我只有一个实现存储库的类,并且它使用 Document 作为从方法返回的类型。我不需要从 Document 派生的不同类型的自定义实现,因为加载、插入和更新的实现细节对于所有类型都是相同的。我只需要识别/提供我们想要使用的方法的类型。
希望你能明白我的意思。请不要回复有关如何建模 TPH 的参考资料,因为我已经这样做了并且建模得很好。
I've implemented table per hierarchy Entity Data Model with an abstract Document entity and multiple derived entities (Blog, Page, ...). I have repository interface with method signatures using Document entity like this
public Document Load(Guid firmId, int prettyId)
{
// the OfType<> can be OfType<Page>, OfType<Blog>, ...
var instance = (from c in _ctx.Documents.OfType<X>() where c.firm_id == firmId && c.PrettyId == prettyId select c).FirstOrDefault();
...
}
I only have one class that implements the repository and it is using Document as the type to return from methods. I don't need custom implementations for different types that derive from Document because the implementation specifics for loading, inserting and updating are the same for all. I just need to identify/provide the type to the methods that we want to work with.
Hopefully you will understand what I mean. Please do not reply with references on how to model the TPH because I've already done that and it is modeled fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我实际上发现我不需要运行时检测,因为我可以在 MVC 控制器中编译时提供类型,这与存储库(只有一个)不同,是特定于类型的(我有 PageController、BlogController 等),像这样:
在我的存储库界面中,我现在有这个:
在存储库实现中,我有这个:
这有效并且看起来不错。
I've actually discovered that I don't need runtime detection because I can provide type at compile time in my MVC controllers, which unlike the repository (which is only one) are type specific (I have PageController, BlogController, etc.), like this:
In my repository interface I now have this:
and in repository implementation I have this:
This works and looks kinda nice.
简单的方法是为要加载的每个特殊文档类型创建单独的方法,或者使用 switch / case 语句以及一些鉴别器作为 Load 方法中的参数。我知道您想避免这样的解决方案。在这种情况下,您应该尝试使用反射,因为不可能在运行时提供泛型类型参数。检查此答案以通过反射调用泛型方法。您将使用 OfType 调用基本查询并获取 IQueryalbe 的实例,该实例将用于带有 where 条件的查询的第二部分。
Simple way is to create separate method for each special Document type you want to load or to use switch / case statements with some discriminator as parameter in Load method. I understand you want to avoid such solution. In that case you should try to use reflection because providing generic type parameter at runtime is not possible. Check this answer for calling generic method with reflection. You will invoke the base query with OfType and get the instance of IQueryalbe which will be used in for second part of query with where conditions.