Multiple Repository constructors using Unity Application Block C#dependency injection

Asked

Viewed 83 times

0

I have a project that uses Unity Application Block dependency injection.

Has the repository class:

public class RepositorioDeLaudoMedico: RepositorioGenericoEntityFramework<LaudoMedico, int>,
        IRepositorioDeLaudoMedico
{

    public IDbConnection Conexao;
    public RepositorioDeLaudoMedico(DbContext contexto) : base(contexto){} //construtor 1
    public RepositorioDeLaudoMedico(IDbConnection conexao) //construtor 2
    {
        this.Conexao = conexao;
    }
}

And in the dependency injection class it’s like this:

container.RegisterType<IRepositorioDeLaudoMedico, RepositorioDeLaudoMedico>(
        new InjectionConstructor(
             new ResolvedParameter(typeof(DbContext), "ContextoLocal")
        )
);

How do I include the second constructor? I can’t touch the first because it comes from a dll...

  • Do you intend to remove this and insert the other one? What is the purpose of the exchange?

  • i know that in dependency injection the ideal is to use only 1 constructor...

  • because I need to create another access to the bank using Dapper

  • 1

    Okay, if you have that principle noted, imagine that two settings for the same record will not work, do you understand that? If you need to use dapper the contexto gives you also the class that is responsible to access the methods of Dapper, IE, does not need to be two settings, because it no longer works, only one and access the extension methods that Dapper provides in the namespace that has to be configured.

  • 1

    EF Core: https://stackoverflow.com/questions/41935752/entity-framework-core-how-to-get-the-connection-from-the-dbcontext

  • 1

    Entity Frameword <=6: https://stackoverflow.com/questions/5286071/entity-framework-getting-an-sql-connection

  • 1

    These two ways propose to you and rescue IDbConnection! understands?

  • 1

    @Rafaelamarraschi this was an old recommendation. nowadays the injection stack chooses the constructor with the largest number of injections it can solve.

  • Guys, thank you, I was able to solve, really with this variable I was able to catch the connection.

Show 4 more comments

1 answer

0

yeah, I got the connection like this:

DbContext contexto;  IDbConnection Conexao =((IDbConnection)this.Contexto.Database.Connection);

Browser other questions tagged

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