简体   繁体   中英

How to use AutoMapper without Entity Framework?

I am learning how to use 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.

Below code may help you get more insight of this:

Response 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. 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.

I tried doing something like this:

I created another class which has the properties with different names:

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.

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. 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

 [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

 [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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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