Mapping items from one class to another if Items are equivalent

前端 未结 4 1330
鱼传尺愫
鱼传尺愫 2021-01-16 02:53

Say I have one class that looks like this:

public class Person
{
     public string Name {get; set;}
     public int Number {get; set;}
}


        
相关标签:
4条回答
  • 2021-01-16 03:08

    Install AutoMapper package in your project.

    As a best practice (for web applications) you can create new class (should derives from Profile) in your App_Start folder, that will contain all your mappings for your project.

    namespace MyApp.App_Start
    {
        public class MyAppMapping : Profile
        {
            public MyAppMapping()
            {
    
                CreateMap<Person, Dog>();
    
                //You can also create a reverse mapping
                CreateMap<Dog, Person>();
    
                /*You can also map claculated value for your destination. 
                Example: you want to append "d-" before the value that will be
                mapped to Name property of the dog*/
    
                CreateMap<Person, Dog>()
                .ForMember(d => d.Days, 
                 conf => conf.ResolveUsing(AppendDogName)); 
            }
    
            private static object AppendDogName(Person person)
            {
                return "d-" + person.Name;
            }
        }
    }
    

    Then Initialize your mapping inside the Application_Start method in Global.asax

    protected void Application_Start()
    {
        Mapper.Initialize(m => m.AddProfile<MyAppMapping>());
    }
    

    You can now use the mappings that you have created

    var dog = AutoMapper.Mapper.Map<Person, Dog>(person);
    
    0 讨论(0)
  • 2021-01-16 03:11

    If you don't work with big generic list, you can do it using LinQ.

    var persons = new List<Person>();
    // populate data [...]
    var dogs = persons.Select(p=>new Dog{Name=p.Name,Number=p.Number}).ToList();
    

    It's easy to remember, and you can filter data previously.

    0 讨论(0)
  • 2021-01-16 03:26

    You can use AutoMapper:

    public Dog UsingAMR(Person prs)
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Person, Dog>();
        });
        IMapper mapper = config.CreateMapper();
        return mapper.Map<Person, Dog>(prs);
    }
    

    Then you can easily:

    Person ps = new Person {Name = "John", Number = 25};
    Dog dog = UsingAMR(ps);
    

    Just don't forget to install AutoMapper first from the package manager console as mentioned in the reference:

    1. From Tools menu click on NuGet Package Manager ==> Package Manager Console
    2. Then type the following command:

      PM> Install-Package AutoMapper
      
    0 讨论(0)
  • 2021-01-16 03:30

    An object oriented approach.

    public class Mammal
    {
        public Mammal(Mammal toCopy)
        {
            Name = toCopy.Name;
            Number = toCopy.Number;
        }
        public string Name {get; set;}
        public int Number {get; set;}
    }
    public class Person: Mammal
    {
        public Person(Mammal toCopy) {} /* will default to base constructor */
    }
    
    public class Dog: Mammal
    {
        public Dog(Mammal toCopy) {} /* will default to base constructor */
    }
    

    This will allow the following:

    Person person = new Person();
    person.Name = "George";
    person.Number = 1;
    Dog dog = new Dog(person);
    
    0 讨论(0)
提交回复
热议问题