zoukankan      html  css  js  c++  java
  • 依赖注入例子

     class Program
        {
            interface ILogger
            {
                void Log(string message);          
            }
            class Logger : ILogger
            {
                public void Log(string message)
                {
                    Console.WriteLine(message);
                }
            }

            interface IPropertyInject
            {
                ILogger PropertyLog { get; set; }
            }

            class PropertyInject : IPropertyInject
            {
                public ILogger PropertyLog { get; set; }
            }

            class ConstuctorInject
            {
                private ILogger ConstuctorLog;
                public ConstuctorInject(ILogger log)
                {
                    ConstuctorLog = log;
                }
                public void Log()
                {
                    ConstuctorLog.Log("我是构造函数注入");
                }
            }
            
            static void Main(string[] args)
            {
                var container = new WindsorContainer();
                container.Register(Component.For<ILogger>().ImplementedBy<Logger>());
                container.Register(Component.For<IPropertyInject>().ImplementedBy<PropertyInject>());
                container.Register(Component.For<ConstuctorInject>());

                var logger = container.Resolve<ILogger>();
                logger.Log("我是依赖注入实例化");

                var property = container.Resolve<IPropertyInject>();
                property.PropertyLog.Log("属性依赖注入");

                var consturctor = container.Resolve<ConstuctorInject>();
                consturctor.Log();
            }
        } 
  • 相关阅读:
    Smali语法
    css 实现垂直水平居中常用方法
    css border实现三角形
    vue-router学习笔记
    vuex状态管理
    es6 reduce的用法
    vue学习笔记
    chrome调式工具
    前端需要了解的http知识
    underscore.js and moment.js
  • 原文地址:https://www.cnblogs.com/ZengYunChun/p/6094203.html
Copyright © 2011-2022 走看看