這篇文章主要為大家展示了“StructureMap怎么用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“StructureMap怎么用”這篇文章吧。
成都創(chuàng)新互聯(lián)公司長(zhǎng)期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為大新企業(yè)提供專(zhuān)業(yè)的成都網(wǎng)站制作、網(wǎng)站建設(shè),大新網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
StructureMap是一款很老的IoC/DI容器,從2004年.NET 1.1支持至今。
//創(chuàng)建業(yè)務(wù)接口 public interface IDispatchService { } public interface ICourier { } public interface IPaymentGateway { } public interface IPaymentMerchant { } //接口的實(shí)現(xiàn) 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(); } } //業(yè)務(wù)使用 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()); } } //配置依賴(lài)關(guān)系 public class BootStrapper { public static void ConfigureStructureMap() { ObjectFactory.Initialize(x => x.AddRegistry()); } } public class ModelRegistry : Registry { public ModelRegistry() { For ().Use (); For ().Use (); For ().Use (); For ().Use (); } } class Program { static void Main(string[] args) { BootStrapper.ConfigureStructureMap();//啟用配置 OrderService orderService = ObjectFactory.GetInstance (); Console.WriteLine(orderService.ToString()); IPaymentGateway paymentGateway= ObjectFactory.GetInstance (); Console.WriteLine(paymentGateway); Console.ReadKey(); } }
以上是“StructureMap怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!