0

I am working on a project that uses Entity Framework Core. I have a class which represents an entity on which I'm trying to perform validation. Is it considered a bad practice to include a Validate method to the class itself (note that the Validate method would not be the only method included)? Is it better to create a repository for the class, and implement validation in the repository? Or to create validation classes per entity? I think there is no "best" approach, but in your opinion, where should the Validation method be implemented? Thank you.

4
  • Be aware that classes used to represent tables in an EF model are deceptively complex; If you attempt to use them in the same way as you'd write code with a regular C# "POCO", your EF classes could churn up a lot of exceptions for really trivial things that would otherwise work just fine on plain C# objects - for example, using a really simple string.Compare in a LINQ Where clause will fail as EF tries to convert that LINQ into raw SQL. Oct 30, 2020 at 10:18
  • 1
    Also, consider looking at the FluentValidation library (fluentvalidation.net), which offers a good pattern for implementing validation rules. Oct 30, 2020 at 10:23
  • I get it. If you could post your comments as an answer, I will accept it.
    – M.R.
    Oct 30, 2020 at 10:42
  • You mention there would be other methods. If you're planning to do DDD style business logic in combination with EF, I highly recommend Vladimir Khorikov's Pluralsight course on that topic
    – Rik D
    Oct 30, 2020 at 11:19

1 Answer 1

4

There is no technical limitation here. EF entity classes can have methods. Those methods do not impact the de/serialization of the entities, so there's no conflict.

That being said, it raises red flags and is generally not a good idea.

For one, your persistence logic and business/domain logic should be separated layers. Since the EF entity belongs to the persistance layer, and validation (or general operations) belong to the business/domain layer, these logically shouldn't find themselves in the same class definition.

Is it better to create a repository for the class, and implement validation in the repository?

EF brings with it some basic data validation, but I would not use that as my business validation, but rather as a last line of defense in case the business logic fails us in some unexpected way.

I would instead validate the DTO which is passed to the business layer, and use that as my main validation logic. When validation passes, the business layer can then talk to the persistence layer and the persistence layer (usually a repository) doesn't need to explicitly validate anything further.

That being said, if you have an application with several data stores, and those stores have different validation rules, there can be value from having the persistence layer do its own round of validation. But that's not the most common scenario. The main business validation should generally be situated in the business layer, not the persistence layer.

Or to create validation classes per entity?

Generally speaking, I do separate my domain models from their validation logic. As far as I'm concerned, these are two separate responsibilities and it's beneficial to keep them separated.

This ensures that validation rules can be altered without impacting the model itself. This can be useful either during development, or if your application has a dynamic validation set that can be altered during runtime (for example, we make applications where the admin end users can set their own validation rules, so we dynamically "pick and mix" our validators).

As per Ben Cottrell's suggestion in the comments, I would also suggest looking at FluentValidation as it helps you in keeping your validation logic separate from the rest of your logic.


I think there is no "best" approach, but in your opinion, where should the Validation method be implemented?

Context is key here. If this is a quick and dirty home project with no long life expectancy, just bang it in wherever it works. I do not apply my own clean coding advice to small tools I hack together once in a while, as the effort does not outweigh the benefit if I don't intend to use the application for a long time.

If this is a professional project or a personal project where long term longevity is a relevant consideration, at the very least keep the persistence and business/domain logic separated.

If this is an enterprise-grade project, I'd advocate keeping the (business/domain) validators separate from the business logic/DTOs themselves to further promote maintainability and loose coupling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.