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

更多相关文章

  1. C#开发微信门户及应用(一)之微信接口的如何使用(图)
  2. .NET中关于接口和类之间的区别介绍
  3. 比较.NET中接口与类的区别
  4. 支付宝的支付接口在.net中的使用
  5. 比较.NET中接口与类
  6. 介绍C#中的接口
  7. c#接口的问题的解决办法详解
  8. C#中的抽象类与接口的详解
  9. 什么是C#接口

随机推荐

  1. 各种类型Android Market了解
  2. android遮罩层
  3. Android for opencv(1)android使用opencv基
  4. 【Android 进阶】Android Home 键监听
  5. Android获取在桌面上有图标的app列表的方
  6. Android Camera 模块分析
  7. android CallCard.java/updateState(Phon
  8. android 连接webservice
  9. android 通过百度地图定位获取坐标在导航
  10. android:layout_gravity和android:gravit