简体   繁体   English

实体框架模型可重用吗?

[英]Are Entity Framework Models Reusable?

How reusable are the results of using EF ? 使用EF的结果有多可重用?

Currently, I use stored procedures for 100% of my data access. 目前,我使用stored procedures进行100%的数据访问。 The main reason I may be looking to do things differently for my newest project is for maintainability: adding an attribute to a table means manually altering dozens of stored procedures. 我可能希望为最新项目做不同的事情的主要原因是出于可维护性:向表中添加属性意味着手动更改数十个存储过程。 If I understand EF correctly, I should be able to add an attribute to an Entity in my EF model, and then ask EF to update my CRUD methods for me. 如果我正确理解EF ,则应该能够在EF模型中为实体添加属性,然后要求EF为我更新CRUD方法。 awesome. 太棒了

However, there is one thing holding me back: reusability. 但是,有一件事阻止了我:可重用性。 I like that I can make the SP's for a given database once, and be done with them; 我喜欢这样,我可以为给定的数据库制作一次SP,然后对它们进行处理。 I can make 12 applications that all use that database and getting that data will be as easy as calling the correct SP. 我可以制作12个都使用该数据库并获取数据的应用程序,就像调用正确的SP一样容易。

  • Will the same be true if I switch to a more EF-centric approach? 如果我改用以EF为中心的方法,情况会一样吗?

  • Can I import an Existing EF Data Model and have it work without too much trouble? 我可以导入现有的EF数据模型并使它正常工作吗?

  • Can I make it so that I alter the Data Model once, and that change is felt across all applications? 我可以这样做,以便一次更改数据模型,并且可以在所有应用程序中感受到这种变化吗?

Ad1. 广告1。 You can easily reuse complex EF queries if you follow the Repository pattern. 如果遵循存储库模式,则可以轻松地重用复杂的EF查询。 Repositories are where you encapsulate your data access and, yes, repositories are easily reused between different modules/applications. 存储库是您封装数据访问的地方,是的,存储库很容易在不同的模块/应用程序之间重用。

(not that you can't reuse code without Repositories! Repositories are just a common way of doing it for data access layer) (并非没有存储库就无法重用代码!存储库只是在数据访问层进行编码的一种常见方式)

Ad2. 广告2。 I am not sure what you mean by "import existing EF model" (import where?) but usually EF models are straightforward and can be reused 我不确定“导入现有的EF模型”(在哪里导入?)是什么意思,但是通常EF模型很简单,可以重用

Ad3. 广告3。 Just have your model in a separate assembly. 只需将模型放在单独的装配中。

A real benefit to using EF is getting away from stored procedures. 使用EF真正好处是摆脱了存储过程。 The problem that exists with using stored procedures for all your data access is that you are forced to put business logic into your data layer . 使用存储过程进行所有数据访问时存在的问题是,您被迫将业务逻辑放入数据层

Check out Should the data access layer contain business logic? 签出数据访问层是否应包含业务逻辑? While its not true in every case, generally keeping your business logic in your business layer gives you better separation of concerns. 尽管并非在所有情况下都是如此,但通常将业务逻辑保留在业务层中可以使您更好地分离关注点。

I have an EF project that I use as the data layer for several applications. 我有一个EF项目,用作多个应用程序的数据层。 This allows me to change it once and have all the other projects get the benefits. 这使我可以更改一次,并让所有其他项目受益。 Granted, sometimes supporting code needs to be changed in these other projects, but you'd have that same problem in a stored procedure model as well. 当然,在其他项目中有时需要更改支持代码,但是在存储过程模型中也会遇到相同的问题。

Any N-tier design would solve this problem by simply creating a class (in a class library) that understands how to access the data by using entity framework. 任何N层设计都可以通过简单地创建一个类(在类库中)来解决此问题,该类了解如何使用实体框架访问数据。 Then any applications that want to access the data uses the class library, configure a connection string in the app.config or web.config and you're done. 然后,所有想要访问数据的应用程序都使用类库,在app.config或web.config中配置连接字符串,您就完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM