资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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.cdkjz.cn/article/dsojep.html
十二年 建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220