
The photo shows one of the 90deg sprays.
class Program
{
static void Main(string[] args)
{
IKernel kernel = new StandardKernel(new RunTimeIoCModule());
IJohnSmith johnSmith = (IJohnSmith)kernel.Get();
johnSmith.PatDog("Rover");
johnSmith.PatDog("Rex");
johnSmith.ChangeDogsName("Rover", "Spike");
IJohnSmith johnSmithAgain = kernel.Get();
johnSmithAgain.PatDog("Rover");
johnSmithAgain.PatDog("Rex");
johnSmithAgain.PullTail("Rex");
System.Threading.Thread.Sleep(3000); // just time to see the output
}
}
public class RunTimeIoCModule : StandardModule
{
public override void Load()
{
Bind().To ();
Bind ().To (); Bind ().To (); Bind ().To ();
}
}
[Transient]
public class Dog : IDog
{
private ITail tail;
private IMouth mouth;
private string name;
[Inject]
public Dog(ITail tail, IMouth mouth)
{
this.tail = tail;
this.tail.SetDog(this);
this.mouth = mouth;
}
public string Name
{
get { return name; }
set { name = value; }
}
public void Pat()
{
Console.WriteLine("Dog '{0}': Pat - thank you", name);
tail.Wag();
}
public void Ouch()
{
Console.WriteLine("Dog '{0}': Ouch!", name);
mouth.Bite();
}
public void PullTail()
{
Console.WriteLine("Dog '{0}': Pulling tail - watch out!", name);
tail.Pull();
}
}
[Transient]
public class Mouth : IMouth
{
public void Bite()
{
Console.WriteLine("Bite!");
}
}
[Transient]
public class Tail : ITail
{
private IDog dog;
public void Wag()
{
Console.WriteLine("Tail Wag");
}
public void Pull()
{
Console.WriteLine("Tail Pull");
dog.Ouch();
}
public void SetDog(IDog dog)
{
this.dog = dog;
}
}
[Singleton]
public class JohnSmith : IJohnSmith
{
private IDog dog1;
private IDog dog2;
[Inject]
public JohnSmith(IDog rover, IDog rex)
{
Console.WriteLine("Hi, my name is John Smith.");
this.dog1 = rover;
this.dog1.Name = "Rover";
this.dog2 = rex;
this.dog2.Name = "Rex";
}
public void ChangeDogsName(string oldName, string newName)
{
IDog dog = GetDog(oldName);
if (dog != null)
{
dog.Name = newName;
}
}
public void PatDog(string name)
{
IDog dog = GetDog(name);
if (dog != null)
{
dog.Pat();
}
}
public void PullTail(string name)
{
IDog dog = GetDog(name);
if (dog != null)
{
dog.PullTail();
}
}
private IDog GetDog(string name)
{
IDog dog;
if (dog1.Name == name)
{
dog = dog1;
}
else if (dog2.Name == name)
{
dog = dog2;
}
else
{
dog = null;
Console.WriteLine("I do not have a dog called '{0}'.", name);
}
return dog;
}
}
Ninject | StructureMap | MicroKernel | Puzzle.Net | Importance | |
License | Apache 2 OSS | essential | |||
C# | 5 | 5 | 4 | ||
Doco | 3 | 4 | X | X | 4 |
XML config | 0 | 4 | 2 | ||
Programmatic config | 5 | 4 | 10 | ||
Constructor injection | 4 | Y | 20 | ||
Method injection | 4 | ? | 4 | ||
Private field injection | 4 | ? | 3 | ||
Setter injection | 4 | 4 | 3 | ||
Singleton activation | 4 | 4 | 10 | ||
Transient activation | 4 | 4 | 10 | ||
Other activation | 4 | 5 | 3 | ||
Simple to use | 4 | ? | 20 | ||
Multiple configs | 4 | 5 | 5 | ||
Contextual binding | 4 | 5 | 4 | ||
Generic types | 3 | 4 | 2 | ||
NMock injection | 3 | 4 | 4 | ||
Maturity | 5 | 4 | |||
SCORE | 58 | 57 | X | X |
Graphics graphics = control.CreateGraphics();Proudly simple (Nigel - I did originally type 'embarrassingly simple', but you are having such an effect on me!).
SizeF size = graphics.MeasureString(text, font);