RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
StructureMap怎么用

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

成都创新互联公司长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为大新企业提供专业的成都网站制作、网站建设,大新网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

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怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


网站栏目:StructureMap怎么用
文章出自:http://scyingshan.cn/article/jojsoh.html