• C# 依赖注入????


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ABCDEFG
    {
        public class 依赖注入
        {
            public void test()
            {
                // 功能是 传递不同对象, 执行不同对象下的方法. ( 有隐式转换)
    
                //ServiceClassA serviceA = new ServiceClassA();  //创建 A对象
                //ServiceClassB serviceB = new ServiceClassB();   //创建B对象
                //ClientClass client = new ClientClass(); // 创建 客户端类对象
                //client.Set_ServiceImpl(serviceA);
                //client.ShowInfo();//结果:我是ServceClassA
                //client.Set_ServiceImpl(serviceB);
                //client.ShowInfo();//结果:我是ServceClassB
                //Console.ReadLine();
    
    
                //通过构造函数注入
                ServiceClassA serviceA = new ServiceClassA();  //创建 A对象
                ServiceClassB serviceB = new ServiceClassB();   //创建B对象
                ClientClass client = new ClientClass(serviceA); // 创建 客户端类对象
                client.ShowInfo();//结果:我是ServceClassA
                client = new ClientClass(serviceA);
                client.ShowInfo();//结果:我是ServceClassB
                Console.ReadLine();
            }
        }
    
        //internal    同一程序集内访问    
        //interface   接口
        interface IServiceClass  //定义 服务 接口类
        {
            String ServiceInfo();
        }
        class ServiceClassA : IServiceClass    //A继承接口 ; 实现方法
        {
            public String ServiceInfo()
            {
                return "我是ServceClassA";
            }
        }
        class ServiceClassB : IServiceClass   //B继承接口 ; 实现方法
        {
            public String ServiceInfo()
            {
                return "我是ServceClassB";
            }
        }
    
    
        #region ---Setter注入
        //class ClientClass   //定义 客户 接口类
        //{
        //    //注入点
        //    private IServiceClass _serviceImpl;  //声明接口变量
        //    //客户类中的方法,初始化注入点  
        //    public void Set_ServiceImpl(IServiceClass serviceImpl)
        //    {
        //        this._serviceImpl = serviceImpl;
        //    }
        //    public void ShowInfo()
        //    {
        //        Console.WriteLine(_serviceImpl.ServiceInfo());
        //    }
        //}
        #endregion
    
        #region --构造注入
        class ClientClass
        {
            private IServiceClass _serviceImpl;
    
            public ClientClass(IServiceClass serviceImpl)
            {
                this._serviceImpl = serviceImpl;
            }
    
            public void ShowInfo()
            {
                Console.WriteLine(_serviceImpl.ServiceInfo());
            }
        }
    
        #endregion
    }
  • 相关阅读:
    bash shell if 命令参数说明
    Linux test命令
    javascript获取事件触发源
    PHP Manual 学习
    js 控制iframe 刷新
    WP7交互特性浅析及APP设计探究
    FirstDayStart点点
    关于Safari的思考(转载)
    如何使控件背景色支持TransparentKey(at Win2k/WinXP 32bit Color Desktop)
    [Bug] VisualStyleRenderer may cause GDI leak!
  • 原文地址:https://www.cnblogs.com/enych/p/10500157.html
Copyright © 2020-2023  润新知