Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 2 validation using DTOs instead of domain entities

I'm struggling to mesh two best practices together:

  1. Using DataAnnotations + ModelBinding for validation in ASP.NET MVC 2
  2. Using DTOs instead of domain entities when passing data via the ViewModel

If I want to pass over DTOs instead of domain entities, then leveraging DataAnnotations + ModelBinding for validation would require me to specify validation attributes on my DTO classes. This results in a lot of duplicated work since multiple DTOs may hold overlapping fields with the same validation restrictions. This means that any time I change a validation rule in my domain, I have to go find all DTOs that correspond with that value and update their validation attributes.

like image 419
Kevin Pang Avatar asked Apr 03 '10 14:04

Kevin Pang


People also ask

Is DTO same as ViewModel?

ViewModel in ASP.NET MVC practice is the same as the DTO, however ViewModel in MVVM pattern is different from DTO because ViewModel in MVVM has behaviors but DTO does not have.

What is DTOs C#?

A DTO is an object that defines how the data will be sent over the network. Let's see how that works with the Book entity. In the Models folder, add two DTO classes: C# Copy.

What is DTO MVC?

A Data Transfer Object (commonly known as a DTO) is usually an instance of a POCO (plain old CLR object) class used as a container to encapsulate data and pass it from one layer of the application to another. You would typically find DTOs being used in the service layer to return data back to the presentation layer.

What are MVC validations?

Validation is an important aspect in ASP.NET MVC applications. It is used to check whether the user input is valid. ASP.NET MVC provides a set of validation that is easy-to-use and at the same time, it is also a powerful way to check for errors and, if necessary, display messages to the user.


3 Answers

You shouldn't have more than one DTO per entity, so you should only have to apply the validation attributes once per DTO. If you need multiple entities for a View, include multiple DTO's as properties of your ViewModel.

like image 181
Dave Swersky Avatar answered Oct 17 '22 18:10

Dave Swersky


You might find useful this.

And keep in mind that validation lives everywhere. There is nothing wrong if DTOs applies UI validation (like getting necessary fields filled, datetime in correct format etc.) and domain objects - domain validation (e.g. account has money before withdrawn operation).

You can't create validation universal. Best thing You can do - put it in appropriate places.

And weed that feeling about duplication out. Usage of DTOs usually means applying single responsibility principle. There is no duplication if you got 2 customer objects where one is responsible for carrying business logic and second that is responsible for displaying it.

like image 33
Arnis Lapsa Avatar answered Oct 17 '22 18:10

Arnis Lapsa


Maybe you could use meta annotations, which puts the attributes on a separate class:

namespace MvcApplication1.Models
{
    [MetadataType(typeof(MovieMetaData))]
    public partial class Movie
    {
    }


    public class MovieMetaData
    {
        [Required]
        public object Title { get; set; }

        [Required]
        [StringLength(5)]
        public object Director { get; set; }


        [DisplayName("Date Released")]
        [Required]
        public object DateReleased { get; set; }
    }
}

Code sample was borrowed from this article.

like image 1
Morten Mertner Avatar answered Oct 17 '22 20:10

Morten Mertner