Problems using a builder

2

I am working on a windows forms application, in which I use ninject, the problem is that in the constructor of my userControl I implement to use my CRUD operations.

public partial class ucProveedor : UserControl, ICommandAction
{
    private ISaProveedor _repositoryProveedor; 
    public ucProveedor(ISaProveedor repositoryProveedor)
    {
        InitializeComponent();
        _repositoryProveedor = repositoryProveedor;
    }
 }

Then when I call the userControl he asks me for a parameter.

private void btnMenuProveedor_Click(object sender, EventArgs e)
{
    ucProveedor _proveedor = new ucProveedor();
    pnlPiso.Controls.Add(_proveedor);
}

What can I do?

    
asked by Pedro Ávila 12.04.2016 в 00:21
source

1 answer

2

If you use Ninject you can not do a new of the instance of ucProveedor, you must solve it using the method Get<>() of IoC container (or ninject) since this is the one who knows how to inject the instance of the constructor.

Dependency Injection in WinForms using Ninject and Entity Framework

You could help with the library

ninject.extensions.infrastructure

The idea is that you can define the

Kernel.Bind<ISaProveedor>().To<ClassConcreta_SaProveedor>();
Kernel.Bind<ucProveedor>().To<ucProveedor>();

Then you can use

ucProveedor _proveedor = kernel.Get<ucProveedor>();

as you get the instance using factory niject, this will assign the instance you need in the constructor

Note: I'm not sure if ninject allows you to map a specific class with your same class, but it's just a matter of trying it.

    
answered by 12.04.2016 / 01:47
source