How to use Simpleinjector in a multi-layer project?

Asked

Viewed 262 times

3

Imagine the classic architecture:

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

That is, Frontend references BLL that references DAL. See that Frontend does not know the DAL, and nor should.

Since my container registration should be in the application’s Entry Point (Frontend) how I can register a dependency of a DAL layer repository if Frontend does not know 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?

2 answers

3


  • A good approach too, so I understood then create a class library responsible for IOC referencing everyone that is necessary? and his "Frontend" calls this DLL and it does the job?

  • 1

    That’s right. So you have a unique responsibility project.

  • Very good, very good... From the beginning I imagined that this would be the ideal solution, I asked the question just to confirm and also because I saw several post arguing that the frontend itself could reference everything, in these posts people argued that reference the DAL DLL, For example, it does not necessarily create dependence between objects, this is true, but it is dangerous. The solution you suggest is much more elegant and safe.

2

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

  • Create an Infrastructure project and relate dependencies (this is the "Wiring" point of your DI);
  • Create a Core project, has Dtos and extensions, and Contracts.
  • Create your app console (Frontend) and refer to Infra and Core.
  • Create your BLL depending only on Core.
  • Create your DAL depending only on Core.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.