1

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.

Lucian Bargaoanu
  • 3,336
  • 3
  • 14
  • 19
  • 2
    Does this answer your question? [Convert DataRow to Object with AutoMapper](https://stackoverflow.com/questions/24665660/convert-datarow-to-object-with-automapper) – Guru Stron Jun 08 '20 at 09:05
  • Thank you, but unfortunately this dint solve my problem; however, i have found something interesting and i will be posting the solution as well. – Soumyashree Pattnaik Jun 09 '20 at 11:47

1 Answers1

0

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.