zoukankan      html  css  js  c++  java
  • StructureMap经典的IoC/DI容器

    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<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();
            }
        }
    

    文档:http://structuremap.github.io/documentation/
    下载:https://sourceforge.net/projects/structuremap/
    GitHub:https://github.com/structuremap/structuremap

  • 相关阅读:
    document.compatMode的CSS1compat
    js自运行函数
    Sublime Text 3 入门(插件控制台安装)
    javascript 面向对象技术
    jQuery ui 中文日历
    js给数字加三位一逗号间隔的两种方法(面试题)
    android eclipse集成环境
    中科红旗倒下,谁来挑战windows
    在网站制作中随时可用的10个 HTML5 代码片段
    IE6/IE7中li底部4px的Bug
  • 原文地址:https://www.cnblogs.com/yangsofter/p/StructureMap.html
Copyright © 2011-2022 走看看