locked
Data Annotations to apply to a DTO with in a DTO RRS feed

  • Question

  • So in my MVVM based N-tier project, I am passing DTOs from my service layer to the UI (Silverlight) layer. For validation purpose (using DataAnnotations), I am defining Model classes for each of these DTOs as below:

    public class CourseModel
    {
        CourseDTO course;
        DateTime commenceDate;
        int maxStudents;
    
        public CourseDTO Course
        {
            get
            {
                return course;
            }
            set
            {
                course = value;
                RaisePropertyChangedNotification(() => Course);
            }
        }
        [Required]
        public DateTime CommenceDate
        {
            get
            {
                return course.CommenceDate;
            }
            set
            {
                course.CommenceDate = value;
                RaisePropertyChangedNotification(() => CommenceDate);
            }
        }
    
        [Range(0, 5000)]
        public int MaxStudents
        {
            get
            {
                return course.MaxStudents;
            }
            set
            {
                course.MaxStudents = value;
                RaisePropertyChangedNotification(() => MaxStudents);
            }
        }
    }

    I bind the above model class to different View elements.

    But the problem is when I have for e.g. a list of 'SubjectDTO's in 'CourseDTO'. I can create a SubjectModel class similar to CourseModel as above, have a property in CourseModel to have a list of SubjectModel objects and iterate through each of the Subject DTOs and populate the SubjectModel list.

    But this really gets very long and complicated.

    So is this the right approach? Can someone suggest how shall I better manage the inner DTO validation?

    Sunday, September 4, 2011 7:28 PM

Answers

  • My collegue suggested me with an easy solution: use Auto Mapper :)

    Monday, September 5, 2011 10:24 PM

All replies

  • My collegue suggested me with an easy solution: use Auto Mapper :)

    Monday, September 5, 2011 10:24 PM
  • Hi,

    You can use Partial class or create DomainModel(Refactoring Model).

    public class Class1
        {

            [StringLength(50, ErrorMessageResourceName = "VehicleModelStringLengthValidationError", ErrorMessageResourceType = typeof(Resources))]      
            [Display(Name = "VehicleModelLabelText")]
            [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ValidationErrorResources))]
            [RegularExpression("^.*[^a-zA-Z0-9].*$", ErrorMessageResourceName = "ValidationErrorBadPasswordStrength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
            [StringLength(50, MinimumLength = 7, ErrorMessageResourceName = "ValidationErrorBadPasswordLength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
            public string ModelName
            {
                get { return this.g.Location; }
            }

            private Entity g { get; set; }// this line from server part.
        }

    Tuesday, September 6, 2011 5:52 AM
  • Thanks again thaicarrot,

    However, I am doing sort of similar thing in my code. My issue was with complex DTOs that can have other DTO in them.

    I will try AutoMapper to solve it.

    Thanks,

    Tuesday, September 6, 2011 7:01 AM