Controller with Repository, Ioc and DI

Asked

Viewed 198 times

2

I’m trying to implement the recording of my views, Usuario repository in my controller, even using dependency injection examples, because in my user’s repository constructor it expects to receive an interface from my context.

How can I do in mine controller to access the methods add, savechanges and etc.

My user Repository:

RepositoryBase<Usuario>, IUsuarioRepository
{
    public UsuarioRepository(ILetsPartyContext context)
        : base(context)
    {

    }

}

How I tried to implement but it didn’t work.

public ILetsPartyContext _Context;

   public UsuarioController(ILetsPartyContext Context)
    {
      _Context = Context;
    }


    UsuarioRepository rep = new UsuarioRepository(_Context);

3 answers

1

Ideally, you have a way to solve the dependency of your types. For example, for your type UsuarioRepository is solved, this depends on ILetsPartyContext, which must also be resolved.

Given that you have one container an inversion of control (IoC - acronym of the English inversion of control), you could do your Controller depend on your user repository and the repository depend on this context. For example:

public class UsuarioRepositorio : IUsuarioRepositorio
{
   private readonly ILetsPartyContext _context = null;

   public UsuarioRepositorio(ILetsPartyContext context)
   {
      _context = context;
   }

   // outros métodos...
}

and its controller:

public class UsuarioController : Controller
{
   private readonly IUsuarioRepositorio _usuarioRepositorio = null;

   public UsuarioController(IUsuarioRepositorio usuarioRepositorio)
   {
      _usuarioRepositorio = usuarioRepositorio;
   }

   // outros métodos...
}

Share how you’re doing to resolve these dependencies, what container you’re using, and how you’re doing setup of this.

  • Felipe, in this case I am having doubts about the correct way to inject a depended in my controller so that I can use the insertion methods of my user repositorio and use the savechanges of my context.

0

Felipe, in this case I am having doubts about the correct way to inject a depended in my controller so that I can use the insertion methods of my user repository and use the savechanges of my context. In this case I am used to the use of Positorios without the concept of Ioc and DI, where what I wish to do is:

public class Clientscontroller : Controller

   {
    // Troca o contexto
    //private BancoContexto db = new BancoContexto();
    private readonly ClienteRepositorio repCli = new ClienteRepositorio();}

so that I can:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include="ClienteID,Nome,CNPJ,Endereco,Telefone,Ativo")] Cliente cliente)
    {
        if (ModelState.IsValid)
        {
            //db.Cliente.Add(cliente);
            repCli.Adicionar(cliente);

            //db.SaveChanges();
            repCli.SalvarTodos();
            return RedirectToAction("Index");
        }

        return View(cliente);
    }

0

namespace LetsParty.Infra.Data.Context

{ public class Letspartycontext : Dbcontext, Iletspartycontext { public Letspartycontext() base("Tccbanco") {

    }
    public LetsPartyContext(string connectionString) : base(connectionString)
    {

    }

    public DbContext Context
    {
        get
        {
            return this;
        }
    }

    public void SaveChanges()
    {
        this.SaveChanges();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new UsuarioDbMapping());
        modelBuilder.Configurations.Add(new FornecedorMapping());

    }

    public System.Data.Entity.DbSet<LetsParty.Domain.Model.Atores.Usuario> Usuarios { get; set; }

}

}

Browser other questions tagged

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