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.
Looks like I already know how to do this - whoops :)
Ninject - how and when to inject
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With