Skip to content

Instantly share code, notes, and snippets.

@PatrickDinh
Created August 8, 2016 07:37
Show Gist options
  • Save PatrickDinh/2719c6744280ba77cb19532c5f728406 to your computer and use it in GitHub Desktop.
Save PatrickDinh/2719c6744280ba77cb19532c5f728406 to your computer and use it in GitHub Desktop.
Test autofac factory
void Main()
{
var builder = new ContainerBuilder();
builder.RegisterType<Foo>().As<IFoo>();
builder.RegisterType<TrueBar>().AsSelf();
builder.RegisterType<FalseBar>().AsSelf();
builder.RegisterType<DefaultBar>().AsSelf();
var container = builder.Build();
container.Resolve<TrueBar>().Show();
container.Resolve<FalseBar>().Show();
container.Resolve<DefaultBar>().Show();
}
// Define other methods and classes here
public interface IFoo
{
void Show();
}
public class Foo : IFoo
{
private bool _correct;
public Foo(bool correct)
{
_correct = correct;
}
public Foo()
{
Console.WriteLine("Default Foo");
}
public void Show()
{
Console.WriteLine(_correct);
}
}
public class TrueBar
{
private IFoo _foo;
public TrueBar(Func<bool, IFoo> fooFactory)
{
_foo = fooFactory(true);
}
public void Show()
{
_foo.Show();
}
}
public class FalseBar
{
private IFoo _foo;
public FalseBar(Func<bool, IFoo> fooFactory)
{
_foo = fooFactory(false);
}
public void Show()
{
_foo.Show();
}
}
public class DefaultBar
{
private IFoo _foo;
public DefaultBar(IFoo foo)
{
_foo = foo;
}
public void Show()
{
_foo.Show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment