Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataAnnotations vs IDataErrorInfo

DataAnnotations vs IDataErrorInfo

Pros and Cons of both? Benefits of one over the other? (especially related to MVC)

like image 977
Martin Avatar asked Jan 20 '10 18:01

Martin


People also ask

What are DataAnnotations in net core?

Data Annotations attributes are . NET attributes which can be applied on an entity class or properties to override default conventions in EF 6 and EF Core. Data annotation attributes are included in the System. ComponentModel.

What is DataAnnotations MVC?

DataAnnotations is used to configure your model classes, which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC, which allows these applications to leverage the same annotations for client-side validations.

What is System ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.


2 Answers

Late entry to the discussion as I do not want to start a new question. Where I am coming from is to determine the best practice to apply to a medium size ASP.NET MVC project.

Let me first summarise our options :-

1) IDataErrorInfo is simple to implement. All you need is to derive IDataErrorInfo in your Model class. The catch is that you are letting your model binding to be enforcing your business rules. Business rules should be enforced by the Model. The other catch for IDataErrorInfo (and likewise for DataAnnotations) is that (paraphrasing from Steven Sanderson's book it could not report multiple errors relating to a single property or multiple errors relating to the whle object model.

2) DataAnnotation to me, is like a schema check (validation). This is the first check that your application should do. However (IMHO), it is not suited to implement your business rules.

3) Implement your own ModelBinder. Although this can be done but seriously speaking, the usage of ModelBinder is to parse and bind your data to your model and not to perform complicated validations and business rule checks. I would leave the Business rules check to be implemented in your Model/Domain layer.

4) Roll your own - Validating with a service layer (see this. The example shown has its advantage of decoupling from the Controller and Model State using an interface class. Another option is to throw an appropriate exception from your model layer. The latter option is useful when you implement your service layer in a separate application (e.g a WCF application).

What do you think? For a medium to large size project, which of the above options have you used (or intend to adopt) and why?

Cheers

like image 65
Syd Avatar answered Nov 08 '22 04:11

Syd


Looks like DataAnnotations are getting official support in MVC 2.0. Scott Guthrie published a good article on doing model validation in 2.0 using DataAnnotations. Given that the team seems headed this direction, you might consider that a vote in its favor.

like image 30
tvanfosson Avatar answered Nov 08 '22 03:11

tvanfosson