首页后端开发ASP.NET经典的IoC/DI容器--StructureMap

经典的IoC/DI容器--StructureMap

时间2024-01-30 06:55:03发布访客分类ASP.NET浏览756
导读:收集整理的这篇文章主要介绍了经典的IoC/DI容器--StructureMap,觉得挺不错的,现在分享给大家,也给大家做个参考。StructureMap是一款很老的IoC/DI容器,从2004年.NET 1.1支持至今。一个使用例子...
收集整理的这篇文章主要介绍了经典的IoC/DI容器--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.AddRegistrymodelRegistry>
    ());
        }
    }
    public class ModelRegistry : Registry    {
        public ModelRegistry()        {
                ForIPaymentGateway>
    ().UsePaymentGateway>
    ();
                ForIPaymentMerchant>
    ().UseStreaMLinePaymentMerchant>
    ();
                ForIDispatchService>
    ().UseDispacthService>
    ();
                ForICourier>
    ().UseFedExCourier>
    ();
        }
    }
    class Program    {
        static void Main(string[] args)        {
                BootStrapper.ConfigureStructureMap();
    //启用配置            OrderService orderService = ObjectFactory.GetInstanceOrderService>
    ();
                Console.WriteLine(orderService.ToString());
                IPaymentGateway paymentGateway= ObjectFactory.GetInstancePaymentGateway>
    ();
                Console.WriteLine(paymentGateway);
                Console.ReadKey();
        }
    }
    

以上就是经典的IoC/DI容器--structureMap的详细内容,更多请关注其它相关文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

StructureMap容器经典"

若转载请注明出处: 经典的IoC/DI容器--StructureMap
本文地址: https://pptw.com/jishu/592187.html
WPF开发Surface Dial实例教程 聊聊Angular中的自定义管道

游客 回复需填写必要信息