English 中文(简体)
失控之处
原标题:Where is the control inverted in Inversion of Control

我花了许多时间来理解ioc。 我从来不理解这种格局中究竟是怎样扭曲控制。 甚至我也混淆了变本的确切含义。 在正常的english锁中,反转是一些东西,如倒置,可以说是 revers倒。

如果我把依赖注射视为伊诺。 我想知道,究竟是哪里有人在座击.。 我在此理解的是,在建筑商、建筑商的帮助下,从外部实体插入了依赖性。

But I never understand where the control is inverted here...

感谢任何帮助。

最佳回答

旧风格:

Class car
{
  Engine _engine;
   Public Car()
   {
       _engine = new V6();
   }
}

Class car
{
   Engine _engine;
    Public Car(Engine engine)
    {
        _engine = engine;
     }
}

var car = new Car(new V4());

打电话的人有控制权,而不是车类。

问题回答

依赖性注射反向控制。

例如,一辆汽车需要一个发动机级。 发动机可以是任何类型的发动机。

如果你不使用DI,则汽车舱将自行确定发动机类型,则汽车舱将受到控制。

在使用DI时,即刻开车的代码将确定车的类型(例如,通过指定建筑商的发动机),警车代码现已控制。 控制权从汽车类别转向使用代码。

To expand on robin s answer:

IUserRepository _user  = new UserRepository();
//you re in control of which instance is created.

With dependency injection:

IUserRepository _user;// you will not do anything else. 

根据其他地方的组合,你使用的依赖性注射框架将考虑为你创建<>明星>右/平线>。 这正是贵国法规中的inversed。 你不是直接从你的法典中挑出任何事情。

Why!? Why would you do this?

一项重大好处是,在你进行测试时,你可以把<>国际交易日志><<> 交存/编码>与假打一。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签