Created
September 12, 2012 20:01
-
-
Save akimboyko/3709484 to your computer and use it in GitHub Desktop.
ConstructorArgument example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
using(var kernel = new StandardKernel(new ExampleKernel())) | |
{ | |
var d6 = kernel.Get<Die>(); | |
var d20 = kernel.Get<Die>(new ConstructorArgument("numSides", 20)); | |
d6.NumSides.Dump(); | |
d20.NumSides.Dump(); | |
} | |
} | |
public interface IRandomProvider | |
{ | |
int GetRandom(int lower, int upper); | |
} | |
public class RandomProvider : IRandomProvider | |
{ | |
private Random _random = new Random(); | |
public int GetRandom(int lower, int upper) | |
{ | |
return lower + _random.Next(upper - lower); | |
} | |
} | |
public class Die | |
{ | |
public int NumSides { get; private set; } | |
public IRandomProvider Provider { get; private set; } | |
public Die(int numSides, IRandomProvider provider) | |
{ | |
NumSides = numSides; | |
Provider = provider; | |
} | |
} | |
public class ExampleKernel : NinjectModule | |
{ | |
public override void Load() | |
{ | |
Bind<IRandomProvider>() | |
.To<RandomProvider>(); | |
Bind<Die>() | |
.ToSelf() | |
.WithConstructorArgument("numSides", 6); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment