简体   繁体   English

实现业务逻辑验证的最佳实践 - 实体框架

[英]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之前添加业务逻辑,这里是我想到的选项:

  1. Implement business logic on the DataContext level - by overriding SaveChanges method 通过重写SaveChanges方法在DataContext级别实现业务逻辑
  2. Implement business logic for each entity using OnPropertyChanging partial method 使用OnPropertyChanging部分方法为每个实体实现业务逻辑
  3. Wrap the generated code in a custom class that implement the validation layer. 将生成的代码包装在实现验证层的自定义类中。

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: 我的经验:

  1. This works but it is quite lot of work and in case of many entities which must be validated this can be slower. 这是有效的,但它是相当多的工作,并且在许多实体必须经过验证的情况下,这可能会更慢。 Actually EFv4.1 does this automatically. 实际上EFv4.1会自动执行此操作。
  2. I don't like this way - it serves only single property changes and doesn't work for complex validation where you need to modify several properties before you get a valid state. 我不喜欢这种方式 - 它只提供单个属性更改,并且不适用于需要在获得有效状态之前修改多个属性的复杂验证。
  3. Perhaps - I like validation on demands. 也许 - 我喜欢验证要求。 Each entity can expose 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)和抽象层中,抽象层通常是我的应用程序中的服务层。

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.

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