Skip to content

Instantly share code, notes, and snippets.

@actaneon
Created January 26, 2010 17:57
Show Gist options
  • Save actaneon/287046 to your computer and use it in GitHub Desktop.
Save actaneon/287046 to your computer and use it in GitHub Desktop.
Factory Using Closure to Configure at Startup
using StructureMap;
public class Order
{
}
interface IOrderShipper
{
void Ship(Order order);
}
public class OrderProcessor
{
public void Process(Order order)
{
IOrderShipper shipper = new OrderShipperFactory().GetDefault();
shipper.Ship(order);
}
}
public class OrderShipperFactory
{
public static Func<IOrderShipper> CreationClosure;
public IOrderShipper GetDefault()
{
return CreationClosure; //executes closure
}
}
public class GenericFactory
{
public static Func<object> CreationClosure;
public T GetDefault<T>()
{
return (T)CreationClosure; //executes closure
}
}
public class Main
{
private static void ConfigureFactories()
{
OrderShipperFactory.CreationClosure =
() => ObjectFactory.GetInstance<IOrderShipper>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment