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:
Which method is best practice when managing business logic on Entity Framework
Have a look at validation with EF - the validation is inside the entities themselves.
It's a very clean way to organise your project.
When you have POCOs, the obvious place for entity validation is in the POCO itself.
It makes sense that any validation of the Customer object is actually in the Customer class.
My experience:
Validate
method which will check that state of the whole entitiy is correct. 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.
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.
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. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With