Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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