{"id":21177129,"url":"https://github.com/hamedmasafi/qinjection","last_synced_at":"2025-07-09T21:32:37.983Z","repository":{"id":70344524,"uuid":"293525024","full_name":"HamedMasafi/QInjection","owner":"HamedMasafi","description":"A simple dependency injection lib for Qt5","archived":false,"fork":false,"pushed_at":"2025-05-05T08:51:47.000Z","size":40,"stargazers_count":24,"open_issues_count":0,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-05T09:56:19.561Z","etag":null,"topics":["dependency","dependency-injection","dependency-manager","dependencyinjection","insert","macros","pool","qt","qt5","slot"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HamedMasafi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-09-07T12:42:02.000Z","updated_at":"2025-05-05T08:51:50.000Z","dependencies_parsed_at":"2025-05-05T09:52:21.814Z","dependency_job_id":"50047993-8551-4bb0-993c-5bd5d6fe80f2","html_url":"https://github.com/HamedMasafi/QInjection","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/HamedMasafi/QInjection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HamedMasafi%2FQInjection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HamedMasafi%2FQInjection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HamedMasafi%2FQInjection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HamedMasafi%2FQInjection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HamedMasafi","download_url":"https://codeload.github.com/HamedMasafi/QInjection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HamedMasafi%2FQInjection/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264503951,"owners_count":23618762,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency","dependency-injection","dependency-manager","dependencyinjection","insert","macros","pool","qt","qt5","slot"],"created_at":"2024-11-20T17:14:42.686Z","updated_at":"2025-07-09T21:32:37.977Z","avatar_url":"https://github.com/HamedMasafi.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Qt dependency injection lib\n\nQInjection is a dependency injection framework for Qt. It provides a way to automatically inject dependencies into classes, simplifying the process of creating and testing code.\n\n  - QInjection is a dependency injection framework for Qt.\n  - It has a simple and easy-to-use API.\n  - It is lightweight and does not add much overhead to your code.\n  - It can be used in any Qt project, regardless of the architecture or design pattern.\n  - You just need to add objects to the pool and they will be automatically injected into your code.\n\n## Quick start\nImagine we have two classes named _MyClass1_ and _MyClass2_ that we use them regularly in our app.\n\nThere are few ways to do that:\n\n1. Create them everywhere needed!\n2. Create an instance and pass it through object creation and function calls to different levels!\n3. Use an _Dependency Injection_ framework.\n\nThis repo that you are visiting is a simple way for the third solution\n \nThis library has a mechanism to store an object into a pool and retrieve it wherever needed. Let's take a look at a simple example:\n\nFirst: adding objects to the dependency pool. In the main method we are adding an instance of _MyClass1_ and _MyClass2_ to the _Pool_. \n```cpp\n#include \"myclass.h\"\n#include \"dependencypool.h\"\n\n#include \u003cQCoreApplication\u003e\n\nMyClass *createMyClass()\n{\n\tauto o = new MyClass;\n\t// o-\u003esetSomeProperty(someValue);\n\treturn o;\n}\n\nvoid foo(MyClass *object = QInjection::Inject)\n{\n\t// do something with object\n}\n\nint main(int argc, char *argv[])\n{\n\tQCoreApplication a(argc, argv);\n\n\tQInjection::addSingleton(createMyClass);\n\n\tfoo(); // \u003c- Empty arguments; the object will be fetched from dependency injection pool\n\treturn a.exec();\n}\n```\n\nAs you can see the object parameter of the method foo is not set; it will be fetched from the pool. Since MyClass is registered as a singleton, it will be the same object throughout the application code..\n\n## Api doc\n\n### Singleton vs Scopped\n\nSingleton objects are the same for entire application lifetime, scopped objects are created on every request.\n\n### Add an object to pool\n```cpp\ntemplate\u003cclass T\u003e\nvoid addSingleton();\n\ntemplate\u003cclass T\u003e\nvoid addSingleton(T *object);\n\ntemplate\u003cclass T\u003e\nvoid addSingleton(T*(*slot)() );\n\ntemplate\u003cclass _Owner, class T\u003e\nvoid addSingleton(_Owner *owner, void (_Owner::*slot)(T *));\n\ntemplate\u003cclass T\u003e\nvoid addScopped(T *(*slot)())\n\ntemplate\u003cclass _Owner, class T\u003e\nvoid addScopped(_Owner *owner, void (_Owner::*slot)(T *));\n```\n\nExamples:\n```cpp\n\nMyAnotherClass *createMyAnotherClass()\n{\n    auto o = new MyAnotherClass;\n    // set some properties for object o\n    return o;\n}\n\nint main(int argc, char *argv[])\n{\n\n    QCoreApplication a(argc, argv);\n\n    auto obj = new MyClass;\n\n    QInjection::addSingleton(obj); // pass object to dependency pool\n    QInjection::addSingleton(createMyAnotherClass); // pass function pointer to dependency pool\n\n    return a.exec();\n}\n\t\n```\n\n### Get object\n**There are some methods to get an object from the pool**\n\nThe _QInjection::Pointer_ class is the main method of fetching object from the pool. It works like _QPointer_. It will delete it's content if the registration type is _scopped_. \n\n```cpp \nvoid myMethod2()\n{\n\tQInjection::Pointer\u003cMyclass\u003e object;\n\t// This is where the object will be taken from the pool. \n\t// If it's registration type is of a scopped type, it'll be deleted after the end.\n}\n```\n\nAnother method is using _QInjection::Inject_. It is Efficient if used in function parameters. In the example below you may pass an object of _MyClass_ type, if not it will be taken from the dependency injection pool.\n```cpp \nvoid myMethod(Myclass *object = QInjection::Inject)\n{\n\t// You can pass a object of type MyClass to this method. If you don't, the object will be taken from QInjection pool\n}\n```\n\n### Object change notify\n\nSometimes we need to check when an object is being added or removed from the _DependencyInjection_ class. for that purpose, we have the _registerObjectNotify_ method in this class:\n\n```cpp\nQInjection-\u003eadd\u003cInterface\u003e();\n\n...\n\nQInjection-\u003eregisterObjectNotify(this, \u0026MainWindow::interface_changed);\n```\nAnd in header we have a slot for this:\n\n```cpp\npublic slots:\n\tvoid interface_changed(Interface *project);\n```\nSo when an object of type _Interface_ is going to be added or removed from DependencyInjection, this slot will be called. Note that when object gets removed from DependencyInjection, the parameter of this method will be _null_ptr_\n\nAlternatively, this method can take a lambda:\n\n```cpp\ndep-\u003eregisterObjectNotify\u003cInterface\u003e(this, [this](interface *interface) {\n\tif (interface)\n\t\tqDebug() \u003c\u003c \"Interface removved from pool\";\n\telse\n\t\tqDebug() \u003c\u003c \"Interface added to pool\";\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhamedmasafi%2Fqinjection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhamedmasafi%2Fqinjection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhamedmasafi%2Fqinjection/lists"}