Created
April 2, 2015 14:23
-
-
Save smudge202/0c9e9ce0cd92f01c57d9 to your computer and use it in GitHub Desktop.
101 on Basic Factories
This file contains 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
static void Main(string[] args) | |
{ | |
var app = new Voldermort.CommandLineApplication(); | |
app.UseServices(services => | |
{ | |
services.AddTransient<IChildFactory, A>(); | |
services.AddTransient<IChild, B.Child>(); | |
services.AddTransient<IDependencyFactory, C.DependencyFactory>(); | |
}); | |
app.OnExecute<IParent>(parent => | |
{ | |
// Option A (the default): Shows how to create simple objects | |
// Option B: Shows the child being injected into the factory and being reused - handy if the child requires build up and can be resused | |
// Option C: Shows how to instantiate children that have dependencies/require buildup, and can not be resused | |
// Note, with Option C, don't get into the hole of creating factory after factory to complete the buildup. Although | |
// possible, likelihood is a structural change can be made at a much higer level to negate the need to factory build up. | |
var options = new[] { typeof(B), typeof(C), typeof(D) }; | |
foreach (var option in options) | |
{ | |
app.Transition<IChildFactory>(option); | |
parent.Write(); | |
app.ReadKey(); | |
} | |
}); | |
app.Execute(); | |
} | |
private interface IParent | |
{ | |
void Write(); | |
} | |
private class MyParent : IParent | |
{ | |
private readonly IChildFactory _factory; | |
public MyParent(IChildFactory factory) | |
{ | |
_factory = factory; | |
} | |
public void Write() | |
{ | |
_factory.Create().Write(); | |
} | |
} | |
private interface IChild | |
{ | |
void Write(); | |
} | |
private interface IChildFactory | |
{ | |
IChild Create(); | |
} | |
private interface IDependency | |
{ | |
string Get(); | |
} | |
private interface IDependencyFactory | |
{ | |
IDependency Create(); | |
} | |
private class A : IChildFactory | |
{ | |
public IChild Create() | |
{ | |
return new Child(); | |
} | |
private class Child : IChild | |
{ | |
public void Write() | |
{ | |
Console.WriteLine("Child A"); | |
} | |
} | |
} | |
private class B : IChildFactory | |
{ | |
private readonly IChild _child; | |
public B(IChild child) | |
{ | |
_child = child; | |
} | |
public IChild Create() | |
{ | |
return _child; | |
} | |
public class Child : IChild | |
{ | |
public void Write() | |
{ | |
Console.WriteLine("Child B"); | |
} | |
} | |
} | |
private class C : IChildFactory | |
{ | |
private readonly IDependencyFactory _factory; | |
public C(IDependencyFactory factory) | |
{ | |
_factory = factory; | |
} | |
public IChild Create() | |
{ | |
return new Child(_factory.Create()); | |
} | |
private class Child : IChild | |
{ | |
private IDependency _dependency; | |
public Child(IDependency dependency) | |
{ | |
_dependency = dependency; | |
} | |
public void Write() | |
{ | |
Console.WriteLine(_dependency.Get()); | |
} | |
} | |
public class DependencyFactory : IDependencyFactory | |
{ | |
public IDependency Create() | |
{ | |
return new Dependency(); | |
} | |
private class Dependency : IDependency | |
{ | |
public string Get() | |
{ | |
return "How much nesting you waaaan?"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment