什么是MVC应用程序中使用的IoC框架? [英] What is the use of an IoC framework in an MVC application?

查看:177
本文介绍了什么是MVC应用程序中使用的IoC框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解使用的IoC框架像StructureMap,但我不禁想到,这些设计模式只是无稽之谈,使得code只是更复杂。

I'm trying to understand the use of an IoC framework like StructureMap, but i can't help thinking that these "design patterns" are just nonsense, making code just more complex.

让我先从一个例子,我认为一个IoC有些有用的。

Let me start with an example where i think an IoC is somewhat usefull.

我认为当与控制器类的MVC框架实例化处理一个IoC是很有用的。在这种情况下,我想在.NET MVC框架。

I think an IoC can be usefull when dealing with the instantiation of controller classes in an MVC framework. In this case i'm thinking about the .NET MVC framework.

通常情况下,控制器类的实例是由框架处理。因此,这意味着你不能真正传递任何参数到控制器类的构造函数。 这是一个IoC框架就可以派上用场。某处在IoC容器可以指定哪些类应该被实例化,并传递给你的控制器构造被调用的控制器类时。

Normally the instantiation of the controller class is handled by the framework. So that means you can't really pass any parameters to the constructor of your controller class. This is where an IoC framework can come in handy. Somewhere in an IoC container you specify what class should be instantiated and passed to your controllers constructor when the controller class is invoked.

这是也很方便,当你想进行单元测试控制器,因为你可以模拟传递给它的对象。

This is also handy when you want to unit test the controller, because you can mock the object that is passed to it.

但就像我说的,我可有点明白人们为什么要使用它自己的控制器类。但不应超出这一点。从那里,你可以简单地做正常的依赖注入的。

But like i said, i can somewhat understand why people want to use it for their controller classes. But not outside of that. From there on you can simply do normal Dependency Injection.

但是,为什么不干脆做这样的:

But why not simply do it like this:

public class SomeController
{
    public SomeController() : this( new SomeObj() ) 
    {
    }

    publiv SomeController(SomeObj obj)
    {
        this.obj = obj;
    }
}

现在你不必使用任何第三方IoC框架这也意味着较低的学习曲线。因为你没有进入该框架藏汉的规格。

Now you don't have to use any 3rd party IoC framework which also means a lower learning curve. Since you don't have to go into the specs of that framework aswell.

您仍然可以嘲笑对象的单元测试。所以没有任何问题无论是。

You can still mock the object in your Unit Tests. So no problem there either.

你可以说的唯一一件事就是,但现在你的类的紧耦合 someObj中。 这是真的。但谁在乎!?这是一个控制器类!我不打算重用这个类,永远......那么,为什么在地球上,我应该担心的是紧耦合..?我可以模拟传递给它的对象。这是唯一重要的事情。

The only thing you can say is, "but now your class is tightly coupled to SomeObj". This is true. But who cares!? It's a controller class! I'm not going to reuse that class, ever.. So why on earth should i worry about that tight coupling..? I can mock the object that is passed to it. That's the only important thing.

所以,我为什么要费心使用IOC?我真的缺少点......?对我来说,IoC模式只是一些被高估的格局。添加更多的,复杂的层到应用程序...

So why should i bother using an IoC? Am i REALLY missing the point...? To me the IoC pattern is just some overrated pattern. Adding more, complex layers to your application...

推荐答案

您问一个很好的问题,在你的榜样,我会说一个IoC会/可能是矫枉过正,但只考虑你的code扩展级以下然后你可能会开始看到有一个IoC做这一切都为你的利益。

You ask a good question and in your example I would say that an IoC would / could be overkill, but just consider your code extended to the following and then you might begin to see the benefit of having an IoC do all this for you.

public class SomeController
{
    public SomeController() : this( new SomeObj(new configuration(), new SomethingElse(new SomethingElseToo())) ) 
    {
    }

    publiv SomeController(SomeObj obj)
    {
        this.obj = obj;
    }
}

这篇关于什么是MVC应用程序中使用的IoC框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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