1

我有一个带有嵌套视图模型类的复杂结构:

public class ApplicationDriverFormVM : IValidatableObject
{
    [Required]
    public string Sign { get; set; }

    public string Company { get; set; }

    public ApplicationDriverAddressFormVM PresentAddress { get; set; }
    public List<ApplicationDriverAddressFormVM> PreviousAddresses { get; set; }

    public ApplicationDriverLicenseFormVM License { get; set; }

    public ApplicationDriverEducationFormVM Education { get; set; }

    public List<ApplicationDriverAdditionalLicenseFormVM> AdditionalLicenses { get; set; }


    //public ApplicationDriverAccidentFormVM DefaultAccident { get; set; }
    public List<ApplicationDriverAccidentFormVM> Accidents { get; set; }

    public List<ApplicationDriverTrafficConvictionFormVM> TrafficConvictions { get; set; }

    public List<ApplicationDriverEmployerFormVM> Employers { get; set; }


    public ApplicationDriverEquipmentTractorFormVM EquipmentTractor { get; set; }
    public ApplicationDriverEquipmentTrailerFormVM EquipmentTrailer { get; set; }
    public ApplicationDriverEquipmentStraightTruckFormVM EquipmentStraightTruck { get; set; }
    public ApplicationDriverEquipmentCargoVanFormVM EquipmentCargoVan { get; set; }

    public ApplicationDriverFormVM()
    {
        PresentAddress = new ApplicationDriverAddressFormVM();
        PreviousAddresses = new List<ApplicationDriverAddressFormVM>();
        PreviousAddresses.Add(new ApplicationDriverAddressFormVM());
        License = new ApplicationDriverLicenseFormVM();
        Education = new ApplicationDriverEducationFormVM();
        AdditionalLicenses = new List<ApplicationDriverAdditionalLicenseFormVM>();
        AdditionalLicenses.Add(new ApplicationDriverAdditionalLicenseFormVM());
        Accidents = new List<ApplicationDriverAccidentFormVM>();
        Accidents.Add(new ApplicationDriverAccidentFormVM());
        TrafficConvictions = new List<ApplicationDriverTrafficConvictionFormVM>();
        TrafficConvictions.Add(new ApplicationDriverTrafficConvictionFormVM());
        Employers = new List<ApplicationDriverEmployerFormVM>();
        Employers.Add(new ApplicationDriverEmployerFormVM());
        EquipmentTractor = new ApplicationDriverEquipmentTractorFormVM();
        EquipmentTrailer = new ApplicationDriverEquipmentTrailerFormVM();
        EquipmentStraightTruck = new ApplicationDriverEquipmentStraightTruckFormVM();
        EquipmentCargoVan = new ApplicationDriverEquipmentCargoVanFormVM();
    }
}

而且每个嵌套类也可以有嵌套类......

问题是这些类中的许多没有任何必填字段,因此,客户可以将某个类的所有字段留空。结果,控制器得到这样的对象:

在此处输入图像描述

AutoMapper 将此类映射到域(然后是基础架构)类,并且每个实体在数据库中保存为适当表中的不同记录。但我不想保存空记录,我不想将它们映射到域类。怎么做?验证每个对象的所有属性是否为 null 还是有更强大的解决方案?

4

0 回答 0