{"id":17453522,"url":"https://github.com/jonsequitur/pocketcontainer","last_synced_at":"2025-04-19T15:38:44.387Z","repository":{"id":24393176,"uuid":"27793261","full_name":"jonsequitur/PocketContainer","owner":"jonsequitur","description":"An embeddable IoC container in a single C# file, with support for extensibility and conventions","archived":false,"fork":false,"pushed_at":"2024-04-03T00:54:41.000Z","size":233,"stargazers_count":19,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-11T00:48:05.495Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonsequitur.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":"2014-12-09T23:48:28.000Z","updated_at":"2024-02-08T15:28:23.000Z","dependencies_parsed_at":"2024-06-18T18:39:49.064Z","dependency_job_id":"155b0069-32d2-470c-9d06-e235561dd2c7","html_url":"https://github.com/jonsequitur/PocketContainer","commit_stats":{"total_commits":138,"total_committers":7,"mean_commits":"19.714285714285715","dds":"0.13043478260869568","last_synced_commit":"201d4375ff4972f76f6d3c9fc07fc363ddbbc6bf"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonsequitur%2FPocketContainer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonsequitur%2FPocketContainer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonsequitur%2FPocketContainer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonsequitur%2FPocketContainer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonsequitur","download_url":"https://codeload.github.com/jonsequitur/PocketContainer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249184822,"owners_count":21226470,"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":[],"created_at":"2024-10-18T00:43:08.819Z","updated_at":"2025-04-16T08:33:06.479Z","avatar_url":"https://github.com/jonsequitur.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"PocketContainer\n===============\n\n[![Join the chat at https://gitter.im/jonsequitur/PocketContainer](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jonsequitur/PocketContainer?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\n[![Build Status](https://ci.appveyor.com/api/projects/status/github/jonsequitur/PocketContainer?svg=true\u0026branch=master)](https://ci.appveyor.com/project/jonsequitur/pocketcontainer) [![NuGet Status](http://img.shields.io/nuget/v/PocketContainer.svg?style=flat)](https://www.nuget.org/packages/PocketContainer/)\n\n\n### An embeddable IoC container in a single C# file, with support for extensibility and conventions.\n\nWhen you install the `PocketContainer` NuGet package, it will add the `PocketContainer` C# file to your project. `PocketContainer` is an internal class and it's quite small, around 250 lines of code including the comments. Embedding it as a class (versus having an assembly) accomplishes a few goals: \n\n- Discourages usage as a service locator.\n- If you want the capabilities of an IoC container within a library, it avoids forcing an additional assembly dependency on consumers of that library.\n- Enables customizations that are very specific to your use case because you can tinker with the code or compose behaviors using partial classes.\n\n#### How do you use it?\n\n`PocketContainer` has familiar-looking `Register` and `Resolve` methods, with generic and non-generic variants:\n\n```csharp\n    var container = new PocketContainer();\n\n    container.Register\u003cIDoThings\u003e(c =\u003e c.Resolve\u003cDoSpecificThings\u003e());\n    \n    // or...\n    \n    container.Register(typeof(IDoThings), c =\u003e c.Resolve\u003cDoSpecificThings\u003e());\n```\n\nThe `Register` methods all take delegates. Many of the constructs that most containers have for lifetime can be replicated using closures.\n\nThe `Resolve` methods will try to recursively satisfy all arguments to the longest constructor on a type. Concrete types don't require explicit registration. This is the default strategy for PocketContainer, but others can be added. This extensibility mechanism allows you to specify how to handle unregistered types the first time someone tries to resolve them.\n\nHere's an example that resolves a concrete type when only one is found for a given interface or abstract class:\n\n```csharp\n    container.AddStrategy(type =\u003e\n    {\n        if (type.IsInterface || type.IsAbstract)\n        {\n            var implementations = Discover.ConcreteTypes()\n                                          .DerivedFrom(type)\n                                          .ToArray();\n\n            if (implementations.Count() == 1)\n            {\n                return c =\u003e c.Resolve(implementations.Single());\n            }\n        }\n        return null;\n    });\n```\n\nIf a strategy returns a delegate, `PocketContainer` registers it and uses it to resolve further calls for that type. If a strategy returns null, `PocketContainer` moves on to the next strategy. If no strategy is found that can resolve the type, an exception is thrown.\n\n#### Extensibility via partial classes\n\n[The Clone extension](Pocket.Container/PocketContainer.Clone.cs) is an example of a partial class being used to extend `PocketContainer`. Because this extensibility mechanism relies on access to `PocketContainer`'s private fields, these fields are considered to be part of the backwards compatibility contract of `PocketContainer`, and a change to one of them constitutes a breaking change and will incur a major version bump. \n\n#### Lazy\u003cT\u003e and Func\u003cT\u003e\n\n`PocketContainer` can return `Lazy\u003cT\u003e` and `Func\u003cT\u003e` instances that, when resolved or invoked, result in `Resolve` calls into the container. `Lazy\u003cT\u003e` and `Func\u003cT\u003e` do not need to be explicitly registered, but this behavior can be overridden by registering them.\n\nSupport for other similar types can be added trivially using `AddStrategy`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonsequitur%2Fpocketcontainer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonsequitur%2Fpocketcontainer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonsequitur%2Fpocketcontainer/lists"}