Controller with repository, Ioc and DI

2

I'm trying to implement the recording of the data in my views, and I'm having doubts about how to instantiate my% repository% in my controller, even using dependency injection examples, because in builder of my user repository he expects to receive an interface from my context.

How can I do in my controller to get access to Usuario , add , and so on.

My User Repository:

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

    }

}

How I tried to implement however did not work.

public ILetsPartyContext _Context;

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


    UsuarioRepository rep = new UsuarioRepository(_Context);
    
asked by anonymous 02.09.2015 / 21:58

3 answers

1

Ideally, you have a way of resolving dependency on your types. For example, in order for its UsuarioRepository type to be resolved, it depends on ILetsPartyContext , which must also be resolved.

Given that you have a% change of% control% ( container ), you could make your IoC depend on your user repository, and the repository will 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 your 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, which container you're using, and how you're doing% with% of it.

    
03.09.2015 / 14:15
0

Felipe, in case I'm having doubts about the correct way to inject a dependency into my controller so that I can use my user's input methods and use the context savechanges.  In case I am used to the use of repositories without the concept of IoC and DI, where what I want 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);
    }
    
08.09.2015 / 16:48
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; }

}

}

    
08.09.2015 / 18:44