AutoMapper Implementation (Post Static Api)

Having some problems with this. It seems that the original design was easy to use, but there are so many ways to implement this in a new design without a static Api. I do not find a single method that would be clear. Many videos and tutorials started to end, but with code syntax that I just don't know.

I just started creating a new application and I want to implement it from the very beginning, if possible.

Can someone give a detailed example of how to implement this from scratch with the next starting point? I understand that the example is so simple that it doesn’t need an automapper

Using MVC w / Code First and EF

Model example

public class Person
{
    public int id { get; set; }
    public string name { get; set; }
    public DateTime created { get;set;
}

ViewModel Example

public class User
{        
    public string name { get; set; }        
}

Controller example - (bit coarse :))

using AutoMapper;
......
[HttpPost]
public ActionResult AddUser(User user)
{

    Person person = new Person();
    person.name = user.name;
    person.created = DateTime.Now;

    db.Persons.Add(person);
    db.SaveChanges();
    return RedirectToAction("Index");
}

I just want to display the name field in this example from ViewModel to Model.

, , .

> https://github.com/AutoMapper/AutoMapper/wiki/Getting-started

  • ? , ?

    Mapper.Initialize(cfg => cfg.CreateMap<Order, OrderDto>());

    Mapper.Initialize(cfg => cfg.CreateMap<User, Person>()); // this is what i did in the controller and Global.asax which isnt valid

  • Action? "" ""?

    OrderDto dto = Mapper.Map<OrderDto>(order);

  • " AutoMapper"

    - Global.asax , "anyIWant", ? , ( ) Global.asax "Application_Start()"?

? ? .. . ...

> https://www.youtube.com/watch?v=-5sZ7hq3J10 2:46, , . , , , .

, , , !


, ...

  • App_Start. MappingConfig.cs
  • , ".App_Start" MappingConfig.cs.
  • MappingConfig , .

    :

    public static void RegisterMaps() { AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<User, Person>()); }

: . , ? , . "" , , . github .

  1. -ActionResult :

    var person = Mapper.Map<Person>(user);

    EDIT: " ".

  2. Global.asax "Application_Start()", . :

    protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); MappingConfig.RegisterMaps(); // Right here }

, , ? , , , , .

+4
2

, , , , *, , . Github Getting-Started...:/

Mapper.Initialize(cfg => cfg.CreateMap<Source, Dest>();
Mapper.Initialize(cfg => cfg.CreateMap<Dest, Source>();

Mapper.Initialize(cfg => cfg.CreateMap<Source, Dest>().ReverseMap();

, , , ( global.asax), config .

, , , , , . , , Automapper: " ." , : " ". , , , .

, , . , - . Jimmy, , . 3 , - AutoMapper , . ManualMapper.

0

Glabal.asax, . :

  Mapper.Initialize(config =>
            {
               config.CreateMap<User, Person>().ReverseMap();                  
            });

ReverseMap() , .

name User Person.

Conrtoller Mapper :

User userViewModel = Mapper.Map<User>(person);

Mapper.Map .

+1

Source: https://habr.com/ru/post/1659359/


All Articles