Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I am working on three tier architecture in MVC with Business Objects layer, Service layer, web layer. business objects layer contains DTO(data transfer object). service layer process the DTO and contains business logic. one of my DTO is like below:

C#
public class InsertCompanyGroupDto
{
    public InsertCompanyGroupDto()
    {
        this.CompanyGroupAddressDetails = new InsertAddressDto();
    }
    public InsertAddressDto CompanyGroupAddressDetails { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "The {0} is required.")]
    [StringLength(100, ErrorMessage = "The {0} can not exceed {1} characters long.")]
    public string CompanyGroupName { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "The {0} is required.")]
    [StringLength(200, ErrorMessage = "The {0} can not exceed {1} characters long.")]
    public string SiteUrl { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "The {0} is required.")]
    [StringLength(20, ErrorMessage = "The {0} can not exceed {1} characters long.")]
    public string UserNameTag { get; set; }
}

in this code, I am using InsertAddressDto inside the Insert Company Group Dto. both have validations. please look at the Insert Address DTO.

C#
public class InsertAddressDto
{
    [Min(1, ErrorMessage = "{0} should be minimum of {1}.")]
    [Required(ErrorMessage = "The {0} is required.")]
    public short AddressTypeId { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "The {0} is required.")]
    [StringLength(100, ErrorMessage = "The {0} can not exceed {1} characters long.")]
    public string Address1 { get; set; }

    [StringLength(100, ErrorMessage = "The {0} can not exceed {1} characters long.")]
    public string Address2 { get; set; }
}


from the above code, I am passing only one request as Json which contains
C#
{ "CompanyGroupName":"CG5", "SiteUrl":"http://localhost:33570/MultiChoice5", "UserNameTag":"Aker1", "SourceCode":"Test22", "PageFooter":"test22", "CreatedById":"1", "AddressTypeId":"6", "Address1":"test 1", "Town":"testt", "Postcode":"24234", "CountryId":"1", "CreatedById":"1" }


For validation,I am using following code inside DTO.

C#
public IEnumerable<ValidationResult> Validate()
    {
        return Validate(new ValidationContext(this));
    }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var results = new List<ValidationResult>();
        Validator.TryValidateObject(this, validationContext, results, true);

        return results;
    }


I am using this code in both DTOs(company group and address) validation only works in InsertCompanyGRoupDTO not works in InsertAddressDTO. How shall i rectify this? I need to know how to validate two dtos(one within another) at the same time?

Regards,
Lalitha
Posted
Comments
Sinisa Hajnal 16-Jun-15 4:05am    
Write a method IsInputValid or just IsValid or something in which you will call the address validator explicitly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900