{"id":23543881,"url":"https://github.com/beatthat/dependency-injection","last_synced_at":"2026-05-02T05:05:26.507Z","repository":{"id":144017038,"uuid":"141928698","full_name":"beatthat/dependency-injection","owner":"beatthat","description":"Provides IoC dependency injection. Works on top of beatthat/services which provides the container itself","archived":false,"fork":false,"pushed_at":"2018-09-19T21:33:25.000Z","size":153,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-15T08:43:18.100Z","etag":null,"topics":["dependency-injection","inversion-of-control","ioc","package","unity","unity3d"],"latest_commit_sha":null,"homepage":null,"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/beatthat.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":"2018-07-22T20:33:53.000Z","updated_at":"2018-09-19T21:33:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"fa11216f-4866-4de5-b3b9-75ff504761a4","html_url":"https://github.com/beatthat/dependency-injection","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/beatthat/dependency-injection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beatthat%2Fdependency-injection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beatthat%2Fdependency-injection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beatthat%2Fdependency-injection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beatthat%2Fdependency-injection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beatthat","download_url":"https://codeload.github.com/beatthat/dependency-injection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beatthat%2Fdependency-injection/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263233883,"owners_count":23434890,"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-injection","inversion-of-control","ioc","package","unity","unity3d"],"created_at":"2024-12-26T07:11:54.439Z","updated_at":"2026-05-02T05:05:26.469Z","avatar_url":"https://github.com/beatthat.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# \u003ca name=\"readme\"\u003e\u003c/a\u003edependency-injection\n\nProvides IoC dependency injection. Works on top of beatthat/services which provides the container itself\n\n## Install\n\nFrom your unity project folder:\n\n    npm init --force # only if you don't yet have a package.json file\n    npm install --save beatthat/dependency-injection\n\nThe package and all its dependencies will be installed under Assets/Plugins/packages.\n\nIn case it helps, a quick video of the above: https://youtu.be/Uss_yOiLNw8\n\n## USAGE\n\nFirst, any service you want to use must be registered.\n\n#### Register Services to make them available for dependency injection and lookup.\n\nThe simplest way to register a service is with the [RegisterService] attribute\n\n```c#\nusing BeatThat.Services;\n[RegisterService]public class Foo {}\n\npublic class UsesLookup\n{\n    void MyMethod()\n    {\n        // any service that is registered\n        // can be looked up directly\n        var foo = Services.Require\u003cFoo\u003e();\n    }\n}\n```\n\n#### Use [Inject] for cleaner code and less tight coupling\n\nDependency injection is a cleaner way to use services. It has a smaller code footprint and less direct dependencies on BeatThat.Services code. Down the road, if you wanted to switch to some other IoC container it would be easier to hack a patch to work with [Inject] tags than, say, Services.Require\u003cSomeClass\u003e calls.\n\n```c#\nusing BeatThat.Services;\nusing BeatThat.DependencyInjection;\n[RegisterService]public class Foo{}\n\npublic class UsesInjection_Manual : MonoBehaviour\n{\n    [Inject]Foo myFoo; // will be set by injection\n\n    void Start()\n    {\n        // Something needs to call DependencyInjection.InjectDependencies.\n        // One option is to call it in Start...\n        InjectDependencies.On(this);\n    }\n}\n```\n\n...the above example still directly calls DependencyInjection.InjectDependencies. You can better contain that dependency by using a base class. One also comes provided\n\n```c#\nusing BeatThat.Services;\nusing BeatThat.DependencyInjection;\n[RegisterService]public class Foo{}\n\npublic class UsesInjection_WithBaseClass : DependencyInjectedBehaviour\n{\n    [Inject]Foo myFoo; // will be set by injection\n}\n```\n\n#### Use interfaces for less tight coupling and easier refactoring\n\nIt's generally a good idea to use narrow interfaces for services to avoid tight coupling. More concretely, accessing services as interfaces makes it easier to swap the implementation, and, assuming the service interfaces are narrowly defined, also makes it much easier to understand at a glance what your service-dependent code really depends upon. This can be a big time saver when you're refactoring and using tools like 'Find References' to try to go through all the classes that depend on some service.\n\nThe [RegisterService] attribute provides a couple of features to make it easier to use interfaces.\n\n###### Use injection freely on interfaces defined directly on the service class\n\n```c#\nusing BeatThat.Services;\nusing BeatThat.DependencyInjection;\npublic interface Bar {}\n\n[RegisterService]public class Foo : Bar {}\n\npublic class UsesBar : DependencyInjectedBehaviour\n{\n    [Inject]Bar myBar; // will be set by injection to Foo\n}\n````\n\n###### Use 'proxyInterfaces' to register interfaces further up the chain\n\n```c#\nusing BeatThat.Services;\nusing BeatThat.DependencyInjection;\npublic interface Bar {}\n\npublic class FooBase : Bar {}\n\n[RegisterService(\n    // interface Bar will not be auto registered\n    // because it is not defined directly on class Foo\n    proxyInterfaces: new System.Type[] { typeof(Bar) }\n)]\npublic class Foo : FooBase {}\n\npublic class UsesBar : DependencyInjectedBehaviour\n{\n    [Inject]Bar myBar; // will be set by injection to Foo\n}\n````\n\n###### Use 'interfaceRegistrationPolicy' to just register all interfaces\n```c#\nusing BeatThat.Services;\nusing BeatThat.DependencyInjection;\npublic interface Bar {}\n\npublic class FooBase : Bar {}\n\n[RegisterService(\n    // register all interfaces on class and parents\n    interfaceRegistrationPolicy: InterfaceRegistrationPolicy.RegisterInterfacesDeclaredOnTypeAndParents\n)]\npublic class Foo : FooBase {}\n\npublic class UsesBar : DependencyInjectedBehaviour\n{\n    [Inject]Bar myBar; // will be set by injection to Foo\n}\n````\n\n## SAMPLES\n\nThis package installs with a Samples that has a few basic examples\nthat demonstrate how to use services, dependency injection etc.\n\nEach Sample also has a short video covering what's going on in the example.\n\n* Example_01_DependencyInjection: https://youtu.be/oaZdGZ98fdA\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeatthat%2Fdependency-injection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeatthat%2Fdependency-injection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeatthat%2Fdependency-injection/lists"}