实体框架4,继承VS延伸?实体、框架、VS

2023-09-03 09:13:12 作者:与我无关〆

有什么优势/劣势每种方法?

What are the advantages/disadvantages to each method?

我知道我在任何一本书或在本网站上的某个地方读取为什么使用表继承是糟糕的实体框架4。

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

例如,为什么不能做一个表,有一个entityId,dateCreated会,datemodified然后让所有其他类中继承,在实体框架?然后我的表的所有其他实体并不需要有那些列。然后,我可以有一个人的类继承的基类,然后特定的人继承的人。

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.

我不知道本作的优点,虽然除了写一个小的SQL脚本生成数据库...

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:

那支对我来说最重要的事情是,我不希望我的数据库依靠我的应用程序的当前继承结构的事实。如果,无论出于何种原因,我想改变的设计我在继承方面的应用,它不会有问题,因为我的数据不会依赖于我目前的制度设计,我认为数据存储的只是 - 。存储数据库是按照正确的数据库设计的标准化,但也传承是应用程序开发,而不是数据存储程序上的选择。单独将prevent我使用它,继承是一个非常困难的事情正确,当涉及到设计一个应用程序,它更容易改变应用程序$ C $温度比它是改变和迁移数据库数据 当大多数不太老练的开发者接近一个问题,他们接近它继承。我确实做了,当我第一次开始发展。它在逻辑上是有道理的。然而,一旦制定了很长一段时间,你知道代表团真的去(服务调用SOA的情况下,服务)和单一用途的服务提供了很多比继承更多的重用的最佳方式。

"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."

这也是情理之中的我。

于是

1)一般情况下,什么是继承VS延伸的优点/缺点 2)在我的上述具体的例子,这将是比较合适? 3)如果我的例子是糟糕的一个或两个,什么是一个很好的例子,使用继承和使用扩展?

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?

我都过,但因为我远离经验丰富用过,我还是不知道该如何处理所有情况。

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

10票,8收藏,百余意见,没有人能扩大? =(。

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

推荐答案

我已经试过类似的事情为你描述,我看,主要的问题是,你无法创建对象集< T> 的派生类型。所以你不会有对象集<人> 存储库。 如果您源于例如BusinessObject的实体的所有实体,你就可以只用对象集&LT工作; BusinessObject的> 。如果您想查询唯一的人库,您可以编写 Context.BusinessObjectSet.OfType&LT查询;人>()了ToList()但我认为它不会产生正确的SQL引擎盖下,将首先得到充分BusinessObject的行,然后过滤掉人单位在内存中。所以我想这将有巨大的性能影响。

I have tried similar thing as you describe, and the main problem I see, is that u can't create ObjectSet<T> for derived type. So you will not have ObjectSet<Person> repository. If you derive all your Entities from for example BusinessObject entity, you will be able to work only with ObjectSet<BusinessObject>. If you want to query only Person repository, you can write query like Context.BusinessObjectSet.OfType<Person>().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.