I set up “Ninject” in my asp.mvc project. And it works fine each controller get its dependency classes. But I have one class in mvc project that is not controller. It’s a simple class that extends “MembershipProvider” (because I have made custom membership) and I need to inject “UserRepository” class in it.
In a NinjectControlelrFactory I bint it:
private void AddBindings()
{
ninjectKernel.Bind<IUserRepository>().To<UserRepository>().WithConstructorArgument(
"connectionString", ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
}
But how to get it from non controller class?
PS
I can’t inject through constructor.
I have some solution but I don’t know how ‘clean’ it is:
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<IUserRepository>()
.To<UserRepository>()
.WithConstructorArgument("connectionString", "ttttttttttttt");
//var tc = kernel.Get<IUserRepository>();
this.userRepository = kernel.Get<IUserRepository>();
}
Use Property Injection. Register your
MembershipProviderin the Ninject and use Property injection.You will need to instantiate
MembershipProvidervia ninject context.Check these articles.
Property Injection in ASP.NET MVC with Ninject
Injecting properties in Ninject 2 without ‘Inject’ attribute