The Tech Platform

Oct 13, 20211 min

Repository Pattern with AutoMapper in .Net Core

First Base Entity


 
entity
 


 
second generic IRepository
 


 
implementation of IRepository
 


 
and inject IRepository in ConfigureServices
 


 

in-service layer


 
install AutoMapper
 

install AutoMapper.Extensions.ExpressionMapping
 

install AutoMapper.Extensions.Microsoft.DependencyInjection
 

install all this package from Nuget Package Manager
 

The first thing is to add base entity DTO
 

This interface will help us in AutoMapper.
 

GroupDto
 


 
IService Async
 


 
Implement IService
 


 
Third, you can make Services for each entity I will make IGroupService and


 
GroupService that will consume services and repository
 


 
and implement IGroupService
 


 
Inject services in ConfigureServcies Method


 
you can write some reflection code to get all services from the current assembly and inject it instead of add each time services
 


 
and add this line in ConfigureServices Method
 


 
don’t forget to add automapper configuration
 

First is Profile I write some reflection code to get all Dto and call the Mapping method this instead of writing all creatMap in profile so code is automatic.
 


 
and add call code in profile
 


 
AddExpressionMapping this method to map Expression<Func<>>

Add UniteOfWork
 

UI Layer


 
All code will find in GitHub

Source: Medium - Amrelsher

The Tech Platform

www.thetechplatform.com

    2