Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created September 12, 2012 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akimboyko/3709484 to your computer and use it in GitHub Desktop.
Save akimboyko/3709484 to your computer and use it in GitHub Desktop.
ConstructorArgument example
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