Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DI with Ninject in a Class Library [duplicate]

I'm successfully using Ninject in my web applications by deriving my Global from NinjectHttpApplication and using the NinjectHttpModule in my web.config

What I want to do now is using DI in one of my class libraries and I don't know how to go about this. I have the following dummy class:

/// <summary>
/// Testing Ninject DI in a class library
/// </summary>
public class Class1
{
    [Inject]
    ICustomerRepository CustomerRepository { get; set; }

    public string SomeText { get; set; }

    public Class1(string text)
    {
        MyConfig config = new MyConfig();
        config.Configure();

        SomeText = text;
    }

    public Customer GetCustomer()
    {
        var customer = CustomerRepository.GetCustomer();
        return customer;
    }
}

public class MyConfig
{
    public IKernel Configure()
    {
        IKernel kernel = new StandardKernel(new NinjectRepositoryModule());
        return kernel;
    }
}

When I instantiate Class1 and call GetCustomer(), the CustomerRepository is null, so I'm obviously doing something wrong.

Also, if I use constructor injection and have my constructor like

public Class1([Inject] ICustomerRepository repository)

how would I go about instantiating Class1?

Quite new to Ninject, so this could all be very easy stuff.

like image 449
Ciaran O'Neill Avatar asked Sep 25 '09 11:09

Ciaran O'Neill


1 Answers

Looks like I already know how to do this - whoops :)

Ninject - how and when to inject

like image 68
Ciaran O'Neill Avatar answered Oct 03 '22 23:10

Ciaran O'Neill