IoC 和 ASP.NET MVC,这一切从哪里开始?
我看到 ASP.NET MVC 中几乎到处都提到了“IoC”和“DI”。虽然我很清楚……“有点”这些是什么,但它是那些几乎模糊的、无定形的浮动概念之一,它似乎要么已经被我忽略,要么我只是没有找对地方。随着 ASP.NET MVC 3.0 即将发布,我对它的认识更加深刻。如此之多以至于感觉它是我不知道的编程的整个部分。
我尝试过寻找有关该主题的书籍,但我发现的大多数内容似乎都做出了很多假设(Ruby 的历史、理解它是什么但不知道如何使用它等)。
我只是预先简单地询问。 IoC 是什么?我为什么关心它?有没有一些不错的网站可以从初学者程序员的角度介绍这一点?假设我对此一无所知。假设我除了 C# 之外一无所知。我阅读了从 .NET 1.1 到 .NET 2.0 的所有关于 C# 的书籍、博客和教程,当 .NET 3.5 和 LINQ 出现时,它变得如此难以处理,以至于我无法跟上。我是一名使用 ASP.NET MVC/C# 的菜鸟开发人员,我不禁觉得我错过了一些非常重要的东西。 (我对 DI(依赖注入)有很多相同的感受)
我用谷歌搜索过,我读过博客,我看过 Castle.Windsor
,我看过示例代码,我浏览过书籍和网络广播,但我还是那么一无所知。这感觉就像是你知道你应该知道的工作内幕故事之一,但出于某种原因,你不知道,而且学校也没有真正让我为此做好准备。
对我来说,StackOverflow 一直是开发社区中最好的。通过大学和入门工作以及低水平的自由职业,我融合了其他程序员的编程教育(尽管据我了解,这不一定是值得羞耻的事情)。因此,我再次向社区声明。
啊?我不明白。
与许多试图进入同一领域的同事交谈时,他们中的许多人都有同样的感觉。所以我不禁感觉到,我们这些“新手”程序员错过了这些概念性编码方法的备忘录。我不仅想得到这个问题的答案,而且我认为对于像我这样试图理解它的其他人来说,一个好的线索可能是一个聪明的想法(如果存在,它需要变得更加明显
)我发现有帮助的是http://www.theserverside。 com/news/1321158/A-beginners-guide-to-Dependency-Injection 和 http://www.dotnetspark.com/kb/1222-inversion-control--beginner-guide.aspx
但我仍然对许多问题摸不着头脑,为什么这很重要。说实话,我觉得开发者社区的很大一部分实际上对 IoC 和单元测试都有这种感觉。很少有人阐述它是什么,而且发现的例子通常都很差,或者很难理解。当 .NET 推出时,它是独立的。我不需要一堆其他概念来运行简单的程序。现在,.NET 是一种受人尊敬的语言,世界上的一切都在采用它(nHibernate、MVC、MVP、MVVP、云平台、游戏架构等)
(如果我是公然错误,请随时指出。我是无论如何,我不会称自己为“好”,但我很了解 C#,我认为自己学得很快,尽管我在这方面遇到了困难,但我只能假设有一些“能力”。那里有需要填补的洞)
I see "IoC" and "DI" mentioned pretty much everywhere for ASP.NET MVC. While I'm well aware of ... 'kind of' what these are, it's one of those almost ambiguous, amorphous floating concepts that seems to have either passed me by, or I'm just not looking in the right place. With the upcoming release of ASP.NET MVC 3.0, I'm seeing it even more. So much so that it feels like it's an entire part of programming I simply don't know about.
I've tried looking for books on the subjects, but most of what I find seems to make a lot of assumptions (history in Ruby, understanding what it is but not how to use it, etc).
I'm just asking upfront and plain. What is IoC, and why do I care about it? Are there any decent websites that cover this from a beginner programmer perspective? Assume I know nothing about it. Assume I know nothing but C#. I read every book, blog, and tutorial on C# from .NET 1.1 to .NET 2.0, and when .NET 3.5 and LINQ hit, it just became so much to handle that I couldn't keep up. I am a rookie developer working in ASP.NET MVC/C# and I can't help but feel I am missing something very important. (I'm having a lot of the same feelings about DI (dependency injection))
I've googled, I've read blogs, I've seen Castle.Windsor
, I've seen sample code, I've browsed books and webcasts, and I'm still so in the dark. It feels like one of those inside stories at work that you know you should know about, but for some reason, you don't, and school didn't really prepare me for it.
StackOverflow has always shown to be the best of the best in the development community for me. Through college and introductory work, and low-level freelance work, I have amalgamated my education on programming from other programmers (though as I understand it that is not neccessarily something to be ashamed of). So once again, I am simply stating to the community.
Huh? I don't understand.
Speaking with many of my colleagues that are trying to break into the same field, many of them feel the same way. So I can't help but feel somewhere along the line, us 'newbie' programmers missed the memo of these conceptual approaches to coding. I'd like to not only get this question answered, but I think a good thread for other people like myself who are trying to understand it might be an intelligent idea (and if one exists, it needs to be made more obvious)
Some of what I have found that has been helpful has been http://www.theserverside.com/news/1321158/A-beginners-guide-to-Dependency-Injection and http://www.dotnetspark.com/kb/1222-inversion-control--beginner-guide.aspx
But I am still left scratching my head on a number of issues as to why this is important. Truthfully, I feel that a large part of the developer community actually feels this way about both IoC and Unit Testing. Very little lays out what it is and the examples discovered are usually pretty poor, or very hard to understand. When .NET was introduced, it was stand-alone. I didn't need a bunch of other concepts to make simple programs run. Now, .NET is a respected language, and everything under the sun is adopting it (nHibernate, MVC, MVP, MVVP, Clouding platforms, gaming architecture, etc)
(Please feel free to point out if I am just flagrantly wrong. I am not calling myself 'good' by any means. But I know C#, and I know it very well. I consider myself a very fast learner and as hard a time as I am having with this I just have to assume that there is some 'hole' out there that needs to be filled)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们把所有 IoC 框架放在一边并讨论这个概念。 IoC 并不是一个新概念,并且已经存在很长时间了。 IoC 的实现基本上是为了使您的系统与某些子系统松散耦合。我们通过将子系统的公共/核心行为提取到合约中来实现解耦。契约通常是界面的形式。现在你依赖这个接口了。您并不担心特定系统如何具体地为您提供您所追求的行为。
最近我在现实生活中遇到了这个。我们现有一个支付系统,然后我们正在开发一个新的支付系统以遵守 PCI。在这种情况下,您要做的就是在界面中提取两个系统的共同特征,然后您的应用程序仅通过界面与这两个系统进行交互。您可以在配置文件或工厂模式中使用标志,或者最终使用 IoC 框架在应用程序中注入此依赖项。在运行时,您的应用程序将获取它所依赖的对象。您需要在由于任何原因而导致任何给定子系统的使用存在不确定性的环境中使用 DI/IoC。在此示例中,新的支付系统无法按时交付,因此我的应用程序需要一个可配置选项来在运行时在任何系统之间切换。如果我不做 IoC,每次业务改变主意时我都必须更改代码。
IoC的另一个需求是做TDD。我上面描述的同样的东西可以用来开发真实应用程序的模拟实现,并且可以用来在真实组件仍在开发中时进行测试。
依赖注入是一本很好的入门书。您还可以在此处下载同一作者关于 DI 的免费论文。
更新:添加代码以更加清晰
现在我可以有两种不同的具体实现:
我的应用程序所要做的就是调用合同中可用的方法。现在应用程序正在显式处理依赖项,但没有任何具体实现。然而它仍然与这两个具体实现相关。 IoC 框架帮助我们将这项工作外包给配置文件。在那里,我可以提供任何其他具体实现,无论它是如何实现的,并且应用程序将继续工作而无需任何代码更改。
以下是我使用 Unity 2 并自行外包显式依赖处理的代码。
相同的配置:
HTH
Let's keep all the IoC frameworks aside and discuss the concept. IoC is not a new concept and has been around for a long time. IoC is basically implemented to make your system loosly coupled to certain subsystem. We achieve decoupling by extracting out common/core behavior of subsystem into a contract. Contract is usually in shape of interface. Now you depend on this interface. You are not worried about how a particular system concretely gives you the behavior that you are after.
Recently I had real life encounter with this. We are having one existing payment system and then we are developing a new payment system to adhere to PCI. What you do in these kind of situation is that you extract the common features of both systems in an interface and then your application interacts with these two systems only via interface. You can either use a flag in config file or factory pattern or ultimately an IoC framework to inject this dependency in your application. At runtime your application will get the object that it depends on. You need DI/IoC in an environment where uncertainty is there regarding the use of any given subsystem due to any reason. In this example new payment system couldn't get delivered on time and hence my application needed a configurable option to switch between any system at runtime. If I don't do IoC, I will have to do code change every time business changes their mind.
Another need of IoC is to do TDD. Same thing that I described above can be used to develop a mock implementation of real application and can be used to do testing while real component is still in development.
Dependency Injection is a good book to start with. You can also download a free paper on DI from the same author here.
Update: Adding code for more clarity
Now I can have two different concrete implementations:
All my application has to do is call the method available in contract. Right now application is handling the dependencies explicitly but is free of any concrete implementation. However it is still tied to these two concrete implementations. IoC frameworks helps us to outsource this job to a configuration file. There I can provide any other concrete implementation regardless of how it is implemented and application will continue to work without any code change.
Following is the code where I am using Unity 2 and outsourcing explicit handling of dependencies on my own.
Config for the same:
HTH
有关一些视频介绍,请查看 DimeCasts.net。
我喜欢 Ioc 的两个主要原因:
For some video intros, check out DimeCasts.net.
The main two reasons I like Ioc:
我认为当您了解为什么以及何时使用 IoC/DI 时,您就会开始掌握 IoC/DI 的内容。我不会太担心使用哪个 IoC 容器。您本质上可以自己编写一个基本容器(在最基本的级别上,它只是一个将接口映射到具体实现的字典)。 这里是一个很好的视频,展示了如何完成此操作。
核心概念是它鼓励组件的松耦合,这意味着代码更容易测试并且不依赖于其他具体类。
此链接包含有关 SOLID 原则的详细信息,您的问题特别与“D “(依赖倒置原理),但其他原理也会引起人们的兴趣。
I think you will begin to grasp IoC/DI stuff when you understand why and when it is used. I wouldnt worry too much about which IoC container to use. You could essentially write a basic container yourself (At the most basic level its is just a Dictionary that maps an interface to a concrete implementation ). Here is a good video showing how that would be done.
The core concept is that it encourages loose coupling of components, which means code is easier to test and does not depend on other concrete classes.
This link has details about the SOLID principles, your question particularly pertains to the "D" (Dependency Inversion Principle), but the others will also be of interest.