Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Model using generic types

Tags:

c#

generics

Hi I'm trying to convert one object to another object using generic types, essentially my models have the same properties the only different thing is the namespace

as you can see from below

namespace Models.Entities
{
    public class News 
    {
        //Properties

        public String Title { get; set; }
        public String Teaser { get; set; }
        public String Body { get; set; }
        public String Old_Resource_Id { get; set; }
        public Image Image { get; set; }
        public List<City> Cities { get; set; }
        public Boolean ClientManaged { get; set; }
        public String Contributor { get; set; }
        public Category Category { get; set; }
        public DateTime? Publish { get; set; }
        public Boolean IsEdited { get; set; }
        public DateTime? Date_Edited { get; set; }
        public Boolean IsDeleted { get; set; }
    }
}

namespace Models.Import
{
    public class News 
    {
        //Properties

        public String Title { get; set; }
        public String Teaser { get; set; }
        public String Body { get; set; }
        public String Old_Resource_Id { get; set; }
        public Image Image { get; set; }
        public List<City> Cities { get; set; }
        public Boolean ClientManaged { get; set; }
        public String Contributor { get; set; }
        public Category Category { get; set; }
        public DateTime? Publish { get; set; }
        public Boolean IsEdited { get; set; }
        public DateTime? Date_Edited { get; set; }
        public Boolean IsDeleted { get; set; }
    }
}

I would like to convert from one model to the other but using Generic Types

something like this

 private static List<T2> ConvertModel<T1, T2>(List<T1> items)
    {
        List<T2> convertedList = new List<T2>(); 

          foreach (var item in items)
          {
            convertedList.Add((T2)item);
          }

        return convertedList;
    } 
like image 304
Charles Avatar asked Apr 30 '26 11:04

Charles


2 Answers

I suggest you to use some mapping library, like AutoMapper (available from NuGet) for that:

Mapper.CreateMap<T1, T2>(); // create default mapping
var converted = Mapper.Map<List<T2>>(items); // map lists

Default mapping will do the job in your case, because you have same properties in source and destination types:

Mapper.CreateMap<Models.Entities.News, Models.Import.News>();
like image 191
Sergey Berezovskiy Avatar answered May 03 '26 01:05

Sergey Berezovskiy


You can modify your classes to support implicit cast between them:

namespace Models.Entities
{
    public class News
    {
    //your code

    public static implicit operator Models.Import.News(News value)
    {
        return new Import.News() 
        { 
            Title = value.Title,
            //and so on
        };
    }  
}

namespace Models.Import
{
    public class News
    {
        //your codes
        public static implicit operator Models.Entities.News(News value)
        {
            return new Entities.News() 
            {
                Title = value.Title,
                //and so on 
            };
        }
    }
 }
like image 35
Hossein Narimani Rad Avatar answered May 03 '26 01:05

Hossein Narimani Rad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!