A singleton to prepare services for dependency injection and to access them. More...
#include <serviceregistry.h>
Public Member Functions | |
template<typename TInterface > | |
bool | isRegistered () const |
Checks if a given interface or service is already registered. | |
bool | isRegistered (const QByteArray &iid) const |
Checks if a given interface or service is already registered. | |
template<typename TInterface , typename TService > | |
void | registerInterface (bool weak=false) |
Register a service for its interface via the type. More... | |
template<typename TInterface , typename TService , typename TFunc > | |
void | registerInterface (const TFunc &fn, bool weak=false) |
Register a service for its interface via a constructor function. More... | |
template<typename TInterface , typename TService > | |
void | registerInterface (TService *service, bool weak=false) |
Register a service for its interface via an already existing instance. More... | |
template<typename TService > | |
void | registerObject (bool weak=false) |
Register a service via its type. More... | |
template<typename TService , typename TFunc > | |
void | registerObject (const TFunc &fn, bool weak=false) |
Register a service via a constructor function. More... | |
template<typename TService > | |
void | registerObject (TService *service, bool weak=false) |
Register a service via an already existing instance. More... | |
void | registerService (const QByteArray &iid, const QMetaObject *metaObject, bool weak=false) |
Register a service by an iid via their metadata. More... | |
void | registerService (const QByteArray &iid, const std::function< QObject *(const QObjectList &)> &fn, QByteArrayList injectables, bool weak=false) |
Register a service by an iid via a generalized constructor function. More... | |
template<typename TInterface > | |
TInterface * | service () |
Returns the service for the given interface. More... | |
QObject * | serviceObj (const QByteArray &iid) |
Returns the service for the given iid. More... | |
void | injectServices (QObject *object) |
Inject services for all injectable properties on object. More... | |
template<typename TClass > | |
TClass * | constructInjected (QObject *parent=nullptr) |
Constructs a new instance of TClass with properties injected. More... | |
QObject * | constructInjected (const QMetaObject *metaObject, QObject *parent=nullptr) |
Constructs a new instance of metaObject with properties injected. More... | |
Static Public Member Functions | |
static ServiceRegistry * | instance () |
Returns the ServiceRegistry singleton instance. | |
A singleton to prepare services for dependency injection and to access them.
This is the class responsible for the dependency injection (DI) part. All services registered with the registry are available for DI. When using lazy initialization, services themselves can also have dependencies to be injected. When the registry creates them it will automatically inject them. Automatic DI is also done for ViewModel created by the CoreApp.
qtmvvm_init()
it is called automatically by the registry right after all dependant services have been injected.The following example shows how to use DI. The first shows how to use it with a simple, interface-less service for an already created object:
The second example shows how to use it for interface based services and on objects that are created via the registry:
Definition at line 17 of file serviceregistry.h.
QtMvvm::ServiceRegistry::constructInjected | ( | QObject * | parent = nullptr | ) |
Constructs a new instance of TClass with properties injected.
TClass | The type of object to be created, must extend QObject |
parent | The parent object of the created object |
ServiceConstructionException | If the registry failed to create an instance that needs to be injected into the object |
First the method creates a new instance of TClass, then loads all services that are needed to inject into properties of the object that are marked for injection via QTMVVM_INJECT. If any lazy service has not been created yet, it is created on the fly and then injected. The returned object is parented to the parent object. If that is null it is owned by the caller.
Q_INVOKABLE explicit TClass(QObject *parent = nullptr);
Definition at line 219 of file serviceregistry.h.
QtMvvm::ServiceRegistry::constructInjected | ( | const QMetaObject * | metaObject, |
QObject * | parent = nullptr |
||
) |
Constructs a new instance of metaObject with properties injected.
metaObject | The metaobject of object type to be created, must extend QObject |
parent | The parent object of the created object |
ServiceConstructionException | If the registry failed to create an instance that needs to be injected into the object |
First the method creates a new instance of metaObject, then loads all services that are needed to inject into properties of the object that are marked for injection via QTMVVM_INJECT. If any lazy service has not been created yet, it is created on the fly and then injected. The returned object is parented to the parent object. If that is null it is owned by the caller.
Q_INVOKABLE explicit TClass(QObject *parent = nullptr);
QtMvvm::ServiceRegistry::injectServices | ( | QObject * | object | ) |
Inject services for all injectable properties on object.
object | The object to inject properties into |
ServiceConstructionException | If the registry failed to create an instance that needs to be injected into the object |
Loads all services that are needed to inject into properties of the object that are marked for injection via QTMVVM_INJECT. If any lazy service has not been created yet, it is created on the fly and then injected.
QtMvvm::ServiceRegistry::registerInterface | ( | bool | weak = false | ) |
Register a service for its interface via the type.
TInterface | The interface type to register the service for |
TService | The service to be registered for that interface. Must extend QObject and implement TInterface |
weak | Specifies if the registration should be a weak one or a normal one |
ServiceExistsException | If a non-weak service has already been registered for TInterface |
If the function returns successfully, from now on a service of the given type can be accessed via the registry for the interface. The service is lazy initialized an will be created as soon as it is requested for the first time. If it has any injectable properties, they will be automatically injected on construction.
If the service is registered as weak, registering another service for the same interface will not throw an exception but instead discard (and delete) this one.
Q_INVOKABLE explicit TService(QObject *parent = nullptr);
Definition at line 155 of file serviceregistry.h.
QtMvvm::ServiceRegistry::registerInterface | ( | const TFunc & | fn, |
bool | weak = false |
||
) |
Register a service for its interface via a constructor function.
TInterface | The interface type to register the service for |
TService | The service to be registered for that interface. Must extend QObject and implement TInterface |
TFunc | The function type of the fn parameter. |
fn | The function to be called to construct the service |
weak | Specifies if the registration should be a weak one or a normal one |
ServiceExistsException | If a non-weak service has already been registered for TInterface |
If the function returns successfully, from now on a service of the given type can be accessed via the registry for the interface. The service is lazy initialized an will be created as soon as it is requested for the first time. It is created by calling the given fn
function.
fn
must have the following signature:
TService is the template parameter of this function. The arguments are variable, but if arguments are present, they all must be pointers of types that are injectable via the registry. When the function is called internally, the registry will "inject" all services as parameters of this function.
If the service is registered as weak, registering another service for the same interface will not throw an exception but instead discard (and delete) this one.
Definition at line 162 of file serviceregistry.h.
QtMvvm::ServiceRegistry::registerInterface | ( | TService * | service, |
bool | weak = false |
||
) |
Register a service for its interface via an already existing instance.
TInterface | The interface type to register the service for |
TService | The service to be registered for that interface. Must extend QObject and implement TInterface |
service | The service instance to be registered as service |
weak | Specifies if the registration should be a weak one or a normal one |
ServiceExistsException | If a non-weak service has already been registered for TInterface |
If the function returns successfully, from now on a service of the given type can be accessed via the registry for the interface. The service instance is from that point on owned by the registry. No DI is performed on the passed service.
If the service is registered as weak, registering another service for the same interface will not throw an exception but instead discard (and delete) this one.
Definition at line 171 of file serviceregistry.h.
QtMvvm::ServiceRegistry::registerObject | ( | bool | weak = false | ) |
Register a service via its type.
TService | The service to be registered by its own type. Must extend QObject |
weak | Specifies if the registration should be a weak one or a normal one |
ServiceExistsException | If a non-weak service has already been registered for TService |
If the function returns successfully, from now on a service of the given type can be accessed via the registry. The service is lazy initialized an will be created as soon as it is requested for the first time. If it has any injectable properties, they will be automatically injected on construction.
If the service is registered as weak, registering another service for the same TService will not throw an exception but instead discard (and delete) this one.
Q_INVOKABLE explicit TService(QObject *parent = nullptr);
Definition at line 185 of file serviceregistry.h.
QtMvvm::ServiceRegistry::registerObject | ( | const TFunc & | fn, |
bool | weak = false |
||
) |
Register a service via a constructor function.
TService | The service to be registered by its own type. Must extend QObject |
TFunc | The function type of the fn parameter. |
fn | The function to be called to construct the service |
weak | Specifies if the registration should be a weak one or a normal one |
ServiceExistsException | If a non-weak service has already been registered for TService |
If the function returns successfully, from now on a service of the given type can be accessed via the registry. The service is lazy initialized an will be created as soon as it is requested for the first time. It is created by calling the given fn
function.
fn
must have the following signature:
TService is the template parameter of this function. The arguments are variable, but if arguments are present, they all must be pointers of types that are injectable via theregistry. When the function is called internally, the registry will "inject" all services as parameters of this function.
If the service is registered as weak, registering another service for the same TService will not throw an exception but instead discard (and delete) this one.
Definition at line 192 of file serviceregistry.h.
QtMvvm::ServiceRegistry::registerObject | ( | TService * | service, |
bool | weak = false |
||
) |
Register a service via an already existing instance.
TService | The service to be registered by its own type. Must extend QObject |
service | The service instance to be registered as service |
weak | Specifies if the registration should be a weak one or a normal one |
ServiceExistsException | If a non-weak service has already been registered for TService |
If the function returns successfully, from now on a service of the given type can be accessed via the registry. The service instance is from that point on owned by the registry. No DI is performed on the passed service.
If the service is registered as weak, registering another service for the same TService will not throw an exception but instead discard (and delete) this one.
Definition at line 201 of file serviceregistry.h.
QtMvvm::ServiceRegistry::registerService | ( | const QByteArray & | iid, |
const QMetaObject * | metaObject, | ||
bool | weak = false |
||
) |
Register a service by an iid via their metadata.
iid | The interface id of the type to register the service for |
metaObject | The metaobject of the service to be registered for that interface. Must extend QObject and implement the interface of iid , unless it is the id of the service itself |
weak | Specifies if the registration should be a weak one or a normal one |
ServiceExistsException | If a non-weak service has already been registered for the iid |
If the function returns successfully, from now on a service of the given metobject can be accessed via the registry for the iid. The service is lazy initialized an will be created as soon as it is requested for the first time. If it has any injectable properties, they will be automatically injected on construction.
If the service is registered as weak, registering another service for the same iid will not throw an exception but instead discard (and delete) this one.
Q_INVOKABLE explicit Service(QObject *parent = nullptr);
QtMvvm::ServiceRegistry::registerService | ( | const QByteArray & | iid, |
const std::function< QObject *(const QObjectList &)> & | fn, | ||
QByteArrayList | injectables, | ||
bool | weak = false |
||
) |
Register a service by an iid via a generalized constructor function.
iid | The interface id of the type to register the service for |
fn | The function to be called to construct the service |
injectables | The iids of the parameters to be passed to fn |
weak | Specifies if the registration should be a weak one or a normal one |
ServiceExistsException | If a non-weak service has already been registered for the iid |
If the function returns successfully, from now on a service of the given type can be accessed via the registry for the interface. The service is lazy initialized an will be created as soon as it is requested for the first time. It is created by calling the given fn
function.
The function must return an object that implements the interface of iid
, unless it is the id of the service itself. The arguments are passed as object list and are determined by the injectables
parameter. That is a list of iids. When the function is called internally, the registry will "inject" all services found by the iids as parameters of this function, comnined to a list of objects.
If the service is registered as weak, registering another service for the same iid will not throw an exception but instead discard (and delete) this one.
QtMvvm::ServiceRegistry::service | ( | ) |
Returns the service for the given interface.
TInterface | The interface type get an instance of |
ServiceConstructionException | If the registry failed to create an instance for that interface |
If the service is lazy inizialized and not created yet, the registry will do so. The returned service is owned by the registry. Be careful with weak services, as they may be deleted at any time. For normal services, this is never the case.
Definition at line 213 of file serviceregistry.h.
QtMvvm::ServiceRegistry::serviceObj | ( | const QByteArray & | iid | ) |
Returns the service for the given iid.
iid | The interface id of the type to get an instance of |
ServiceConstructionException | If the registry failed to create an instance for that interface |
If the service is lazy inizialized and not created yet, the registry will do so. The returned service is owned by the registry. Be careful with weak services, as they may be deleted at any time. For normal services, this is never the case.