0
votes

I am writing some Web API application, where I have 4 basic layers - API, BusinessLogic(which I call BusinessServices), DAL (which using EF to speak with the database), and EntitiesData(where I have my entities).

API calls businessService, bs ask DAL, DAL using EF is asking database about my EntitiesData.

Ok, now what's the problem ;)

On the BusinessServices, I want to map entities to some DTO, which I can return to API. I wanted to use AutoMapper, but on tutorials, there are really simple examples, which I understand.

The first question: Should I use 2 IoC containers? Or maybe move my IOC from API to the business services layer? 1st Container is on API level and it contains BusinessServices (like UsesrsService, MessageService, etc.) The second container would be at BusinessServices level - I want to use it to store my AutoMapper maps.

And this is the second question - what should I do with AutoMapper. I know, how to create the configuration, did sth like this:

private void Congifure()
{
    if(!(configuration == null))
        return;

    var config = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<User, UserDto>();
        cfg.CreateMap<Message,MessageDto>();
    });
}

but what should I do now? pack it to the IoC container? From which place in the code I should call my class which is configuring mapper? In businessServices I have only my business-logic classes and DTO's.

2
I would avoid using automapper for a Web Api applicaton, it is really slow. Something like Omu value injecter it's much faster and easier to use. - Isma
The title of your question makes no sense? What static class? - Storm Muller

2 Answers

0
votes

You can pack it into your Startup.cs ConfigureServices method:

var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<User, UserDto>();
                cfg.CreateMap<Message,MessageDto>();
            });
var mapper = config.CreateMapper();
services.AddScoped<AutoMapper.IMapper>(c => mapper);

And than inject it into your classes:

public class MyService
{
    public MyService(IMapper mapper)
    {
        ...
    }
}

I would use one mapper, and put it somewhere vertically to your layer like into "helpers" project. Your mapper has to map between different layers so it should sit "between" them. Just move the logic of creation of MapperConfiguration into separate project and call it from your Startup.cs.

0
votes

The very first thing in the automapper docs speaks about initialization. This should be done where ever you are bootstrapping your IOC container.

You only need 1 IOC container, making 2 would kind of make them useless as you would have broken the dependency tree into 2 halves.

And you should consider using mapping profiles for your different layers.