akimboyko

Ninject Kernel with multiple Modules

Aug 19th, 2012
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. void Main()
  2. {
  3.     var kernel = new Ninject.StandardKernel(new EntitieseModule(), new DocumentStoreModule(), new ModelRepositoryModule());
  4. }
  5.  
  6. public interface IModelRepository<T> where T: IModel
  7. {
  8.     //interface stuff here
  9. }
  10.  
  11. public interface IDocumentStore
  12. {
  13. }
  14.  
  15. public class DocumentStore : IDocumentStore
  16. {
  17. }
  18.  
  19. public interface IModel
  20. {
  21. }
  22.  
  23. public class User : IModel
  24. {
  25. }
  26.  
  27. public class UserRepository : IModelRepository<User>
  28. {
  29.     public UserRepository(IDocumentStore documentStore, string databaseName)
  30.     {
  31.         //constructor code here
  32.     }
  33. }
  34.  
  35. public class ModelRepositoryModule : NinjectModule
  36. {
  37.     public override void Load()
  38.     {
  39.         string databaseName = "SomeName";
  40.  
  41.         Bind<IModelRepository<User>>()
  42.             .To<UserRepository>()
  43.             .WithConstructorArgument("documentStore", Kernel.Get<IDocumentStore>())
  44.             .WithConstructorArgument("databaseName", databaseName);
  45.     }
  46. }
  47.  
  48. public class DocumentStoreModule : NinjectModule
  49. {
  50.     public override void Load()
  51.     {
  52.         Bind<IDocumentStore>()
  53.             .To<DocumentStore>();
  54.     }
  55. }
  56.  
  57. public class EntitieseModule : NinjectModule
  58. {
  59.     public override void Load()
  60.     {
  61.         Bind<User>()
  62.             .ToSelf();
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment