控制反转/依赖注入在.net中有很好的解释和例子?

发布于 2024-08-20 04:31:43 字数 162 浏览 8 评论 0原文

你好

我对 .net 框架中的 IoC/DI 框架了解不多。任何人都可以给我链接,用 C# 中的一些示例详细解释 IoC/DI 吗?我想仔细阅读并进一步了解这些框架。

这样我就可以获得知识,在哪里以及如何使用这些框架对实现项目很有用。



nrk

Hi

I don't have much knowledge on IoC/DI frameworks in .net framework. Can anyone give me links that explans IoC/DI in detail with few example in C#? I want go through it and get more idea about these frameworks.

So that I can get the knowledge, where and How can I use these frameworks are useful in implementing the project.

nrk

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲性难收 2024-08-27 04:31:43

有关 DI 概念的一般介绍,包括 C# 中的综合示例,您可能需要阅读我的书依赖注入.NET

For a general introduction to the concept of DI, including comprehensive examples in C#, you may want to read my book Dependency Injection in .NET.

雨的味道风的声音 2024-08-27 04:31:43

之一

您最好的选择也是查看 IOC/DI 网站Spring.net
http://www.springframework.net/

温莎城堡
http://www.castleproject.org/container/index.html

结构图
http://structuralmap.sourceforge.net/Default.htm

关于比较的好文章国际奥委会

http://blog.ashmind.com/index.php/2008/08/19/comparing-net-di-ioc-frameworks-part-1/

http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx

我希望有所帮助。我个人使用过 Spring.Net 和 Castle Windsor - 后者可能更容易理解和使用。

也看看这个问题。
IoC 容器示例

Your best bet is too look at one of the IOC/DI website

Spring.net
http://www.springframework.net/

Castle Windsor
http://www.castleproject.org/container/index.html

Structure Map
http://structuremap.sourceforge.net/Default.htm

Good articles on comparison of IOC

http://blog.ashmind.com/index.php/2008/08/19/comparing-net-di-ioc-frameworks-part-1/

http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx

I hope that helps. I personally have used Spring.Net and Castle Windsor - the latter probably easier to understand and use.

Have a look at this question as well.
Examples of IoC Containers

倾`听者〃 2024-08-27 04:31:43

Aim Kai 提到了一些关于具体 IOC 容器实现和相应教程的非常好的资源,但是它们狭隘地集中在正在讨论的 IOC 容器上,而不是一般性的介绍/教程。

就我个人而言,我喜欢

Aim Kai has mentioned some very good resources on concrete IOC Container implementations and corresponding tutorials, however they are narrowly focused on the IOC Container beeing discussed and less of a general introduction/tutorial.

Personally, I like the introduction Rob Connery wrote best.

等风来 2024-08-27 04:31:43

这都是关于松散耦合的。

假设您有一个类 foo 依赖于类 bar

public class Foo
{
      public Foo(Bar bar)
      {
      }
}

这里我们必须将 bar 的实例传递到类中以实例化它。如果我们想要有两种不同的 Bar 实现,一种用于与数据库交互,另一种用于测试,会发生什么?

好吧,我们将创建并连接 IBar 并使用它,然后我们可以使用两个具体的实现。

现在考虑一下,我们在很多地方都有很多被调用的类中有很多复杂的依赖关系,如果我们想改变实现,我们就必须在创建实现 IBar 的对象的每个地方改变代码,这可能是很多工作。

相反,我们可以像这样使用依赖注入。

IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<IBar, Bar>();
Foo foo = myContainer.Resolve<Foo>();

这里,Unity 框架已将 Bar 对象插入到 Foo 的构造函数中,如果您在一个地方更改了类型注册,则您会更改 Foo 对象在整个地方的解析方式。

当您有可能发生变化的复杂依赖项时,DI 至关重要!

It's all about loose coupling.

Imagine you have a class foo that is dependant on class bar

public class Foo
{
      public Foo(Bar bar)
      {
      }
}

Here we have to pass an instance of bar into the class to instantiate it. What happens if we want to have two differant implementations of Bar, one for interacting with a DB and one for testing?

Well we would create and interface IBar and use that instead, then we can use two concrete implementations.

Now consider that we have lots of complex dependencies in lots of classes which are being called in lots of places, if we wanted to change the implementation we would have to change the code in every place a an object implementing IBar is created, that could be a lot of work.

Instead we can use dependancy injection like this.

IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<IBar, Bar>();
Foo foo = myContainer.Resolve<Foo>();

Here the unity framework has inserted a Bar object into the Foo's constructor, if you changed the type registration in one place, you change how Foo objects will get resolved all over the place.

When you have complex dependencies that may change, DI is essential!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文