控制及反转;依赖注入在.NET Framework [英] Inversion of Control & Dependency Injection in the .NET Framework

查看:435
本文介绍了控制及反转;依赖注入在.NET Framework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有DI被应用作为建筑原则或设计模式的在.NET Framework本身的任何具体的例子/实例?是否有任何在该框架的类型的(或多个)/ BCL符合IoC的?

Is there any specific example/instance of DI being applied as an architectural principle or design pattern in the .NET Framework itself? Do any (or many) of the types in the framework/BCL conform to IoC?

基于C#中的类型名称和简要说明/解释将是巨大的!

The type names and a brief illustration/explanation based in C# would be great!

此将复方需要的DI注入设计原理作为最佳实践...,因为它是从基部框架本身中收集

This would compund the need for DI infused design principle as a best-practice...as it is gleaned from the base framework itself.

我要重申,我不是在寻找的IoC / DI框架的可是作为的IoC / DI 的框架。

I reiterate, I am not looking for IoC/DI Frameworks BUT for IoC/DI IN the framework.

编辑:只是想获得更多的实例/例子......因此,赏金

Just wanted to get more instances/examples ... hence the bounty!

推荐答案

在一般不会有大量的DI在BCL例子 - 也许是因为BCL是一个相当自足的框架,DI是更多应用程序架构的关注。不过,这里有一些例子,我能找到为止。

In general there aren't a lot of examples of DI in the BCL - perhaps because the BCL is a rather self-contained framework, and DI is more of an application architecture concern... However, here are some examples I've been able to find so far.

构造器注入

有没有构造器注入的BCL的例子很多。最好的候选人

There are not many examples of Constructor Injection in the BCL. The best candidates are

  • System.IO.StreamWriter
  • 就是System.IO.StreamReader

属性注入

  • System.ComponentModel.IComponent.Site

我们也看到在Workflow Foundation中的 WorkflowRuntime.AddService 和相关方法的变化,虽然你可能会认为这是更接近方法注入。

We also see a variation in Workflow Foundation's WorkflowRuntime.AddService and related methods, although you might argue that this is closer to Method Injection.

方法注入

  • System.ComponentModel.Design.IDesigner.Initialize
  • System.ComponentModel.TypeConverter(很多方法都ITypeDescriptorContext)
  • System.Web.Mvc.IModelBinder.BindModel(从ASP.NET MVC)

周围语境

  • System.Threading.Thread.CurrentPrincipal
  • System.Threading.Thread.CurrentCulture
  • System.Threading.Thread.CurrentUICulture
  • System.Diagnostics.Trace.Listeners

FWIW,我画了从我即将出版的新书

FWIW, I drew these examples from my upcoming book.

这篇关于控制及反转;依赖注入在.NET Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆