Details on the As <> () Method in AutoMapper

I did quite a lot of Googling, and I looked at the AutoMapper wiki, and I cannot find a definition of what the As <> method does in AutoMapper.

I have some guesses. I played a little with him. But I feel that I need to see the documentation.

I think the method name is too common for an efficient search.

If anyone knows where the documents are on this method (or knows that it is not) send it.

+4
source share
2 answers

This is a display redirection. The following test demonstrates this:

 [TestFixture] public class DestinationTypePolymorphismTest { public class Customer { public int Id { get; set; } public string Name { get; set; } } public class CustomerStubDTO { public int Id { get; set; } } public class CustomerDTO : CustomerStubDTO { public string Name { get; set; } } public class Order { public Customer Customer { get; set; } } public class OrderDTO { public CustomerStubDTO Customer { get; set; } } [Test] public void Mapper_Should_Allow_Overriding_Of_Destination_Type() { var order = new Order() { Customer = new Customer() { Id = 1, Name = "A" } }; Mapper.CreateMap<Order, OrderDTO>(); Mapper.CreateMap<Customer, CustomerDTO>(); Mapper.CreateMap<Customer, CustomerStubDTO>().As<CustomerDTO>(); var orderDto = Mapper.Map<Order, OrderDTO>(order); var customerDto = (CustomerDTO)orderDto.Customer; Assert.AreEqual("A", customerDto.Name); Assert.AreEqual(1, customerDto.Id); } } 
+1
source

I still couldn’t find the How-to documentation, but as far as I can tell, this is a redirection of matching.

For example, this code:

 Mapper.CreateMap<SomethingFromWebService, ISomething>().As<Something>(); 

Remedy: For this mapping ( ISomething ), resolve it to As a Something .

Note. This question may provide some context for this answer.

0
source

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


All Articles