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:
- Angular2 Application (client side)
- Resources API (ASP.NET Core APP for APIs)
- Authentication API (ASP.NET Core App for APIs)
- Service Layer (Class Library)
- Repository Layer (Class Library)
- 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.
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?
