依赖注入在C ++中有用吗 [英] Is dependency injection useful in C++

查看:88
本文介绍了依赖注入在C ++中有用吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#大量使用 依赖注入(DI) 来使 无损 可测试 平台。为此,我需要一个 界面 ,也许需要一个 DI 控制反转(IoC)容器用于解决我的实例。



但是您如何使用C ++做到这一点?似乎C ++中的依赖注入并不像C#中那么重要。在C ++中,您使用对对象的引用-这是在C ++中使用DI的方法,对吗?



如果我的参考文献理论正确,是否有类似容器的东西可以解析所有参考文献?在C#中,我有一个 错误的类/错误的项目/程序集 ,它在程序启动时将所有实例注册到一个静态容器中。然后,在每个类中,我都可以实例化静态容器并可以解析特定的实例,这在C ++中可能吗?



您是否正在使用Dependency C ++中的注入(或所谓的注入)?如果是,您如何使用它?

解决方案


为此,我需要一个接口并也许是用于解决我的实例的容器。但是,如何用C ++做到这一点呢?


用同样的方式。不同之处在于,在C#中编程为接口时,在C ++中编程为基类。此外,您还拥有C ++中没有的其他工具(例如,基于策略的模板实现了在编译时选择的依赖注入)。


在C ++中,您使用对对象的引用,这是在C ++中使用DI的方法,对吗?


否;这不是使用DI的 方法,这是在C ++中使用DI的 a 方法。






  • 使用指向对象的指针(或智能指针,具体取决于情况)

  • 为策略使用模板参数(例如,请参阅std :: default_delete在智能指针中使用)

  • 将lambda calcullus与注入的函子/谓词一起使用。




在C#中,我有一个错误的类/错误的项目/程序集,它在程序启动时将我的所有实例注册到一个静态容器中


如果我正确理解,您可以在此静态容器中设置所有数据,并在整个应用程序中使用它们。如果是这种情况,那么您将无法正确使用依赖项注入,因为这会违反Demeter定律。


在C ++中这可能吗?


是的,这是完全有可能的(但您不应该这样做,因为它违反了Demeter的定律)。看一下boost :: any(这将允许您将异构对象存储在容器中,类似于通过C#中的 object 引用存储对象)。


您在使用依赖注入还是在C ++中调用它?


是的(这被称为依赖注入:))。


如果是,您将如何使用它?


如上所述(策略模板参数,注入的函子和谓词作为可重用组件,通过引用,指针智能指针或值注入对象)。 / p>

C# uses Dependency Injection (DI) a lot to have a lossless and testable platform. For this, I need an interface and maybe a DI or Inversion of Control (IoC) container for resolving my instances.

But how do you do this in C++? I've read a little bit about this, and it seems that dependency injection in C++ isn't as big a topic as in C#. In C++ you use a reference to an object - this is the way to use DI in C++, right?

If my theory with references are correct, is there something like a container where I can resolve all the references? In C# I have a "bad class/bad project/assembly" which registers all my instances into a static container at the program start. Then, in every class, I'm able to instance the static container and can resolving a specific instance, is this possible in C++?

Are you using Dependency Injection (or whatever it is called) in C++? If yes, how you're use it? Are there similarities to C#?

解决方案

For this, I need an interface and maybe a container for resolving my instances. But how you do this in C++?

In the same way. The difference is that where you "program to an interface" in C#, you "program to a base class" in C++. Additionally, you have extra tools in C++ that you do not have in C# (for example, policy-based templates implement dependency injection chosen at compilation time).

In C++ you're use a reference to an object, this is the way to use DI in C++, right?

No; this is not the way to use DI, this is a way to use DI in C++.

Also consider:

  • use a pointer to an object (or smart pointer, depending on the case)
  • use a template argument for a policy (for an example, see std::default_delete use in smart pointers)
  • use lambda calcullus with injected functors/predicates.

In C# I've a "bad class/bad project/assembly" which register all my instance into a static container at the program start.

If I understand correctly, you set all your data in this static container and use it all over the application. If this is the case, then you do not use dependency injection correctly, because this breaks Demeter's Law.

is this possible in C++?

Yes, it is perfectly possible (but you shouldn't do it, due to it breaking Demeter's law). Have a look at boost::any (this will allow you to store heterogenous objects in a container, similar to storing objects by object reference in C#).

Are you using dependency injection or whatever it is called in C++?

Yes (and it is called dependency injection :) ).

If yes, how you're use it?

As I described above (policy template arguments, injected functors and predicates as reusable components, injecting objects by reference, pointer smart pointer or value).

这篇关于依赖注入在C ++中有用吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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