找到你要的答案

Q:MVC 6 dependency injection - to use or not to use

Q:MVC 6依赖注入,使用或不使用

I am developing a simple project in MVC 6 and bit confused about whether to use DI or not.

I have a view that POSTs to an action - AddData(). Now inside AddData() action I instantiate a couple of classes, say Class1 and Class2, and then invoke their methods to get the job done.

Now my confusion is - In MVC 5 I used to create local instances of Class1 and Class2 inside the action and then call methods on them. This was without any DI.

In MVC 6 do I need to DI Class1 and Class2 in the controller? They are needed only in AddData() action. So is this situation good for DI or traditional local objects would be better?

Please clarify.

我开发MVC 6和困惑,是否使用DI或不是一个简单的项目。

我有一个观点,一个动作adddata()帖子。现在在adddata()行动我实例化一个几班,一班和二班说,然后调用他们的方法去做。

现在我的困惑是在MVC 5我用来创建在行动,然后调用它们的方法的本地实例的一阶、二阶。这是没有任何DI。

在MVC 6需要我地一阶、二阶的控制器?他们只需要adddata()行动。那么这种情况是否适合DI或传统的本地对象会更好?

请澄清。

answer1: 回答1:

Dependency injection is a tool on your developer toolbox that should be used when needed. It will allow you to follow principles like SOLID, which will make your application design better, and it will be of invaluable help if you plan to do unit testing.

IMHO it is a great thing that dependency injection is now fully integrated across the whole ASP 5 pipeline. This way, whenever you need it, you won't have to fight the framework as the seam for your code is already there.

But the fact that you can now use it, doesn't mean you should always use it. Use your judgement!

  • If you are writing some throwaway code or a very simple app, and you really think dependency injection is overkill, then don't use it. But at least you have taken a conscious decision!

Of course, the easier it is to apply DI, the more you might end up using it even for simple projects or throwaway code.

依赖注入是开发工具工具箱中的一个工具,需要时使用。它可以让你遵循像固体的原则,这将使您的应用程序设计更好,这将是非常宝贵的帮助,如果你打算做单元测试。

IMHO it is a great thing that dependency injection is now fully integrated across the whole ASP 5 pipeline. This way, whenever you need it, you won't have to fight the framework as the seam for your code is already there.

但事实上,你现在可以使用它,并不意味着你应该始终使用它。运用你的判断!

  • If you are writing some throwaway code or a very simple app, and you really think dependency injection is overkill, then don't use it. But at least you have taken a conscious decision!

当然,这是更容易申请迪,你越会使用它,即使是简单的项目或一次性代码。

answer2: 回答2:

It is better to do so, so that in case you want to test your app later on, you can substitute with mocks, for instance. However, if you do not care about testing or if the app is really simple, then I don't see a reason against simply instantiating them inside the controller.

最好是这样做的,所以,如果你想测试你的应用程序后,你可以用嘲笑,例如。然而,如果你不在乎测试或者应用程序是非常简单的,那么我没有看到一个反对简单地实例化它们控制器内部原因。

answer3: 回答3:

Without knowing what "Class1" and "Class2" are used for, this can't really be answered.

  • If they are models, then no, you wouldn't use DI.

  • If they are services/utilities, then yes, ideally use DI.

However - there's no difference in the architecture of DI between MVC5 and MVC6, so if you are not using DI in MVC5 then there's no reason to suddenly need to when you move to MVC6 unless you want to improve your practices.

Note there are some 'simple' IoC/DI frameworks that make this very easy and you can always use "poor man's DI" where you have a constructor with the class and one without, eg:

public Controller1() 
{
    this.class1 = new Class1();
}

public Controller1(Class1 class1)
{
    this.class1 = class1;
}

(many people discourage this, but it's an option)

不知道“一级”和“班”来,这真的无法回答。

  • 如果他们是模型,那么没有,你不会使用DI。

  • 如果他们是服务/公用事业,那么是的,理想的使用DI。

然而,在二MVC5和mvc6之间的建筑没有什么区别,所以如果你不使用DI MVC5再没有理由突然需要你去mvc6除非你想提高你的做法。

注意有一些'简单' IOC / DI框架,使这非常容易,你可以永远使用“穷人的DI”,你有一个构造函数的类和一个没有,例如:

public Controller1() 
{
    this.class1 = new Class1();
}

public Controller1(Class1 class1)
{
    this.class1 = class1;
}

(许多人对此表示反对,但这是一个选择)

asp.net  asp.net-mvc  asp.net-core-mvc