Best practices for dependency injection via constructor
Asked Answered
L

2

16

Inversion of control is a value-proof technique which is used to modularize a system and decouple the components from each other.

Low coupling is always an advantage: it simplifies automatic testing of the components and makes the code better conforming to single responsibility principle.

Among the ways to declare a dependency to another class (service locator, property injection calling a public method / setting a public property...), the constructor injection seems the best approach.

Though it's probably the most difficult one (at least from the listed three) to implement, it comes with significant advantages:

  • all the dependencies are truly visible with constructor signature;
  • cyclic dependencies don't happen because of the well-defined order of instantiation.

What are the pros / cons of the many choices C++ offers to perform the injection via constructor?

Landahl answered 1/9, 2016 at 9:12 Comment(0)
L
22

Instance copyable class

class object
{
public:
  object(dependency d) : dep_(d) {}

private:
  dependency dep_;
};

Only works in case dependency class is completely stateless, i.e. doesn't have any members. Practically, this rarely happens because dependency class may store its own dependency.

Raw pointer

class object
{
public:
  object(dependency *d) : dep_(d)
  {
    if (d == nullptr)
      throw std::exception("null dependency");
  }

private:
  dependency *dep_;
};

This works like true injection. We're required to check the passed pointer for nullptr value.

object class does not own dependency class, thus it's the responsibility of calling code to make sure the object is destroyed before the dependency object.

In real application, it's sometimes very difficult to validate.

Reference

#define DISALLOW_COPY_AND_ASSIGN(Class) \
  Class(const Class &) = delete;        \
  Class &operator=(const Class &) = delete

class object
{
public:
  object(dependency &d) : dep_(d) {}

  DISALLOW_COPY_AND_ASSIGN(object);

private:
  dependency &dep_;
};

The reference cannot be null, so it's a bit safer in this prospective.

However this approach brings additional constraints to object class: it has to be non-copyable since a reference cannot be copied. You have to either manually override assignment operator and copy constructor to stop from copying or inherit it from something like boost::noncopyable.

Like with raw pointer, the ownership constraint is in place. Calling code should provide the correct destruction order for both classes, otherwise the reference becomes invalid and application crashes with access violation.

If the dependency is a const reference:

class object
{
public:
  object(const dependency &d) : dep_(d) {}

private:
  const dependency &dep_;
};

you should pay attention to the fact that the object class accepts references to temporary objects:

dependency d;
object o1(d);             // this is ok, but...

object o2(dependency());  // ... this is BAD.

Further details:

Smart pointer

class object
{
public:
  object(std::shared_ptr<dependency> d) : dep_(d)
  {
    if (!d)
      throw std::exception("null dependency");
  }

private:
  std::shared_ptr<dependency> dep_;
};

Similar to raw pointer but the ownership is controlled by smart pointer mechanism.

Still need to check for nullptr in the constructor body.

The major advantage is the dependency object lifetime control: there is no need for the calling application to properly control the destruction order (but consider that you need to be very careful when designing your APIs with std::shared_ptr).

Once the dependency class is no longer used it's automatically destroyed by shared_ptr destructor.

There are cases when shared_ptr owned objects are not destroyed (so called cyclic references). However, with constructor injection, cyclic dependencies aren't possible due to the specific well-defined order of construction.

This works of course if no other injection methods are used across the application.

A smart pointer has a small overhead but it isn't a real problem in the majority of cases.

Further details:

Landahl answered 1/9, 2016 at 9:12 Comment(6)
"Only works in case dependency class is completely stateless, i.e. doesn't have any members." Could you explain that?Loehr
The one useful version is missing: std::unique_ptr<dependency>.Romanic
What's the point in posting an answer together with a question?Gaillardia
I would suggest std::shared_ptr<const dependency> over the non-const version.Loehr
@Loehr I've changed the answer to community wiki. It should be improved in many ways (please feel free to correct my mistakes). I needed a starting point to collect/document several techniques.Landahl
@KerrekSB I've changed the answer to community wiki. It should be improved in many ways (please feel free to correct my mistakes). I needed a starting point to collect/document several techniques.Landahl
B
2

This is an old question but for me this is a hot topic because I've found automatic dependency injection sorceries in all web the framewrks I could hear of, they are often built with introspection shananigans and I always have great time discovering their implementations. But I couldn't find an easy way to do the same in C++.

The service locator approach can solve the problem pretty well indeed but declaring the dependencies in the constructor and getting rid of such pattern in between seems cleaner and more flexible to use because it is easier to instantiate your classes passing different instances of your services.

But the service locator approach can also handle cyclic dependencies because they can be lazily picked, and sometimes cyclic dependencies can happen (maybe in bad code only).

Unfortunately I haven't figured out way to detect the types of the arguments in constructors and automatically inject instances of such types, yet.

Anyway I want to share the best solution I found so far to automatically inject deendencies in classes. It is similar to a service locator that handles its service as a singleton with smart pointers and can be used for dependency injection, but it have to be revised to allow two classes that have some dependencies in common to get different instances of the same type.

template<typename T>
struct di_explicit
{
    static std::shared_ptr<T> ptr;

    virtual ~di_explicit()
    {
        if(di_explicit<T>::ptr.use_count() == 1) {
            reset();
        }
    }

    virtual std::shared_ptr<T> get()
    {
        return di_explicit<T>::ptr;
    }

    static void reset()
    {
        di_explicit<T>::ptr.reset();
    }

    static void swap(std::shared_ptr<T> arg)
    {
        arg.swap(di_explicit<T>::ptr);
    }

    static void emplace(auto && ... args)
    {
        swap(std::make_shared<T>(std::forward(args) ...));
    }

    static void emplace_if_not_exists(auto && ... args)
    {
        if(!di_explicit<T>::ptr) {
            emplace(std::forward(args) ...);
        }
    }
};

template<typename T>
std::shared_ptr<T> di_explicit<T>::ptr {};

template<typename T>
struct di : di_explicit<T>
{
    di(auto && ... args)
    {
        di_explicit<T>::emplace_if_not_exists(std::forward(args) ...);
    }
};

template<typename T>
struct di_lazy : di_explicit<T>
{
    auto get(auto && ... args)
    {
        di_explicit<T>::emplace_if_not_exists(std::forward(args) ...);
        return di_explicit<T>::ptr;
    }
};

The ideas behind the above snippet are:

It is a logic wrapper that handles the memory of another class, such wrapper is able to automatically create an instance of the managed class and pass the reference as a singleton when requested, the memory is automatically deallocated when there are no more reference to the managed object.

It is possible to use a specific instance of the managed class (or a subtype) so that the user can declare a dependency to an interface of the needed service and instanciate the concrete dependency when the program is running or a mock during tests.

In case of circular dependency there is a way to lazily instanciate the needed dependency.

The basic logic is coded in the base class di_explicit<T> that uses a static shared_ptr<T> to make the singletons, and a destructor that resets the shared pointer when the last reference left is the static one (stored in di_explicit<T>).

The struct di : di_explicit<T> retrive the dependency in its constructor while di_lazy : di_explicit<T> only does it when the dependency is requested (in the get() method).

The following is an example (non lazy) with a mock.

namespace {
    struct dependency {
        virtual void do_something() {
            std::cout << "doing something" << std::endl;
        }
    };

    struct mock : dependency {
        using dependency::do_something;
        void do_something() {
            std::cout << "mocking something" << std::endl;
        }
    };

    struct srv {
        di<dependency> dep;
        void do_stuff() {
            std::cout << "doing stuff" << std::endl;
            return dep.get()->do_something();
        }
    };

    int test = [](){
        // the classes are not instanciated yet
        std::cout << "ptr exists " << !!di<srv>::ptr << std::endl;
        {
            // the classes instanciated here
            di<srv> s;
            s.get()->do_stuff();
            std::cout << "ptr exists " << !!di<srv>::ptr << std::endl;
        } // <- the instances are destroyed here
        std::cout << "ptr exists " << !!di<srv>::ptr << std::endl;
        
        {
            // use a mock instance
            di_explicit<dependency>::swap(std::make_shared<mock>());
            di<srv>{}.get()->do_stuff();
        } // <- the mock is destroyed here too
        std::cout << "ptr exists " << !!(di<dependency>::ptr) << std::endl;
       
        return 0;
    }();
}

The following is an example with circular references and di_lazy.

namespace {
    struct dep_2;
    struct dep_3;

    struct dep_1 {
        di_lazy<dep_2> dep;

        void do_something();
    };

    struct dep_2 {
        di_lazy<dep_3> dep;

        void do_something();
    };

    struct dep_3 {
        di_lazy<dep_1> dep;

        void do_something() {
            std::cout << "dep_3 do_something" << std::endl;
            dep.get()->do_something();
        }

        virtual void do_something_else() {
            std::cout << "dep_3 do_something_else" << std::endl;
        }
    };

    void dep_1::do_something() {
        std::cout << "dep_1 do_something" << std::endl;
        dep.get()->do_something();
    }

    void dep_2::do_something() {
        std::cout << "dep_2 do_something" << std::endl;
        dep.get()->do_something_else();
    }

    struct srv_2 {
        di<dep_3> dep;
        void do_something() {
            std::cout << "srv_2 do_something" << std::endl;
            return dep.get()->do_something();
        }
    };

    int result = [](){        
        {
            // neither the dependencies or the service are requested yet
            di_lazy<srv_2> wrapper{};

            // here the service is requested
            auto s = wrapper.get(); 

            // dependencies are requested inside this function
            s->do_something();
        }

        
        {
            struct mock_dep_3 : dep_3 {
                virtual void do_something_else() {
                    std::cout << "dep_3 do_something_else MOCKED!" << std::endl;
                }
            };
            // a mock can be used with di_lazy as well
            di_explicit<dep_3>::swap(std::make_shared<mock_dep_3>());
            di<srv_2>{}.get()->do_something();
        }
        return 0;
    }();
}

I know there is room for improvements (any sugestion are appreciated), I hope you find it useful

EDIT

I found a sligly better way to do the same but this time extending the std::shared_ptr class itself.

It is still some kind of service locator but with the following snippet is also possible to pass shared pointers as arguments in your constructors

template<typename T>
class di : public std::shared_ptr<T>
{
    static std::shared_ptr<T> ptr;

public:
    static void reset()
    {
        di<T>::ptr.reset();
    }

    static di<T> replace(std::shared_ptr<T> ptr)
    {
        di<T>::ptr = ptr;
        return di<T>::ptr;
    }

    template<typename ... args_t>
    static di<T> emplace(args_t && ... args)
    {
        return di<T>::replace(std::make_shared<T>(
            std::forward<args_t>(args) ...
        ));
    }

    static di<T> instance()
    {
        return di<T>::ptr;
    }

    ~di()
    {
        if(this->is_linked() && di<T>::ptr.use_count() <= 2){
            di<T>::ptr.reset();
        }
    }

    bool is_linked()
    {
        return *this && di<T>::ptr.get() == this->get();
    }

    template<typename ... args_t>
    di(args_t && ... ptr) : std::shared_ptr<T>(std::forward<args_t>(ptr) ...)
    {}
};

template<typename T>
std::shared_ptr<T> di<T>::ptr {};

With this class you can pass the instance of some service to another using constructor

ie

struct logger_interface
{
    virtual void log(std::string) = 0;
    virtual ~logger_interface() = default;
};

struct some_service_interface
{
    virtual void serve() = 0;
    virtual ~some_service_interface() = default;
};

struct logger_with_id : logger_interface
{
    static int counter;
    int id = ++counter;
    void log(std::string s) {
        std::cout << id << ") " << s << std::endl;
    }
};
int logger_with_id::counter = 0;

struct some_service : some_service_interface
{
    di<logger_interface> logger;

    some_service(
        di<logger_interface> logger = di<logger_interface>::instance()
    ) :
        logger(logger)
    {}

    void serve() {
        logger->log("serving...");
    }
};

int app = []() {
    di<logger_interface>::replace(di<logger_with_id>::emplace());
    di<some_service_interface>::replace(di<some_service>::emplace());
    std::cout << "running app"<< std::endl;
    di<logger_interface>::instance()->log("app");
    di<some_service_interface>::instance()->serve();
    std::cout << std::endl;
    return 0;
}();

Will print

running app
1) app
1) serving...

And if you need you can override the dependency for some service

struct decorated_logger : logger_interface {
    di<logger_interface> logger;
    decorated_logger(
        di<logger_interface> logger = di<logger_interface>::instance()
    ) :
        logger(logger)
    {}
    void log(std::string s) {
        logger->log("decorating...");
        logger->log(s);
    }
};

int app_with_custom_logger_on_service = [](
    di<logger_interface> logger,
    di<some_service_interface> service
) {
    std::cout << "running app_with_custom_logger_on_service"<< std::endl;
    logger->log("app");
    service->serve();
    std::cout << std::endl;
    return 0;
}(
    di<logger_interface>::replace(std::make_shared<logger_with_id>()),
    di<some_service_interface>::replace(std::make_shared<some_service>(
        std::make_shared<decorated_logger>(std::make_shared<logger_with_id>())
    ))
);

Will print

running app_with_custom_logger_on_service
2) app
3) decorating...
3) serving...

This can also be used for tests

struct mock_logger : logger_interface {
    void log(std::string) {
        std::cout << "mock_logger" << std::endl;
    }
};

struct mock_some_service : some_service_interface {
    void serve() {
        std::cout << "mock_some_service" << std::endl;
    }
};

int test = [](
    di<logger_interface> logger,
    di<some_service_interface> service
) {
    std::cout << "running test"<< std::endl;
    logger->log("app");
    service->serve();
    std::cout << std::endl;
    return 0;
}(
    di<logger_interface>::replace(std::make_shared<mock_logger>()),
    di<some_service_interface>::replace(std::make_shared<mock_some_service>())
);

Will print

running test
mock_logger
mock_some_service

I made a gist for this example, you can run it on wandbox with clang

Brittne answered 21/11, 2022 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.