记一次利用AutoMapper优化项目中数据层到业务层的数据传递过程。

时间:2023-03-08 19:32:09

目前项目中获取到DataSet数据后用下面这种方式复制数据。

    List<AgreementDoc> list = new List<AgreementDoc>();
    ].Rows)
                 {
                     AgreementDoc docItem = new AgreementDoc() {
                         ContactID = row["ContactID"].ToString(),
                         AgreementAccepted = row["AgreementAccepted"].ToString(),
                         AgreementSource = row["AgreementSource"].ToString(),
                         AgreementStatus = row["AgreementStatus"].ToString(),
                         AlternateFirstName = row["AlternateFirstName"].ToString(),
                         AlternateLastName = row["AlternateLastName"].ToString(),
                         City = row["City"].ToString(),
                         RecruiterID = row["RecruiterID"].ToString(),
                         ConsultantID = row["ConsultantID"].ToString(),
                         ConsultantStatus = row["ConsultantStatus"].ToString(),
                         ConsultantSuffix = row["ConsultantSuffix"].ToString(),
                         Director = row["Director"].ToString(),
                         EligibleToLogin = row["EligibleToLogin"].ToString(),
                         EmailAddress = row["EmailAddress"].ToString(),
                         FirstName = row["FirstName"].ToString(),
                         GovernmentIssuedId = row["GovernmentIssuedId"].ToString(),
                         Inserted = row["Inserted"].ToString(),
                         IsReceived = row["IsReceived"].ToString(),
                         LastName = row["LastName"].ToString(),
                         MiddleName = row["MiddleName"].ToString(),
                         PhoneNumber = row["PhoneNumber"].ToString(),
                         Reason = row["Reason"].ToString(),
                         RecruiterContactID = row["RecruiterContactID"].ToString(),
                         RecruiterIndicator = row["RecruiterIndicator"].ToString(),
                         RecruiterName = row["RecruiterName"].ToString(),
                         RegistrationProvince = row["RegistrationProvince"].ToString(),
                         ReleaseAuthorizationIndicator = row["ReleaseAuthorizationIndicator"].ToString(),
                         ResidenceID = row["ResidenceID"].ToString(),
                         StartDate = row["StartDate"].ToString(),
                         StreetAddress = row["StreetAddress"].ToString(),
                         UnitID = row["UnitID"].ToString(),
                         UnitSuffix = row["UnitSuffix"].ToString(),
                         RecuriterFirstName = row["RecuriterFirstName"].ToString(),
                         RecuriterLastName = row["RecuriterLastName"].ToString(),
                         RecuriterMiddleName = row["RecuriterMiddleName"].ToString()
                     };
                     list.Add(docItem);
                 }

探索反射解决方案:

对于目标实体中字段与数据库中字段数据类型不一样时,果断抛出异常。—失败告终....

探索AutoMapper中dynamicMap映射IDataReader到实体时同样会抛出异常,其实它的原理就是反射—失败告终...

探索AutoMapper针对列名属性单一映射方法ForMember();—成功优化。

对于某些需要转化格式的数据做特别处理。

优化后的code:

           AutoMapper.Mapper.CreateMap<IDataReader, AgreementDoc>().ForMember(des => des.ContactID, opt =>
                 {
                     opt.MapFrom(src => (src.GetInt64(src.GetOrdinal("ContactID")).ToString()));
                 }).ForMember(des => des.StartDate, opt =>
                 {
                     opt.MapFrom(src => (src.GetDateTime(src.GetOrdinal("StartDate")).ToString()));
                 });
              List<AgreementDoc> list = AutoMapper.Mapper.Map<IDataReader,List<AgreementDoc>>(ds.CreateDataReader());