How to use SimpleInjector in a multi-layer project?

3

Imagine the classic architecture:

ConsoleApplication (Frontend) - > Business Layer (BLL) - > Data Access Layer (DAL)

That is, Frontend reference BLL that reference DAL. Note that Frontend does not know the DAL, nor should it.

Given that the record of my container should be in the application's Entry Point (FrontEnd) how can I register a dependency on a DAL repository if Frontend does not know the DAL?

Imagine the following structure:

DAL

// Camada DAL
public class DAOPessoa : IDAOPessoa
{
    public DAOPessoa()
    {

    }

    public List<string> Obter()
    {
        return new List<string>() { "Pessoa 1", "Pessoa 2", "Pessoa 3" };
    }
}

BLL

// CAMADA BLL
public class Processador
{
    private readonly IDAOPessoa _daoPessoa;

    public Processador(IDAOPessoa daoPessoa)
    {
        this._daoPessoa = daoPessoa;
    }

    public void Processar()
    {
        this._daoPessoa.Obter();
    }
}

FRONTEND

//Um console application, por exemplo
public static class Program
{
    static readonly Container container;

    static Program()
    {
        // 1. Create a new Simple Injector container
        container = new Container();

        // 2. Configure the container (register)
        container.Register<IDAOPessoa, DAOPessoa>(); // Aqui fica sublinhado em vermelhor pois este assembly (Frontend) não conheçe nada da DAL


        // 3. Verify your configuration
        container.Verify();
    }
}

Is there any standard or recommendation to solve this problem?

    
asked by anonymous 28.12.2017 / 22:46

2 answers

3

Yes, you should create your IoC as a CrossCutting. That is, a project, DLL, which has records caching, and which sees all projects.

You can see an example here: link

Just add the IoC project to your frontend and request the registration in the container.

In the end, you have your dependency injection container with all the necessary records, your frontend without seeing your DAL

    
29.12.2017 / 09:33
2

Complementing Lunardi's response, and looking from another perspective:

  • Create an Infrastructure project and list the dependencies (this is the "wiring" point of your DI);
  • Create a Core project, have the DTOs and extensions, and Contracts.
  • Create your console app (Frontend) and refer to Infra and Core.
  • Create your BLL depending on Core only.
  • Create your DAL depending on Core only.
29.12.2017 / 09:48