{"id":21420847,"url":"https://github.com/lord-turmoil/minioc","last_synced_at":"2026-02-08T22:01:53.349Z","repository":{"id":194517876,"uuid":"691005475","full_name":"Lord-Turmoil/MinIoC","owner":"Lord-Turmoil","description":"MinIoC is a lightweight header-only IoC container for C++","archived":false,"fork":false,"pushed_at":"2024-12-21T12:45:25.000Z","size":107,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-19T16:41:45.617Z","etag":null,"topics":["cpp","header-only","ioc","ioc-container"],"latest_commit_sha":null,"homepage":"https://www.tonys-studio.top/posts/MinIoC-A-Miniature-IoC-Container-in-C/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lord-Turmoil.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2023-09-13T10:10:46.000Z","updated_at":"2024-12-21T12:47:55.000Z","dependencies_parsed_at":"2025-01-23T06:28:29.501Z","dependency_job_id":"855561f9-2b54-4342-96b9-d0a41ccf77fb","html_url":"https://github.com/Lord-Turmoil/MinIoC","commit_stats":null,"previous_names":["lord-turmoil/minioc"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/Lord-Turmoil/MinIoC","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lord-Turmoil%2FMinIoC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lord-Turmoil%2FMinIoC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lord-Turmoil%2FMinIoC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lord-Turmoil%2FMinIoC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lord-Turmoil","download_url":"https://codeload.github.com/Lord-Turmoil/MinIoC/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lord-Turmoil%2FMinIoC/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29246420,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-08T21:42:34.334Z","status":"ssl_error","status_checked_at":"2026-02-08T21:41:38.468Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["cpp","header-only","ioc","ioc-container"],"created_at":"2024-11-22T20:22:50.521Z","updated_at":"2026-02-08T22:01:53.275Z","avatar_url":"https://github.com/Lord-Turmoil.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mini IoC\r\n\r\nA mini IoC implementation with C++.\r\n\r\n\u003e ## Reference\r\n\u003e\r\n\u003e The implementation of this IoC is based on [A Miniature IOC Container in C++](https://www.codeproject.com/Articles/1029836/A-miniature-IOC-Container-in-Cplusplus).\r\n\u003e\r\n\u003e Some necessary modifications have been made to the source code on the website, and here is the original License: [The Code Project Open License (CPOL)](https://www.codeproject.com/info/cpol10.aspx)\r\n\r\n---\r\n[![Build](https://github.com/Lord-Turmoil/MinIoc/actions/workflows/cmake-multi-platform.yml/badge.svg?branch=main)](https://github.com/Lord-Turmoil/MinIoc/actions/workflows/cmake-multi-platform.yml)\r\n\r\n## 1. Overview\r\n\r\nThis mini IoC container provides basic adding and resolving abilities and the simplest dependency injection. It provides two lifetimes - singleton and transient. Singleton object will be created only once, while transient object will be constructed every time it is resolved. What's more, it can provide lazy initialization for singleton instances. 😁\r\n\r\nSince everything is implemented with C++ template, only header files are located under the `mioc/include` directory. A test file, `Test.cpp` is placed under the `src/` directory as an example.\r\n\r\nTo avoid pointer problems, `std::shared_ptr` is used everywhere to wrap all native pointers. 🙂\r\n\r\nIt will be very nice for you to light up the 🌟. :)\r\n\r\n## 2. Usage\r\n\r\n### 2.1 Test Classes\r\n\r\nHere are the classes we use in this demonstration. Interfaces are omitted as they are straightforward. We can see that B depends on A, and C depends on B. 🧐\r\n\r\n```cpp\r\nclass A : public IA\r\n{\r\npublic:\r\n    A() = default;\r\n};\r\n\r\nclass B : public IB\r\n{\r\npublic:\r\n    B(const std::shared_ptr\u003cIA\u003e\u0026 a) :_a(a) {}\r\nprivate:\r\n    std::shared_ptr\u003cIA\u003e _a;\r\n};\r\n\r\nclass C : public IC\r\n{\r\npublic:\r\n    C(const std::shared_ptr\u003cIB\u003e\u0026 b) : _b(b) {}\r\nprivate:\r\n    std::shared_ptr\u003cIB\u003e _b;\r\n};\r\n```\r\n\r\n### 2.2 Create Container\r\n\r\nAll required header files are included in `mioc.h`, simply include this header file, and you are ready to go!\r\n\r\nYou can create a container using `ServiceContainer::New()`. In this case, you have to inject this container everywhere you need. You can choose whether to enable lazy initialization for this container **on creation**. You **won't** be able to change it later. By default, lazy initialization is enabled.\r\n\r\n```cpp\r\nmioc::ServiceContainerPtr lazyContainer = mioc::ServiceContainer::New();\r\nmioc::ServiceContainerPtr hungryContainer = mioc::ServiceContainer::New(false);\r\n```\r\n\r\nOr, you can use `SingletonContainer`, which provides a global-scale singleton container. This container will not be created until the first time you get it. Also, on your first call, you can choose whether to enable lazy initialization for the global container.\r\n\r\n```cpp\r\n// by default, the global container enables lazy initialization\r\nmioc::ServiceContainerPtr container = mioc::SingletonContainer::GetContainer();\r\n// or you can disable it on, and only on the first call\r\nmioc::ServiceContainerPtr container = mioc::SingletonContainer::GetContainer(false);\r\n```\r\n\r\n### 2.3 Add Singleton\r\n\r\nThe first is to use the type name only. You should provide its interface type and concrete type. And all dependency types if it has.\r\n\r\n```cpp\r\ncontainer-\u003eAddSingleton\u003cIA, A\u003e();      // without dependency\r\ncontainer-\u003eAddSingleton\u003cIB, B, IA\u003e();  // with dependencies\r\n```\r\n\r\nAlso, you can directly add a pre-constructed instance to it. This way, you may need a pointer conversion first, which converts concrete type to its corresponding interface. You don't need to pass a type name in this case. And, of course, lazy initialization matters not in this way.\r\n\r\n```cpp\r\nstd::shared_ptr\u003cIC\u003e c = std::make_shared\u003cC\u003e(b);\r\ncontainer-\u003eAddSingleton(c);\r\n```\r\n\r\n### 2.4 Add Transient\r\n\r\nIt is simpler to add a transient object. We need to pass type names, and they will be appropriately resolved later.\r\n\r\n```cpp\r\ncontainer-\u003eAddTransient\u003cIA, A\u003e();\r\ncontainer-\u003eAddTransient\u003cIB, B, IA\u003e();\r\ncontainer-\u003eAddTransient\u003cIC, C, IB\u003e();\r\n```\r\n\r\n### 2.5 Resolve Instances\r\n\r\nNo matter how you add objects into the container, you can always resolve an instance with the interface type you provided on adding. `nullptr` will be returned if the interface is not registered.\r\n\r\n```cpp\r\nstd::shared_ptr\u003cIC\u003e c = container-\u003eResolve\u003cIC\u003e();\r\n```\r\n\r\n## 3. Limitations\r\n\r\nThough this mini IoC container can handle dependency injection, you have to specify all dependencies when you add anything manually. 🥲\r\n\r\n## 4. Acknowledgement\r\n\r\nThis project uses [doctest](https://github.com/doctest/doctest) for unit testing.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flord-turmoil%2Fminioc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flord-turmoil%2Fminioc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flord-turmoil%2Fminioc/lists"}