简体   繁体   English

如何在没有实体框架的情况下使用 AutoMapper?

[英]How to use AutoMapper without Entity Framework?

I am learning how to use AutoMapper.我正在学习如何使用 AutoMapper。 First thing first, I don't use Entity Framework to read my data.首先,我不使用实体框架来读取我的数据。

Hence, in my case I have to do manual mapping for each of the properties of my response model.因此,就我而言,我必须为响应 model 的每个属性进行手动映射。

Below code may help you get more insight of this:下面的代码可以帮助您更深入地了解这一点:

Response model:响应 model:

 public class TotalLossResults
 {
     public string N_CLAIM_NUMBER { get; set; }
     public string N_CLAIM_ID { get; set; }             
 }

MapperClass:映射器类:

 public class TLResultsMapper : Profile
 {
     private TotalLossResults tlResultsObj = new TotalLossResults();

     public TLResultsMapper()
     {
         IMappingExpression<DataRow, TotalLossResults> mappingExpression = CreateMap<DataRow, TotalLossResults>();

         foreach (var prop in tlResultsObj.GetType().GetProperties())
         {
             mappingExpression.ForMember(prop.Name, y => y.MapFrom(s => s[prop.Name]));
         }
     }
 }

Note: in the mapper class I used for each to get rid of the mappingExpression.ForMember statement for each property.注意:在映射器 class 中,我使用了每个属性来摆脱每个属性的mappingExpression.ForMember语句。 But this works only when the property name is the same as of the column name (entity name for example) of the result which I get from the database.但这仅在属性名称与我从数据库获得的结果的列名称(例如实体名称)相同时才有效。

I am looking out for some option where I can take similar approach to map the data values to my response model properties when the property's names are not matching with the column names.我正在寻找一些选项,当属性名称与列名称不匹配时,我可以对 map 数据值对我的响应 model 属性采取类似的方法。

I tried doing something like this:我试着做这样的事情:

I created another class which has the properties with different names:我创建了另一个 class ,它具有不同名称的属性:

public class TLResultsDifferentNames
{
    public string N_CLAIM_NUMBER { get; set; }
    public string N_CLAIM_ID { get; set; }
}

and a mapper implementation like this:和这样的映射器实现:

private TLResultsDifferentNames tlResultsObj = new TLResultsDifferentNames ();
private TotalLossResults tlResultsColObj = new TotalLossResults ();*

for (int i = 0, j = 0; i<tlResultsObj.GetType().GetProperties().Length - 1 && j<tlResultsColObj.GetType().GetProperties().Length - 1; i++, j++)
{
    mappingExpression.ForMember(tlResultsObj.GetType().GetProperties()[i].Name, y => y.MapFrom(s => s[tlResultsColObj.GetType().GetProperties()[j].Name]));
}

But this doesn't work.但这不起作用。 It binds the last column values to all the model properties.它将最后一列的值绑定到所有 model 属性。

Any help/suggestion to achieve the mapping without using the manual way of mapping would be very helpful.在不使用手动映射方式的情况下实现映射的任何帮助/建议都会非常有帮助。

I could find something really interesting in Auto Mapper today.今天我可以在 Auto Mapper 中找到一些非常有趣的东西。 Which is Attribute Mapping and using that i need not to worry about any sort of manual/dynamical mapping for my models.这是属性映射,使用它我不必担心我的模型的任何类型的手动/动态映射。

Below is the code which works perfectly now for all the properties:下面是现在对所有属性都能完美运行的代码:

Ex1: here all the properties' names are same Ex1:这里所有属性的名称都相同

 [AutoMap(typeof(object))] //this takes our Source class name
    public class TotalLossResults
     {
         public string N_CLAIM_NUMBER { get; set; }
         public string N_CLAIM_ID { get; set; }             
     }

Ex2: here we got different properties Ex2:在这里我们得到了不同的属性

 [AutoMap(typeof(TotalLossResults))] //this takes our Source class name
 public class TLResultsDifferentNames
{
    [SourceMember(nameof(TotalLossResults.N_CLAIM_NUMBER))]
    public string claimNumberOfJack { get; set; }
    public string claimIDofJack { get; set; }
}

For mapping configuration we gonna use the below code:对于映射配置,我们将使用以下代码:

var config1 = new MapperConfiguration(cfg => 
cfg.AddMaps(typeof(TotalLossResults))); 
var mapper = new Mapper(config1);     

var response = mapper.Map<TotalLossResults>(sourceObject);

Note: Its better to have the configs created in App Start.注意:最好在 App Start 中创建配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM