Note that there are some explanatory texts on larger screens.

plurals
  1. POis this a good idea to implement validation in entity framework POCO entities in dabtase first?
    primarykey
    data
    text
    <p>It`s seems that the best place to implement validation is as close as possible to the database, so when I use entity framework the nearest objects are the entities, in my case the POCO entities.</p> <p>The reason for that is that if I want to reuse this POCO entities, the validation is implemented in the POCO objects and then there are less posibilities to insert worng data in the database.</p> <p>this also avoid that someone try to insert incorrect data in the databse creating another application, or because he does not implement the validation. So it is more secure.</p> <p>One way to do that is using partial classes that extends the POCO entities and that implements the IValidatableObject interface and return a list of validationresult.</p> <p>But other way is the following. I have a common assembly that has the following:</p> <ul> <li>One interface that declare the methods that need to implement the repositories.</li> <li>The POCO entities that will be used by the repositories.</li> <li>One class with utilities, such as copy entities and methods to validate the data of the entities.</li> </ul> <p>Then I can create many repositories that use different versions of EF or another technology and all of them use the common assembly. This repositories implements the validation using the methods in the common library.</p> <p>In this case I implement the validation only once. The only problem is that the repositories need to call the methods to validate the data.</p> <p>But there are advantages in this way, from my point of view. For example, I can validate the data of the entities depending on the type of the operation. For example, if I am adding a new record and the primary key as an autonumeric, if the ID is not 0, then I can throw an exception, or if I try to delete a register when the ID is 0, then I don't need to send the command to the database.</p> <p>So this second solution solves the problem to implement the validation as close as possible to the database, bacause is used in the repository, that is the element that access to the database, but has the problem that if some developer creates a new repository and not use the validation methods, I can have incorrect data in the database.</p> <p>So my question is if the best option is to use validation with partial classes or to use a common library and the validation is implemented in the repositories, that is really what the users will use.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload