Install and the first example

1. NuGet Installation Steps

  1. Create a console project (other project types can also be used, the console is only for demonstration).
  2. Search for AutoMapper in the NuGet package manager (author: Jimmy Bogard).
  3. Click Install;Choose the corresponding package version to match the project’s .NET framework version, otherwise the installation will fail.

NuGet Package Manager Console command

```markdown

1. NuGet Installation Steps

  1. Create a console project (other project types can also be used, the console is only for demonstration).
  2. Search for AutoMapper in the NuGet package manager (author: Jimmy Bogard).
  3. Click Install;Choose the corresponding package version to match the project's .NET framework version, otherwise the installation will fail.

NuGet Package Manager Console command

Install-Package AutoMapper

2. Create Entity Classes

class Person
{
    public int Age { set; get; }
    public string Name { set; get; }
    public decimal Salary { set; get; }
    public bool Sex { set; get; }
    public DateTime Birthday { set; get; }
}

class People
{
    public int Age { set; get; }
    public string Name { set; get; }
    public decimal Salary { set; get; }
    public bool Sex { set; get; }
    public DateTime Birthday { set; get; }
}Code language: JavaScript (javascript)

3. Example

class Program
{
    static void Main(string[] args)
    {
        // ① Initialize mapping configuration: Create a mapping relationship from Person to People
        Mapper.Initialize(m => m.CreateMap<Person, People>());

        // ② Instantiate the source object Person
        Person p = new Person()
        {
            Age = 12,
            Name = "Tom",
            Birthday = DateTime.Now,
            Sex = true,
            Salary = 10000
        };

        // The target object cannot be null, it needs to be instantiated first
        People peo = new People();

        // ③ Execute mapping, copy the values from the source object p to peo
        Mapper.Map(p, peo);

        // ④ Read the mapped results
        Console.WriteLine(peo.Name);
        Console.ReadKey();
    }
}Code language: JavaScript (javascript)

Key Points

  1. Mapper.Initialize: Register mapping rules globally. Execute it once when the program starts, do not write it repeatedly in loops.
  2. CreateMap<SourceType, TargetType>(): Declare mapping between two classes.
  3. Mapper.Map(SourceObject, TargetObject): The target object must be instantiated, cannot be null, otherwise an error will be thrown.
  4. Convention rules: If the attribute names and types match, they will be automatically copied without additional configuration.

Version reminder: AutoMapper 10+ no longer recommends using static Mapper.Initialize / Mapper.Map, but instead using DI injection IMapper; the above code is only suitable for learning old versions.

Comparison with Manual Assignment

  • Manual: peo.Name = p.Name; peo.Age = p.Age; ..., the more fields there are, the more boilerplate code there will be.
  • AutoMapper: Only need to declare mapping relationships, automatically copy all attributes with the same name.

Install and the first example

Leave a Reply

Your email address will not be published. Required fields are marked *