Dependency Injection in C++

Dependency Injection is a powerful software design technique promoting component-based design and enabling complete component logic isolation from initialization time boilerplate. As a useful consequence, components written for dependency injection have well defined interfaces, can be exhaustively tested with unit tests and can thus be used as generic architectural building block without the fear of exciting a new execution path (opposed to typically solid design of software systems written in a chaotic, "Jira-by-Jira" way). Java community can use dependency injection with minimum effort when using Spring Framework, whereas C++ community is traditionally ignorant to the maintenance and code reuse benefits so there is no single leader around. The article is a step-by-step how-to instruction on enabling dependency injection in C++ code on Linux.
Separate Bootstrapping and Business Logic
WIP
Separate Interface and Implementation
WIP
Use Dynamic Linking
WIP
Enforce Build Time Unresolved Symbols Check
The last, but not the least — build time hygiene. The Makefile used in the project is below. It demonstrates how to correctly build dynamic libraries on GNU Linux to make sure no runtime surprises with unresolved symbols ever occur.
CXX=g++
CPPFLAGS:=-MMD -I/home/bobah/work/cpplibs/boost_1_47_0
CXXFLAGS:=-m64 -fPIC
LDFLAGS:=-m64 -fPIC -Wl,-zdefs -Wl,-znow
LOADLIBES:=
LDLIBS:=

.PHONY: all
all: app libclient.so libmodule.so

.PHONY: clean
clean:
	/bin/rm -f app libclient.so libmodule.so

libmodule.so: module.cc
	$(CXX) -shared -Wl,-soname,$@.1 -o $@ $^ -m64 -fPIC -Wl,-zdefs -Wl,-znow 

libclient.so: client.cc
	$(CXX) -shared -Wl,-soname,$@.1 -o $@ $^ -m64 -fPIC -Wl,-zdefs -Wl,-znow 

app: app.cc
	$(CXX) -o $@ $(LDFLAGS) $^ -m64 -fPIC -Wl,-zdefs -Wl,-znow -ldl

-include $(patsubst %.cc,%.d,$(wildcard *.cc))

AttachmentSize
plugin.tgz1.94 KB