StructureMap怎么用-创新互联
                                            这篇文章主要为大家展示了“StructureMap怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“StructureMap怎么用”这篇文章吧。

StructureMap是一款很老的IoC/DI容器,从2004年.NET 1.1支持至今。
一个使用例子
    //创建业务接口
    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());
        }
    }
    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怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联成都网站设计公司行业资讯频道!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享文章:StructureMap怎么用-创新互联
文章链接:http://www.scyingshan.cn/article/dsojep.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 