如何创建没有构造函数参数且具有依赖项注入的类 [英] How to create a class without constructor parameter which has dependency injection

查看:125
本文介绍了如何创建没有构造函数参数且具有依赖项注入的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将依赖项注入添加到了项目中.但是,当我使用 new 关键字创建实例时,依赖项注入无效.

I have added the dependency injections to the project. But when i create an instance by using new keyword, dependency injection doesn't work.

public class MyClass
{
    ILoginTokenKeyApi _loginTokenKeyApi;

    public MyClass(ILoginTokenKeyApi loginTokenKeyApi)
    {
        _loginTokenKeyApi = loginTokenKeyApi;
    }

    ...
}

当我尝试创建MyClass的实例时,它希望自然构造一个参数.

When i try to create an instance of MyClass, it wants a parameter to be constructed naturally.

就像这样:

MyClass mc = new MyClass(); // ERROR, it wants a parameter (but it is what i want)

我必须做:

MyClass mc = new MyClass(new LoginTokenKeyClass()); // this is not a good code for me

我如何创建不带参数的MyClass实例,因为它已注入依赖项.

How i create an instance of MyClass without parameter because it has dependency injected.

推荐答案

但是当我使用new关键字创建实例时,依赖项注入不起作用.

But when i create an instance by using new keyword, dependency injection doesn't work.

从根本上讲,这就是依赖注入的工作原理.

That’s fundamentally how dependency injection works.

使用依赖项注入,您根本不应该new创建新对象.这就是依赖注入

With dependency injection, you are simply not supposed to new up new objects. That’s the whole point of dependency injection and inversion of control. Instead of creating objects and managing those objects’ dependencies, you are depending on the framework to give you the dependencies you need without having you to care about where they actually come from and how they are constructed properly. So you are moving the responsibility to create the object up to the caller.

如果发现自己需要创建一个具有依赖项的对象,那么这很明显表明您做错了.造成这种情况的一个常见原因是,您想要创建对象以管理其生命周期,或者因为它实际上是一个数据对象,而该对象恰好具有某些需要其他依赖项才能起作用的操作(例如,具有保存"功能的实体"方法).在第一种情况下,您根本不会那样做.您只需要依靠它,就可以让框架管理生命周期.如果生存期不正确,则应使用DI容器对其进行重新配置.

If you find yourself in need to create an object that has a dependency, then this is a clear sign that you are doing it wrong. A common reason for this is that you want to create the object in order to manage its lifetime, or because it is actually a data object that just happens to have some operations that needs other dependencies to work (e.g. an entity that has a "save" method). In the first case, you simply don’t do it like that. You just depend on it and let the framework manage the lifetime; if it has an incorrect lifetime, then you should reconfigure it with the DI container.

在后一种情况下,如果您的数据对象带有操作,则应将其拆分.您应该只具有一个数据对象,没有任何逻辑,然后注入一些能够为您对该数据对象执行操作的管理器服务.

In the latter case where you have a data object with operations, you should split this up. You should just have a data object, without any logic, and then inject some manager service that is able to perform the operation on that data object for you.

例如,在ASP.NET Core Identity中,您具有User对象,该对象只是一个普通实体,没有任何逻辑.为了例如添加用户角色或更改密码,则取决于您可以注入的用户管理器.因此User对象本身没有任何依赖关系.

For example in ASP.NET Core Identity, you have the User object which is just a normal entity without any logic. In order to e.g. add user roles or change the password, you rely on the user manager which you can inject. So the User object itself is without any dependencies.

我通常建议您阅读

I’d generally suggest you to read the dependency injection chapter of the ASP.NET Core documentation to understand how dependency injection works and how it is supposed to be used within the framework.

这篇关于如何创建没有构造函数参数且具有依赖项注入的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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