<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Oyekunle Oyewusi</title>
    <description>The latest articles on DEV Community by Oyekunle Oyewusi (@sensationalkunlex).</description>
    <link>https://dev.to/sensationalkunlex</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F905089%2F72823fe3-c159-47e6-a3e3-a344ea38425f.jpeg</url>
      <title>DEV Community: Oyekunle Oyewusi</title>
      <link>https://dev.to/sensationalkunlex</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sensationalkunlex"/>
    <language>en</language>
    <item>
      <title>C# Automapper in .NET 6 </title>
      <dc:creator>Oyekunle Oyewusi</dc:creator>
      <pubDate>Sat, 17 Sep 2022 00:04:18 +0000</pubDate>
      <link>https://dev.to/sensationalkunlex/c-automapper-in-net-6-51jh</link>
      <guid>https://dev.to/sensationalkunlex/c-automapper-in-net-6-51jh</guid>
      <description>&lt;p&gt;An automapper is a simple library used to map one object type into another. The library uses reflection mechanism to dynamically bind types and property names together. There are some properties that require manual mapping based on their names and types.&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;Let's get started&lt;br&gt;
&lt;strong&gt;Step 1: Install the library&lt;/strong&gt;&lt;br&gt;
You can add automapper library to your .net project using CLI Or adding it from your nugget package.&lt;br&gt;
CLI command:&lt;br&gt;
&lt;code&gt;&amp;gt; dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection&lt;/code&gt;&lt;br&gt;
Visual studio:&lt;br&gt;
&lt;code&gt;&amp;gt; Install-package AutoMapper.Extensions.Microsoft.DependencyInjection&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Step2: Configuration&lt;/strong&gt; (I)&lt;br&gt;
In .NET 5, you can add Automapper inside your startup.cs. In .NET 6, we don't have startup.cs class by default. You can now add  Automapper library directly in program.cs class&lt;br&gt;
Snippet Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step3: Configuration&lt;/strong&gt; (II) &lt;br&gt;
We need to write a class that inherits from the automapper library's Profile class and include the configuration in the constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public class UserProfile:Profile
    {
       public UserProfile()
        {
         //source mapping to destination
            CreateMap&amp;lt;User, UserDTO&amp;gt;().ForMember(des=&amp;gt;des.UserId, opt=&amp;gt;opt.MapFrom(src=&amp;gt;src.Id));

       } 
    }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 4: &lt;br&gt;
The code implementation, the project starts working from the program.cs file, then moves to the class where Automapper Profile is being inherited before the controller class. The IMapper interface from the library is injected into the controller class and the mapping is done on the method that requires it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserRepo _repository;
        private readonly  IMapper _mapper ;
        public UserController(IUserRepo repository, IMapper mapper)
        {
            _mapper = mapper;
            _repository = repository;
        }
        [HttpGet]
         public ActionResult&amp;lt;IEnumerable&amp;lt;UserDTO&amp;gt;&amp;gt;  GetAllUsers()
         {

          var userLists=_repository.GetAllUsers();
          //Mapping Here Source (User) to Destination
          return Ok(_mapper.Map&amp;lt;IEnumerable&amp;lt;UserDTO&amp;gt;&amp;gt;(userLists) );
          }
      }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The major benefits:&lt;/strong&gt;&lt;br&gt;
As it uses conventions to determine default mappings, it should significantly reduce the amount of code you have to write compared to manual mapping. When the properties of your classes are conventionally aligned (types and names), your mapping configuration will be easy.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n2-SyqaL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxc0l3vs2v04ptuxw8l1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n2-SyqaL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxc0l3vs2v04ptuxw8l1.png" alt="Image description" width="880" height="424"&gt;&lt;/a&gt;&lt;br&gt;
Using a predefined extension method (Reversed()), Automapper can be configured to map in both directions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public class UserProfile:Profile
    {
       public UserProfile()
        {
         //source mapping to destination
            CreateMap&amp;lt;User, UserDTO&amp;gt;().ForMember(des=&amp;gt;des.UserId, opt=&amp;gt;opt.MapFrom(src=&amp;gt;src.Id)).ReverseMap();

       } 
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The Automapper is a useful and straightforward tool for mapping one class to another. It reduces the number of codes and makes clearer codes.&lt;br&gt;
I hope you found the material useful. We'll cover more advanced AutoMapper topics, including the CQRS pattern and microservices, in a future post.&lt;br&gt;
Let's connect &lt;a href="https://www.linkedin.com/in/oyekunle-oyewusi/"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
