2

Excuse me for this lengthy explanation, this to mention all details of my problem.

I am using .NET Core and want to know where is the best place I can put the AutoMapper Configuration? From where should I call it? And what is the best way to inject common services to several ASP.NET Core apps.

Here is the situation right now, I have the following:

  1. Angular2 Application (client side)
  2. Resources API (ASP.NET Core APP for APIs)
  3. Authentication API (ASP.NET Core App for APIs)
  4. Service Layer (Class Library)
  5. Repository Layer (Class Library)
  6. Core (Class Library)

In the repository layer, I have my Entity Framework classes (EntityModels Folder), and I have my implementation of Repositories where I use EF to get the data. The mapping is the happening at this level:

    public User Get(int id)
    {
        using (var securityDb = new SecurityContext())
        {
           var record = securityDb.Users.Where(u => u.UserId == id).SingleOrDefault();

           return AutoMapper.Mapper.Map<User>(record);
        }
    }

In the Core project, I have my domain and dto classes which are returned from Repository layer to the service layer.

Both Services and Repositories project reference the Core Class library.

enter image description here

Now, I should register all the dependencies, this is the "ConfigureServices" Startup Method in Authentication API Project where I am configuring AutoMapper by calling the Mapping class method.

services.AddTransient<SecurityCore.RepositoryContracts.IUserRepository, SecurityCore.Repositories.UserRepository>();
 services.AddTransient<SecurityCore.ServiceContracts.IUserService, SecurityCore.Services.UserService>();

//Automapper configuration
  SecurityCore.Repositories.EntityModels.Mappings.Configure();

I have the mappings class in the repositories project as in the image above:

  public class Mappings
{
    public static void Configure()
    {
        AutoMapper.Mapper.Initialize(cm => cm.CreateMap<EntityModels.Users, Models.User>());
    }
}

This is some of my questions:

  • Should I create a separate project that bootstrap all the mapping configuration, but this means that this project should reference both Core and Repositories project (instead of having the mapping config in Repositories)

  • Should the automapping configuration be made at the ASP.NET Core as in my case?

  • What about injecting dependencies code which is duplicated in both of ASP.NET Core projects, how I can separate the bootstrapping process? Should it be called once in every Application?

1 Answer 1

1

I cannot say that I am an architectural expert and I’m not sure what best practices are concerning how granular our projects should be in the “.NET Core era”.

However, here is a sample project in which I am in the process of porting existing code that implements AutoMapper and Autofac to a new ASP.NET Core MVC solution.

My sample code places AutoMapper mapping interfaces in business logic layer(BLL) libraries which includes the domain models. Implementations of those interfaces as well as configurations(AutoMapper Profiles) are in separate projects in the data access layer (DAL). This arrangement facilitates swapping out the mapping engine without touching the BLL.

The configurations are executed during AutoMapper initialization, which in an ASP.NET Core solution are called via Startup.ConfigureServices().

Please watch this post and my sample code for updates to my AutoMapper and Autofac implementations.

I assume that you are putting your repository interfaces into the same BLL library with your models and implementing those repository interfaces in your DAL. This is what I understand to be a “best practice” and will enable you to change object relational mappers (ORMs) without touching the BLL.

I suggest the following changes to your code:

  • refactor mapping logic from your repositories into a separate project in your DAL (Single Responsibility Principal)
  • configure mappings for each domain model in classes derived from AutoMapper.Profile
  • perform mapping between domain models and data transfer objects (DTOs) in your service classes
  • initialize and configure AutoMapper once, via your startup project

As for dependency injection (DI):

  • DI container is instantiated once via your startup project
  • Services are injected via class constructors

The following articles on DI are worth reviewing:

  • docs.asp.net/en/latest/fundamentals/dependency-injection.html
  • wildermuth.com/2016/08/07/ASP-NET-Core-Dependency-Injection
  • docs.autofac.org/en/latest/integration/aspnetcore.html
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.