I can not do dependency injection [duplicate]

2

I'm working with ASP.NET CORE 2 and it's a Layer application.

This is my ConfigureServices no Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<DbContext, ApplicationContext>();
        services.AddTransient<IUsuarioRepository, UsuarioRepository>();
        services.AddTransient<IUsuarioService, UsuarioService>();
       services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

Error appearing when starting the (Updated) Stack:

  

InvalidOperationException: Unable to resolve service for type 'ddd.Infrastructure.Data.ApplicationContext' while attempting to activate 'ddd.Infrastructure.Repositories.UsuarioRepository'.

Follow my UsuarioRepository

public class UsuarioRepository : Repository<Usuario>, IUsuarioRepository
{
    private readonly ApplicationContext _context;

    public UsuarioRepository(ApplicationContext context) : base(context)
    {
        _context = context;
    }

    public bool ValidarUsuarioSenha(Usuario usuario)
    {
        return true;
    }
}

Follow my ApplicationContext

public class ApplicationContext : DbContext
{
    private string strConnection
    ="";

    public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
    {

    }

    public DbSet<Usuario> Usuarios { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseNpgsql(strConnection);
        }
    }
}

* Updated question.

    
asked by anonymous 13.12.2018 / 13:26

3 answers

1

There are two injections left for your code to work correctly, 1st in the Repository and 2nd in your context, your code should look like this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<DbContext, ApplicationContext>();
    services.AddTransient<IUsuarioRepository, UsuarioRepository>();
    services.AddTransient<IUsuarioService, UsuarioService>();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

In your UsuarioRepository and Repository use DbContext instead of ApplicationContext because in dependency injection you are "saying" when you need a DbContext use ApplicationContext

public class UsuarioRepository : Repository<Usuario>, IUsuarioRepository
{
    private readonly DbContext _context;

    public UsuarioRepository(DbContext context) : base(context)
    {
        _context = context;
    }
}

//Sua classe deve estar diferente disso, é apenas um exemplo
public class Repository
{
    private DbContext _context;

    public Repository(DbContext context) : base(context)
    {
        _context = context;
    }
}
    
18.12.2018 / 01:57
0

In the Startup.cs file you also need to add the IUsuarioRepository in dependency injection, your file would look like this:

public void ConfigureServices(IServiceCollection services)
{
       services.AddTransient<IUsuarioRepository, UsuarioRepository>();
       services.AddTransient<IUsuarioService, UsuarioService>();
       services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
    
13.12.2018 / 16:09
0

Your user service receives IUsuarioRepository right, in this case you have to pass

   services.AddTransient<IUsuarioRepository, UsuarioRepository>();
   services.AddSingleton<IUsuarioService, UsuarioService>();

It will pass the UserRepository to the UserService and UserService to the controller. Test it and tell me if it worked ...

    
18.12.2018 / 01:48