在C ++中的依赖注入 [英] Dependency injection in C++

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

问题描述

这也是我在MiškoHevery的一个评论中提出的问题



我不知道工厂/构建器如何连接依赖注入一起可以在C ++中工作。



我们有一个依赖于B的类A.构建器将在堆中分配B,在A的构造函数中传递一个指针给B,同时在堆中分配并返回一个指向A的指针。



谁后来清理?在完成之后,让构建器清理好,这很好吗?它似乎是正确的方法,因为在谈话中,它说,构建器应该设置对象,预期具有相同的生命周期或至少依赖有更长的生命周期(我也有一个问题)。我在代码中的意思:

 类构造器{
public:
builder b m_ClassA(NULL),m_ClassB(NULL){
}
〜builder(){
if(m_ClassB){
delete m_ClassB;
}
if(m_ClassA){
delete m_ClassA;
}
}
ClassA * build(){
m_ClassB = new class B;
m_ClassA = new class A(m_ClassB);
return m_ClassA;
}
};

现在如果有一个依赖关系预计持续时间超过我们注入的对象的生命周期我知道我们应该将构建方法更改为如下:

  ClassA * builder: :build(ClassC * classC){
m_ClassB = new class B;
m_ClassA = new class A(m_ClassB,classC);
return m_ClassA;
}

您首选的方法是什么?



在C ++中,我们尝试

以传递RAW指针。这是因为RAW指针没有与之相关联的所有权语义。如果你没有所有权,那么我们不知道是谁负责清理对象。



我发现大多数时间依赖注入是通过C ++ 。

在极少数情况下,您必须使用指针,将其包装在
std :: unique_ptr<> std :: shared_ptr<> ,具体取决于如何管理所有权。

如果您无法使用C ++ 11功能,请使用std :: auto_ptr< gt;或boost :: shared_ptr<>。



我还要指出,C ++和Java风格的编程现在是如此分歧,应用一种语言的风格到另一种语言将不可避免地导致灾难。


This is also a question that I asked in a comment in one of Miško Hevery's google talks that was dealing with dependency injection but it got buried in the comments.

I wonder how can the factory / builder step of wiring the dependencies together can work in C++.

I.e. we have a class A that depends on B. The builder will allocate B in the heap, pass a pointer to B in A's constructor while also allocating in the heap and return a pointer to A.

Who cleans up afterwards? Is it good to let the builder clean up after it's done? It seems to be the correct method since in the talk it says that the builder should setup objects that are expected to have the same lifetime or at least the dependencies have longer lifetime (I also have a question on that). What I mean in code:

class builder {
public:
    builder() :
        m_ClassA(NULL),m_ClassB(NULL) {
    }
    ~builder() {
        if (m_ClassB) {
            delete m_ClassB;
        }
        if (m_ClassA) {
            delete m_ClassA;
        }
    }
    ClassA *build() {
        m_ClassB = new class B;
        m_ClassA = new class A(m_ClassB);
        return m_ClassA;
    }
};

Now if there is a dependency that is expected to last longer than the lifetime of the object we are injecting it into (say ClassC is that dependency) I understand that we should change the build method to something like:

ClassA *builder::build(ClassC *classC) {
    m_ClassB = new class B;
    m_ClassA = new class A(m_ClassB, classC);
    return m_ClassA;
}

What is your preferred approach?

解决方案

This talk is about Java and dependency injection.

In C++ we try NOT to pass RAW pointers around. This is because a RAW pointer have no ownership semantics associated with it. If you have no ownership then we don't know who is responsible for cleaning up the object.

I find that most of the time dependency injection is done via references in C++.
In the rare cases where you must use pointers, wrap them in std::unique_ptr<> or std::shared_ptr<> depending on how you want to manage ownership.
In case you cannot use C++11 features, use std::auto_ptr<> or boost::shared_ptr<>.

I would also point out that C++ and Java styles of programming are now so divergent that applying the style of one language to the other will inevitably lead to disaster.

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

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