热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

实体框架4,继承vs扩展?-EntityFramework4,inheritingvsextending?

Whataretheadvantagesdisadvantagestoeachmethod?每种方法有哪些优点缺点?IknowIvereadsomewhereine

What are the advantages/disadvantages to each method?

每种方法有哪些优点/缺点?

I know I've read somewhere in either a book or on this site why using table inheritance is crappy for Entity Framework 4.

我知道我已经在书籍或本网站的某处阅读过为什么使用表继承对于Entity Framework 4来说很糟糕。

For instance, why not make one table which has an entityId, datecreated, datemodified and then have every other class inherit that in entity framework? Then my tables for all other entities don't need to have those columns. Then I can have a person class inherit that base class, and then a specific person inherit person.

例如,为什么不创建一个具有entityId,datecreated,datemodified的表,然后让其他所有类继承实体框架中的那个?然后我的所有其他实体的表不需要那些列。然后我可以让一个人类继承该基类,然后一个特定的人继承人。

I am not sure of the advantages of this though other than writing a smaller SQL script to generate the database...

除了编写一个较小的SQL脚本来生成数据库之外,我不确定这有什么优点......

Disadvantages I see are that it makes querying /viewing the data directly in SQL a big pain in the ass (all relevant information is broken across so many tables), and I also asked my friend who said:

我看到的缺点是,它直接在SQL中查询/查看数据是一个很大的痛苦(所有相关信息在这么多表中被打破),我也问过我的朋友说:

"The biggest thing that sticks out for me is the fact that i wouldn't want my database to rely on the current inheritance structure of my application. if, for whatever reason, i wanted to change the design of my application in terms of inheritance, it wouldn't matter because my data wouldn't be reliant on my current system design. i think of data storage as just that--storage. the database is normalized according to proper database design, but inheritance is a programatic choice of application development, not data storage. that alone would prevent me from using it. inheritance is a very difficult thing to do properly when it comes to designing an application. it's much easier to change application code than it is to change and migrate database data when most less-seasoned devs approach a problem they approach it with inheritance. i did too when i first started developing. it logically makes sense. however once developing for a long time you learn that delegation is really the best way to go (services calling services in the case of soa) and that single-purpose services provide a lot more reuse than inheritance."

Which also makes sense to me.

这对我来说也很有意义。

So

所以

1) In general, what are the pros/cons of inheritance vs extending
2) In my specific example above, what would be more appropriate?
3) If my example is crappy for either or both, what is a good example for using inheritance and for using extending?

1)一般来说,继承与扩展的利弊是什么2)在我上面的具体例子中,哪些更合适? 3)如果我的例子对于其中一个或两个都很糟糕,那么使用继承和使用扩展的好例子是什么?

I've used both before but as I am far from seasoned, I am still unsure how to handle all situations.

我之前使用过两者,但由于我远没有经验丰富,我仍然不确定如何处理所有情况。

10 Votes, 8 favorited, over a hundred views and no one can expand? =(.

10个投票,8个收藏,超过100个观点,没有人可以扩展? =(。

3 个解决方案

#1


5  

I have tried similar thing as you describe, and the main problem I see, is that u can't create ObjectSet for derived type. So you will not have ObjectSet repository. If you derive all your Entities from for example BusinessObject entity, you will be able to work only with ObjectSet. If you want to query only Person repository, you can write query like Context.BusinessObjectSet.OfType().ToList() but i think it will not generate proper SQL under the hood and will first get full BusinessObject rows and then filter out Person entities in memory. So I guess it will have huge performance impact.

我尝试过类似于你描述的东西,我看到的主要问题是你不能为派生类型创建ObjectSet 。所以你不会有ObjectSet 存储库。如果从例如BusinessObject实体派生所有实体,则只能使用ObjectSet 。如果你只想查询Person存储库,你可以编写类似Context.BusinessObjectSet.OfType ()。ToList()的查询,但我认为它不会生成正确的SQL,并将首先获得完整的BusinessObject行然后过滤out内存中的Person实体。所以我猜它会产生巨大的性能影响。

#2


9  

I'm a little late to the party, but I've been having the same questions as you regarding inheritance in EF and found a pretty good summary for you to read. It's an excerpt from Julie Lerman's book Programming Entity Framework.

我参加派对的时间有点晚了,但是我在EF的遗传方面遇到了同样的问题,并为你找了一个很好的总结。这是朱莉·勒曼(Julie Lerman)的“编程实体框架”(Programming Entity Framework)一书的摘录。

After reading it, here's my conclusions on the topic:

阅读之后,这是我对这个主题的结论:

1) In general, what are the pros/cons of inheritance vs extending - The pros really depend on the table strategy you choose. See How to choose an Inheritance Strategy - MSDN Blog. However, they are only "pros" on the assumption that you have already decided to use inheritance at all. And that is not a decision to take lightly.

1)一般来说,继承与扩展的优缺点是什么 - 优点实际上取决于您选择的表策略。请参见如何选择继承策略 - MSDN博客。但是,假设您已经决定使用继承,它们只是“专业人士”。这不是一个轻率的决定。

The cons are many, but the biggest is the inability to add an existing entity into the database as a derived entity. For example: I have a Student which inherits from Person. There is a Person record for John Smith. Some time later I need John Smith to be a Student. Well too bad. That's not possible. (At least not without circumventing EF with a stored procedure).

缺点很多,但最大的是无法将现有实体作为派生实体添加到数据库中。例如:我有一个继承自Person的学生。 John Smith有一个Person记录。一段时间后,我需要约翰史密斯成为一名学生。太糟糕了。那是不可能的。 (至少在没有通过存储过程规避EF的情况下)。

2) In my specific example above, what would be more appropriate? - In your example you should just add those columns (entityId, datecreated, datemodified) to tables that need them. You could use a Complex Type for datecreated and datemodified, but that wouldn't be necessary unless you're a very strict DRY guy. Even then, it might be overkill. The reason is that once you have an entity, you can never add that entity to another derived table. A Person (which is a BaseEntity) can not be added as a Student later. Also, writing LINQ queries would be far more complex than needed. Alex already showed that.

2)在我上面的具体例子中,哪个更合适? - 在您的示例中,您应该将这些列(entityId,datecreated,datemodified)添加到需要它们的表中。您可以使用复杂类型进行datecreated和datemodified,但除非您是一个非常严格的DRY家伙,否则这不是必需的。即便如此,它可能有点矫枉过正。原因是,一旦有了实体,就永远不能将该实体添加到另一个派生表中。一个人(这是一个BaseEntity)以后不能作为学生添加。另外,编写LINQ查询会比需要复杂得多。 Alex已经证明了这一点。

3) If my example is crappy for either or both, what is a good example for using inheritance and for using extending? - Generally, if you can make your base-type abstract, inheritance might work for you. But you should still consider other options first. If your base type will be directly instantiated somewhere, just use composition. Inheritance gets very troublesome when it comes to actually querying and inserting records.

3)如果我的例子对于其中一个或两个都很糟糕,那么使用继承和使用扩展的好例子是什么? - 通常,如果您可以创建基类型抽象,继承可能适合您。但你应该首先考虑其他选择。如果您的基本类型将直接在某处实例化,只需使用合成。在实际查询和插入记录时,继承变得非常麻烦。

In summary, just because you can do inheritance in EF does not mean you should. If you can get away with an inheritance-free design, then by all means do it.

总之,仅仅因为你可以在EF中进行继承并不意味着你应该这样做。如果你能摆脱无继承设计,那么一定要做到。

#3


3  

One disadvantage of using inheritance in your model is that you won't be able to surface your model through WCF Data Services (OData) as it doesn't currently support derived entities.

在模型中使用继承的一个缺点是,您将无法通过WCF数据服务(OData)显示模型,因为它当前不支持派生实体。


推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • 本文介绍了在使用Laravel和sqlsrv连接到SQL Server 2016时,如何在插入查询中使用输出子句,并返回所需的值。同时讨论了使用CreatedOn字段返回最近创建的行的解决方法以及使用Eloquent模型创建后,值正确插入数据库但没有返回uniqueidentifier字段的问题。最后给出了一个示例代码。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • 本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ... [详细]
author-avatar
晨曦dora
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有