PetShop动态加载Pattern
加载Order strategy,见Order class
1. Interface
public interface IOrderStrategy
{
void Insert(PetShop.Model.OrderInfo order);
}
2. Concrete class
public class OrderAsynchronous : IOrderStrategy
{
private static readonly PetShop.IMessaging.IOrder asynchOrder = PetShop.MessagingFactory.QueueAccess.CreateOrder();
public void Insert(PetShop.Model.OrderInfo order) {
asynchOrder.Send(order);
}
}
3.Factory
private static PetShop.IBLLStrategy.IOrderStrategy LoadInsertStrategy()
{
// Look up which strategy to use from config file
string path = ConfigurationManager.AppSettings[”OrderStrategyAssembly”];
string className = ConfigurationManager.AppSettings[”OrderStrategyClass”];
// Using the evidence given in the config file load the appropriate assembly and class
return (PetShop.IBLLStrategy.IOrderStrategy)Assembly.Load(path).CreateInstance(className);
}
4.web.Config
<add key=”OrderStrategyClass” value=”PetShop.BLL.OrderSynchronous”/>
