[英]Best practice to implement business logic validation - Entity Framework
I'm using Entity Framework for the first time, and I need to add business logic before inserting new objects into the db, here are the options I thought about: 我是第一次使用Entity Framework,我需要在将新对象插入db之前添加业务逻辑,这里是我想到的选项:
Which method is best practice when managing business logic on Entity Framework 在Entity Framework上管理业务逻辑时,哪种方法是最佳实践
Have a look at validation with EF - the validation is inside the entities themselves. 看看EF验证 - 验证是在实体本身内部。
It's a very clean way to organise your project. 这是组织项目的一种非常干净的方式。
When you have POCO s, the obvious place for entity validation is in the POCO itself. 当你有POCO时 ,实体验证的明显位置在POCO本身。
It makes sense that any validation of the Customer object is actually in the Customer class. 有意义的是,Customer对象的任何验证实际上都在Customer类中。
My experience: 我的经验:
Validate
method which will check that state of the whole entitiy is correct. Validate
方法,该方法将检查整个权利的状态是否正确。 But all this works only if you always use the whole entity. 但只有当你总是使用整个实体时,这一切才有效。 Once you start to use partial updates and other features you will still have to handle validation elsewhere.
一旦开始使用部分更新和其他功能,您仍然需要在其他地方处理验证。 That is another +1 for validation on demand.
这是另一个+1按需验证。
I prefer a version of number 3. I like to abstract Entity Framework anyways using a repository or something similar, in case I want/need to replace EF in the future. 我更喜欢3号版本。我喜欢使用存储库或类似的东西抽象实体框架,以防我希望/将来需要替换EF。
Then for validation/business logic, I use whatever validation techniques make sense for the application, but usually some combination of DataAnnotations (for UI minimum validation) and a validation framework like Fluent Validation for maximum validation/business rules. 然后,对于验证/业务逻辑,我使用对应用程序有意义的任何验证技术,但通常是DataAnnotations (用于UI最小验证)和验证框架(如Fluent验证)的一些组合,以获得最大验证/业务规则。 This validation/business logic lives in both the entity class (DataAnnotations) and in an abstraction layer, which is usually a service layer in my applications.
此验证/业务逻辑同时存在于实体类(DataAnnotations)和抽象层中,抽象层通常是我的应用程序中的服务层。
Article from Visual Studio Magazine addressing the issue: 来自Visual Studio Magazine的文章解决了这个问题:
http://visualstudiomagazine.com/articles/2012/04/01/integrating-validation-with-the-entity-framework.aspx http://visualstudiomagazine.com/articles/2012/04/01/integrating-validation-with-the-entity-framework.aspx
Maybe your answer stands in thoses lines ;) 也许你的答案代表着一条线;)
Best practice entity validation in ASP.NET MVC & ADO.NET Entity Framework ASP.NET MVC和ADO.NET实体框架中的最佳实践实体验证
Another way to consider is to fully componentize out your data access layer from your business logic layer completely. 另一种考虑方法是完全从业务逻辑层完全组件化您的数据访问层。
Create a data access interface that only accesses Entity Framework directly, then in a seperate project I would create your business logic classes which interface with the data access layer through the interface. 创建一个只能直接访问Entity Framework的数据访问接口,然后在一个单独的项目中,我将创建业务逻辑类,通过接口与数据访问层连接。 No Entity Framework referernces in your business logic project.
您的业务逻辑项目中没有实体框架引用。
In this way the layers are componentized and easier to distribute as multiple assemblies for either two-tier or three-tier access. 通过这种方式,这些层被组件化并且更容易分发为用于两层或三层访问的多个组件。
也许试着阅读规范模式
You can allways extend your classes by creating another partial class definition, most EF templates define the Entities as partial definitions just for people to easly extend them. 您可以通过创建另一个部分类定义来扩展您的类,大多数EF模板将实体定义为部分定义,仅供人们轻松扩展它们。 You will want to do this if you're working with WPF or Silverlight as most things are not bound directly, you either have a boolean and want to convert that into a color, etc. Writing converters is slow and requires a lot more code to setup then just creating new Getters on your BusinessObjects.
如果你正在使用WPF或Silverlight,你会想要这样做,因为大多数东西都没有直接绑定,你要么有一个布尔值,要把它转换为颜色等等。编写转换器很慢,需要更多代码然后设置只需在BusinessObjects上创建新的Getter。
We have been using EF 4.0 STE (Self Tracking Entities) for a while now, and we extend most of them with our own partial definitions. 我们已经使用EF 4.0 STE(自我追踪实体)一段时间了,我们用它们自己的部分定义扩展它们中的大部分。 We changed a bit of the T4 template that creates the STEs to allow access to constructor on the custom partial class definition and other small improvements.
我们改变了一些创建STE的T4模板,以允许访问自定义部分类定义的构造函数和其他小改进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.