C# 实现IOC 设计原则和简单封装

IOC设计原则

控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。

实现规则

依赖倒置原则(DIP): 设计模式的六大原则之一。
依赖注入(DI): IOC的实现方式之一。
IOC容器: 依赖注入的框架,用来映射依赖,管理对象创建和生存周期(DI框架)。

依赖倒置原则(DIP)

概念:高层模块不依赖低层次模块的细节,高层次就是不依赖细节而是依赖抽象(不依赖具体的类,而是依赖于接口)

伪代码:


    interface IModuleBase
    {

        void Init();
    }



 public class ModuleA : IModuleBase
    {

        public void Init()
        {
            Console.WriteLine("ModuleA Init");
        }
    }



    public class ModuleB :IModuleBase
    {


        public void Init()
        {
            Console.WriteLine("ModuleB Init");
        }

    }

class Program
    {
        static void Main(string[] args)
        {

            IModuleBase aTest = new ModuleA();
            IModuleBase bTest = new ModuleB();
            aTest.Init();
            bTest.Init();


        }
    }



输出结果为:

ModuleA Init

ModuleB Init

依赖注入(DI)

控制反转实现的一种方式,就是就是将依赖对象的创建和绑定转移到被依赖对象类的外部来实。

依赖注入有多种实现方式:

  • 构造函数注入
    public class ModuleManager
    {
        private IModuleBase module;
        public ModuleManager(IModuleBase module)
        {
            this.module = module;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            IModuleBase aTest = new ModuleA();
            ModuleManager manager = new ModuleManager(aTest);

        }
    }
    
  • 属性注入

    public class ModuleManager
    {
        private IModuleBase module;
        public ModuleManager()
        {
            
        }

        public IModuleBase Module { get => module; set => module = value; }

        public void Init()
        {
            module.Init();
        }
    }
	//调用测试
    static void Main(string[] args)
    {
        IModuleBase aTest = new ModuleA();
        ModuleManager manager = new ModuleManager();
        manager.Module = aTest;
        manager.Init();
    }
  • 接口注入
public interface IManager
{
    void SetModue(IModuleBase module);
}
public class ModuleManager: IManager
{
    private IModuleBase module;
    public void Init()
    {
        module.Init();
    }

    public void SetModue(IModuleBase module)
    {
        this.module = module;
    }
}

static void Main(string[] args)
{
    IModuleBase aTest = new ModuleA();
    ModuleManager manager = new ModuleManager();
    manager.SetModue(aTest);
    manager.Init();

}
    

IOC容器

class IocContainer
{
   Dictionary<Type, Type> iocMap = new Dictionary<Type, Type>();
 

   /// <summary>
   /// 手动注册
   /// </summary>
   /// <typeparam name="TypeInterface"></typeparam>
   /// <typeparam name="InterfaceClass"></typeparam>
   public void Register<TypeInterface, InterfaceClass>()
   {
       if (iocMap.ContainsKey(typeof(TypeInterface)))
       {
           throw new Exception(string.Format("The interface {0} have be registered to Container", typeof(TypeInterface).FullName));
       }
       else
       {
           iocMap.Add(typeof(TypeInterface), typeof(InterfaceClass));
       }
   }

   public T Resolve<T>()
   {
       return (T)Resolve(typeof(T));
   }
   public object Resolve(Type TypeInterface)
   {
       if (!iocMap.ContainsKey(TypeInterface))
       {
           throw new Exception(string.Format("The interface {0} not be registered to Container", TypeInterface.FullName));
       }
       Type InterfaceClass = iocMap[TypeInterface];
       ConstructorInfo conInfo = InterfaceClass.GetConstructors().First();
       List<ParameterInfo> paramsInfoList = conInfo.GetParameters().ToList();
       List<object> interfaceClassParams = new List<object>();
       foreach (var param in paramsInfoList)
       {
           Type temp = param.ParameterType;
           object result = Resolve(temp);
           interfaceClassParams.Add(result);
       }
       object resultObject = conInfo.Invoke(interfaceClassParams.ToArray());
       return resultObject;
   }
}

上面是一个简单的IOC容器,下面是我想通过构造函数以配置的方式去注册容器内容,代码如下:

  class IocContainer
  {
      Dictionary<Type, Type> iocMap = new Dictionary<Type, Type>();
      public IocContainer()
      {
          string  config = "IModuleA;IModuleB";//配置文件


          Assembly asm = Assembly.GetAssembly(typeof(ModuleAttribute));
          Type[] types = asm.GetExportedTypes();

          Func<Attribute[], bool> IsMyAttribute = o =>
          {
              foreach (Attribute a in o)
              {
                  if (a is ModuleAttribute)
                      return true;
              }
              return false;

          };

          Type[] typesTemp = types.Where(o =>
          {
              return IsMyAttribute(Attribute.GetCustomAttributes(o, true));
          }).ToArray();

          string[] tempArray = config.Split(';');
          foreach(string item in tempArray)
          {
   
              foreach(Type typeItem in typesTemp)
              {
                  Type inter = typeItem.GetInterfaces().ToList().Find(o => o.Name.Equals(item));
                  if(inter != null)
                  {
                      iocMap.Add(inter,typeItem);
                  }
              }
          }
          
          //Register<IModuleA, ModuleA>();
          //Register<IModuleB, ModuleB>();
      }

      /// <summary>
      /// 手动注册
      /// </summary>
      /// <typeparam name="TypeInterface"></typeparam>
      /// <typeparam name="InterfaceClass"></typeparam>
      public void Register<TypeInterface, InterfaceClass>()
      {
          if (iocMap.ContainsKey(typeof(TypeInterface)))
          {
              throw new Exception(string.Format("The interface {0} have be registered to Container", typeof(TypeInterface).FullName));
          }
          else
          {
              iocMap.Add(typeof(TypeInterface), typeof(InterfaceClass));
          }
      }

      public T Resolve<T>()
      {
          return (T)Resolve(typeof(T));
      }
      public object Resolve(Type TypeInterface)
      {
          if (!iocMap.ContainsKey(TypeInterface))
          {
              throw new Exception(string.Format("The interface {0} not be registered to Container", TypeInterface.FullName));
          }
          Type InterfaceClass = iocMap[TypeInterface];
          ConstructorInfo conInfo = InterfaceClass.GetConstructors().First();
          List<ParameterInfo> paramsInfoList = conInfo.GetParameters().ToList();
          List<object> interfaceClassParams = new List<object>();
          foreach (var param in paramsInfoList)
          {
              Type temp = param.ParameterType;
              object result = Resolve(temp);
              interfaceClassParams.Add(result);
          }
          object resultObject = conInfo.Invoke(interfaceClassParams.ToArray());
          return resultObject;
      }
  }

在IocContainer中我通过反射去的ModuleAttribute标注的类,去将器注册到容器内,测试如下:

        static void Main(string[] args)
        {
            //IOC测试
            IocContainer container = new IocContainer();
            container.Resolve<IModuleA>().FunA();
            container.Resolve<IModuleB>().FunB();
        }

输出结果:
ModuleA Init

ModuleB Init
参考代码下载

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明
实现IOC的设计模式有多种方式,其中比较常见的方式是使用依赖注入(Dependency Injection)来实现。依赖注入是指通过将对象的依赖关系从程序内部提到外部来管理。通过依赖注入,我们可以将对象的创建和依赖关系的管理交给IOC容器来处理,从而实现对象的解耦和灵活性。 在实现依赖注入时,可以使用不同的方式,如构造函数注入、属性注入和方法注入等。构造函数注入是最常见的方式,通过在对象的构造函数中接收依赖对象的参数来实现注入。属性注入是通过在对象的属性上标记注入的依赖对象来实现注入。方法注入是通过在对象的方法中接收依赖对象的参数来实现注入。 除了依赖注入,还可以使用服务定位器(Service Locator)来实现IOC。服务定位器是一个中心化的容器,用于管理对象的创建和依赖关系的解析。通过服务定位器,我们可以通过名称或类型来获取所需的对象,而不需要显式地指定依赖关系。 另外,还可以使用IOC容器框架来实现IOC。常见的IOC容器框架有Autofac、Unity、Spring等。这些框架提供了一套完整的IOC容器,可以方便地管理对象的创建和依赖关系的解析。 总结起来,实现IOC的设计模式可以通过依赖注入、服务定位器或使用IOC容器框架来实现。具体选择哪种方式取决于项目的需求和个人的偏好。 #### 引用[.reference_title] - *1* *3* [IOC设计模式](https://blog.csdn.net/C_gyl/article/details/109488822)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [设计模式之——IOC](https://blog.csdn.net/u012539826/article/details/103071473)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值