Note that there are some explanatory texts on larger screens.

plurals
  1. POPreferred methods for business entity validation in the enterprise
    primarykey
    data
    text
    <p>Let me preface this question and state that using Entity Framework is not an option for us.</p> <p>In our financial organization we have business entities that are going to be used across solutions. Some have a UI others do not. Validation and business rules must be contained within the entity.</p> <p>I code against a DAL and DTOs that are generated for me and those DALs use procs to operate CRUD on the DB (could be SQL could be Oracle).</p> <p>So as I am creating MVC, WCF, Console apps etc.. the question has been nagging if a better method of validation could be implemented.</p> <p>Here is a couple typical properties in an entity object:</p> <pre><code>[DefaultValue("")] public string Branch { get { return _branch; } set { if (value != null &amp;&amp; value == _branch) return; const string propertyName = "Branch"; ValidationInstance.Clear(propertyName); ValidationInstance.ValidateRequired(propertyName, value); ValidationInstance.ValidateNumeric(propertyName, value); ValidationInstance.ValidateLength(propertyName, value, 2); _branch = value; if (EntityState != EntityStateType.New) EntityState = EntityStateType.Changed; } } [DefaultValue(0)] public decimal HighDefermentMargin { get { return _highDefermentMargin; } set { if (value == _highDefermentMargin) return; const string propertyName = "HighDefermentMargin"; ValidationInstance.Clear(propertyName); ValidationInstance.ValidateRange(propertyName, value); _highDefermentMargin = value; if (EntityState != EntityStateType.New) EntityState = EntityStateType.Changed; } } </code></pre> <p>As you can see there is a mix of data annotations and explicit calls to a validation class to perform increasingly detailed validation.</p> <p>In an MVC app we painstakingly duplicate validation on the ViewModel so we get client side and server side validation. Here is the ViewModel version of the same property from above:</p> <pre><code>[Required] [Range(0.0, 99.99)] [Display(Name = "High Deferment Margin")] public decimal HighDefermentMargin { get; set; } </code></pre> <p>The main difference here is that the validation in the entity loads the errors into an errors collection on the Validation class which can be queried at the time the entity goes to save itself. If(!IsValid) then throw a custom exception that contains the array of errors. The controller loops thru them and adds them to ModelState.</p> <p>I'm starting to work on some classes that have literally a couple hundred fields. Even if they get broken down by OO the number of fields is still very high. These are loan certifications etc that have a lot of data for a single record. Having to write out validation on that many properties makes me want to vomit. I can't just write a utility to generate the entities and validation because business rules are what drive the validation, not the database. Meaning a field may be nullable in the db but not allowed to be persisted as null based on business rules, or the field can be null but only if a separate field has a value, etc..</p> <p>So, can using just the data annotations in the View Model AND the entity the same way achieve the same results? I can write custom validators for the non-standard validation and then business rules for more complex stuff. Will validation errors bethrown up to a higher level from the entity so the UI can inform the user in the same friendly way as ModelState? What are others doing in this same kind of situation?</p>
    singulars
    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.
 

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