Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ninject Kernel with multiple Modules
void Main()
{
var kernel = new Ninject.StandardKernel(new EntitieseModule(), new DocumentStoreModule(), new ModelRepositoryModule());
}
public interface IModelRepository<T> where T: IModel
{
//interface stuff here
}
public interface IDocumentStore
{
}
public class DocumentStore : IDocumentStore
{
}
public interface IModel
{
}
public class User : IModel
{
}
public class UserRepository : IModelRepository<User>
{
public UserRepository(IDocumentStore documentStore, string databaseName)
{
//constructor code here
}
}
public class ModelRepositoryModule : NinjectModule
{
public override void Load()
{
string databaseName = "SomeName";
Bind<IModelRepository<User>>()
.To<UserRepository>()
.WithConstructorArgument("documentStore", Kernel.Get<IDocumentStore>())
.WithConstructorArgument("databaseName", databaseName);
}
}
public class DocumentStoreModule : NinjectModule
{
public override void Load()
{
Bind<IDocumentStore>()
.To<DocumentStore>();
}
}
public class EntitieseModule : NinjectModule
{
public override void Load()
{
Bind<User>()
.ToSelf();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment