ToB企服应用市场:ToB评测及商务社交产业平台

标题: 从依赖倒置原则到IOC控制反转实现 [打印本页]

作者: 用户国营    时间: 2024-5-18 05:20
标题: 从依赖倒置原则到IOC控制反转实现
从依赖倒置原则(Dependency Inversion Principle, DIP)到控制反转(Inversion of Control, IoC)再到依赖注入(Dependency Injection, DI)的演进过程,我们可以理解为一种逐步抽象和解耦的设计头脑。这种头脑在C#等面向对象的编程语言中得到了广泛的应用。
起首,让我们回首一下依赖倒置原则。这个原则建议我们:
这个原则鼓励我们设计代码时,让高层模块与底层模块之间的依赖通过抽象(接口或基类)来进行,从而淘汰了模块间的耦合度。
接下来,控制反转是一种设计模式,它通过将原本由代码直接控制的对象的调用权交给第三方(如一个容器)来控制,来降低代码间的耦合度。在C#中,IoC容器是实现控制反转的一个工具。
DI是IoC的一种实现方式,它将依赖关系通过构造函数、属性或方法注入到对象中。这种方式进一步降低了代码间的耦合度,使得对象在被创建时就能得到它所必要的依赖。
现在,我们来实现一个简单的IoC容器,并通过递归完成构造函数注入。以下是一个简单的示例:
  1. public interface IService  
  2. {  
  3.     void DoWork();  
  4. }  
  5.   
  6. public class ConcreteService : IService  
  7. {  
  8.     private readonly IDependency _dependency;  
  9.   
  10.     public ConcreteService(IDependency dependency)  
  11.     {  
  12.         _dependency = dependency;  
  13.     }  
  14.   
  15.     public void DoWork()  
  16.     {  
  17.         _dependency.DoSomething();  
  18.     }  
  19. }  
  20.   
  21. public interface IDependency  
  22. {  
  23.     void DoSomething();  
  24. }  
  25.   
  26. public class ConcreteDependency : IDependency  
  27. {  
  28.     public void DoSomething()  
  29.     {  
  30.         Console.WriteLine("Dependency is doing something...");  
  31.     }  
  32. }  
  33.   
  34. public class SimpleIoCContainer  
  35. {  
  36.     private readonly Dictionary<Type, Func<object>> _registrations = new Dictionary<Type, Func<object>>();  
  37.   
  38.     public void Register<TService, TImplementation>() where TImplementation : TService  
  39.     {  
  40.         _registrations.Add(typeof(TService), () => Activator.CreateInstance(typeof(TImplementation)));  
  41.     }  
  42.   
  43.     public TService Resolve<TService>()  
  44.     {  
  45.         if (!_registrations.ContainsKey(typeof(TService)))  
  46.         {  
  47.             throw new InvalidOperationException($"No registration found for {typeof(TService).FullName}");  
  48.         }  
  49.   
  50.         var factory = _registrations[typeof(TService)];  
  51.         return (TService)factory();  
  52.     }  
  53.   
  54.     public object Resolve(Type serviceType)  
  55.     {  
  56.         if (!_registrations.ContainsKey(serviceType))  
  57.         {  
  58.             throw new InvalidOperationException($"No registration found for {serviceType.FullName}");  
  59.         }  
  60.   
  61.         var factory = _registrations[serviceType];  
  62.         return factory();  
  63.     }  
  64. }  
  65.   
  66. public class Program  
  67. {  
  68.     public static void Main()  
  69.     {  
  70.         var container = new SimpleIoCContainer();  
  71.         container.Register<IService, ConcreteService>();  
  72.         container.Register<IDependency, ConcreteDependency>();  
  73.   
  74.         IService service = container.Resolve<IService>();  
  75.         service.DoWork();  
  76.     }  
  77. }
复制代码
在这个示例中,我们定义了一个简单的IoC容器SimpleIoCContainer,它利用了一个字典来存储服务范例到工厂方法的映射。Register方法用于注册服务,而Resolve方法用于解析服务。在这个例子中,ConcreteService依赖于IDependency,IoC容器负责在创建ConcreteService实例时注入ConcreteDependency实例。
应用场景

IoC容器和依赖注入在许多场景下都非常有效,包括但不限于:

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4