-2

I have an app which uses a dataset. eg.

public class mainData
{
        public string itemID { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string address { get; set; }
}

I have 4 other data sets which are imported API data from other apps which have the 'same' data (eg same type of fiends) but use different names. eg,

public class importedData1
{
        public string itemsID { get; set; }
        public string fName { get; set; }
        public string lName { get; set; }
        public string add { get; set; }
}

Each of the 4 sets is different.

What I'm doing at the moment is:

  • Importing data from the API of the new data (eg. importedData1)/

  • Created a function to convert that data into the mainData class model by doing a foreach loop on each of the records in the imported data.

      public static mainData ConvertData1(importedData1 item)
      {
         mainData sxData = new mainData ()
         {
           itemID = item.itemsID,
           firstName = item.fname,
           lastName = item.lname,
           address = item.add};
    
    
           return sxData;
           }
    

(apologies, the indentation is having issues in stack) I then have 4 of these functions, 1 for each of the imported data sets.

Is this an 'ok' way to achieve this?
if it was just a single set of data to another I was going to use {get;set} within the main Model however with all 4 I thought this would start getting very mixed up as there are some crossovers in naming convention but with different object types!

ASh
  • 34,632
  • 9
  • 60
  • 82
Glenn Angel
  • 381
  • 1
  • 3
  • 14
  • 2
    Generally "is this OK" are off-topic as opinion-based (as long as code does what you want)... but luckily we already have plenty of duplicates how to use AutoMapper (and one is even typed out here for you with copy-paste ready code). – Alexei Levenkov Apr 19 '22 at 00:41
  • FYI - the standard naming convention in C# has class and public properties/fields as PascalCase, not camelCase. – Enigmativity Apr 19 '22 at 01:23
  • @Enigmativity thanks Enigma, Ill look into that too. – Glenn Angel Apr 19 '22 at 03:17
  • @AlexeiLevenkov ty, I didnt really even know the terminology of Automapper but sounds great. – Glenn Angel Apr 19 '22 at 03:17

1 Answers1

1

One of the best approaches is to use AutoMapper. It is an open-source project and you can effortlessly search, download and use it.

Now assume you have the following classes and want to map between:

public class mainData
{
    public string itemID { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string address { get; set; }
}

public class importedData1
{
    public string itemsID { get; set; }
    public string fName { get; set; }
    public string lName { get; set; }
    public string add { get; set; }
}

Now, because your classes use different property names, we need to use the fluent builder of AutoMapper to map your fields. it's a little tricky but a life saver:

var config = new MapperConfiguration(cfg =>
    cfg.CreateMap<importedData1, mainData>()
        .ForMember(x => x.itemID, y => y.MapFrom(x => x.itemsID))
        .ForMember(x => x.firstName, y => y.MapFrom(x => x.fName))
        .ForMember(x => x.lastName, y => y.MapFrom(x => x.lName))
        .ForMember(x => x.address, y => y.MapFrom(x => x.add))
        .ReverseMap()
);

Now you can easily map your two classes as like as following example:

var mapper = new Mapper(config);
var recDto = mapper.Map<mainData>(impDto);

Console.WriteLine($"{recDto.firstName} {recDto.lastName}");
Console.WriteLine(recDto.GetType().Name); // Its maindata

Output:

Output Console

Reza Heidari
  • 1,192
  • 2
  • 18
  • 23