top of page

Repository Pattern with AutoMapper in .Net Core


First Base Entity


ree

entity

ree

second generic IRepository

ree

implementation of IRepository

ree

and inject IRepository in ConfigureServices

ree



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

ree

ree


This interface will help us in AutoMapper.

GroupDto

ree

IService Async

ree

Implement IService

ree

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

GroupService that will consume services and repository

ree

and implement IGroupService

ree

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

ree

and add this line in ConfigureServices Method

ree

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.

ree

and add call code in profile

ree

AddExpressionMapping this method to map Expression<Func<>>


Add UniteOfWork

ree

ree

UI Layer


ree

All code will find in GitHub




Source: Medium - Amrelsher


The Tech Platform

Comments


bottom of page