Advertisement
akimboyko

ConstructorArgument example

Aug 27th, 2012
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. void Main()
  2. {
  3.     using(var kernel = new StandardKernel(new ExampleKernel()))
  4.     {
  5.         var d6 = kernel.Get<Die>();
  6.         var d20 = kernel.Get<Die>(new ConstructorArgument("numSides", 20));
  7.        
  8.         d6.NumSides.Dump();
  9.         d20.NumSides.Dump();
  10.     }
  11. }
  12.  
  13. public interface IRandomProvider
  14. {
  15.     int GetRandom(int lower, int upper);
  16. }
  17.  
  18. public class RandomProvider : IRandomProvider
  19. {
  20.     private Random _random = new Random();
  21.  
  22.     public int GetRandom(int lower, int upper)
  23.     {
  24.         return lower + _random.Next(upper - lower);
  25.     }
  26. }
  27.  
  28. public class Die
  29. {
  30.     public int NumSides { get; private set; }
  31.     public IRandomProvider Provider { get; private set; }
  32.  
  33.     public Die(int numSides, IRandomProvider provider)
  34.     {
  35.         NumSides = numSides;
  36.         Provider = provider;
  37.     }
  38. }
  39.  
  40. public class ExampleKernel : NinjectModule
  41. {
  42.     public override void Load()
  43.     {
  44.         Bind<IRandomProvider>()
  45.             .To<RandomProvider>();
  46.            
  47.         Bind<Die>()
  48.             .ToSelf()
  49.             .WithConstructorArgument("numSides", 6);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement