public
interface
IDispatchService { }
public
interface
ICourier { }
public
interface
IPaymentGateway { }
public
interface
IPaymentMerchant { }
public
class
DispacthService : IDispatchService
{
private
ICourier _courier;
public
DispacthService(ICourier courier)
{
_courier = courier;
}
public
override string ToString()
{
return
_courier.ToString();
}
}
public
class
FedExCourier : ICourier { }
public
class
StreamLinePaymentMerchant : IPaymentMerchant { }
public
class
PaymentGateway : IPaymentGateway
{
private
IPaymentMerchant _paymentMerchant;
public
PaymentGateway(IPaymentMerchant paymentMerchant)
{
_paymentMerchant = paymentMerchant;
}
public
override string ToString()
{
return
_paymentMerchant.ToString();
}
}
public
class
OrderService
{
private
IPaymentGateway _paymentGateway;
private
IDispatchService _dispacthService;
public
OrderService(IPaymentGateway paymentGateway, IDispatchService dispacthService)
{
_paymentGateway = paymentGateway;
_dispacthService = dispacthService;
}
public
override string ToString()
{
return
string.Format(
"IPaymentGateway:{0} IDispatchService:{1}"
, _paymentGateway.ToString(), _dispacthService.ToString());
}
}
public
class
BootStrapper
{
public
static
void ConfigureStructureMap()
{
ObjectFactory.Initialize(x => x.AddRegistry<ModelRegistry>());
}
}
public
class
ModelRegistry : Registry
{
public
ModelRegistry()
{
For<IPaymentGateway>().Use<PaymentGateway>();
For<IPaymentMerchant>().Use<StreamLinePaymentMerchant>();
For<IDispatchService>().Use<DispacthService>();
For<ICourier>().Use<FedExCourier>();
}
}
class
Program
{
static
void Main(string[] args)
{
BootStrapper.ConfigureStructureMap();
OrderService orderService = ObjectFactory.GetInstance<OrderService>();
Console.WriteLine(orderService.ToString());
IPaymentGateway paymentGateway= ObjectFactory.GetInstance<PaymentGateway>();
Console.WriteLine(paymentGateway);
Console.ReadKey();
}
}