{"id":23167994,"url":"https://github.com/mykolav/unitycontainer-param-autofactory","last_synced_at":"2025-08-18T06:32:53.186Z","repository":{"id":143406675,"uuid":"133422617","full_name":"mykolav/unitycontainer-param-autofactory","owner":"mykolav","description":"A Unity (DI container) parameterized instantiation extension inspired by Autofac","archived":false,"fork":false,"pushed_at":"2024-01-24T03:52:25.000Z","size":5333,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-26T02:51:40.215Z","etag":null,"topics":["autofactory","extension","unity-container"],"latest_commit_sha":null,"homepage":"","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/mykolav.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}},"created_at":"2018-05-14T21:17:27.000Z","updated_at":"2024-01-24T03:15:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"fe6bbb3c-0345-4b42-a790-137246176051","html_url":"https://github.com/mykolav/unitycontainer-param-autofactory","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/mykolav/unitycontainer-param-autofactory","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykolav%2Funitycontainer-param-autofactory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykolav%2Funitycontainer-param-autofactory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykolav%2Funitycontainer-param-autofactory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykolav%2Funitycontainer-param-autofactory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mykolav","download_url":"https://codeload.github.com/mykolav/unitycontainer-param-autofactory/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykolav%2Funitycontainer-param-autofactory/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270954807,"owners_count":24674767,"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","status":"online","status_checked_at":"2025-08-18T02:00:08.743Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["autofactory","extension","unity-container"],"created_at":"2024-12-18T02:37:20.438Z","updated_at":"2025-08-18T06:32:52.165Z","avatar_url":"https://github.com/mykolav.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Parameterized Auto Factory for the Unity IoC Container\n\nA [UnityContainer](https://github.com/unitycontainer) extension inspired by [Autofac's Parameterized Instantiation](http://docs.autofac.org/en/latest/resolve/relationships.html#parameterized-instantiation-func-x-y-b).\n\n# What is it for?\n\nIt lets you supply certain parameters of the target type's constructor by passing them to the auto-generated factory. While taking advantage of the container to resolve and inject all the other parameters that you don't want to supply manually.\n\nIn other words. Unity generates a factory `Func` for constructor parameters of the form `Func\u003cIDependency\u003e`.\n```csharp\npublic class TargetType\n{\n    // Unity will generate createDependency for us.\n    public TargetType(Func\u003cIDependency\u003e createDependency) { /* ... */ }\n}\n```\n\nThis extension takes it one step further. It automatically uses parameter overrides when \n- it sees a dependency of the form `Func\u003cIDependencyOfDependency, IDependency\u003e`\n- and the concrete class implementing `IDependency` needs a parameter of type `IDependencyOfDependency`.  \n\nThis only kicks in if `Func\u003cIDependencyOfDependency, IDependency\u003e` is not explicitely registered.\n\nHere is a quick example presented as an [xUnit](https://xunit.github.io/) test.\n\n```csharp\npublic interface IDialogBoxService { /* ... */ }\npublic interface IUserListDataSource { /* ... */ }\n\npublic class UsersGridWindow\n{\n    public UsersGridWindow(string windowTitle, \n                           IUserListDataSource userListDataSource, \n                           IDialogBoxService dialogBoxService)\n    {\n        WindowTitle = windowTitle;\n        UserListDataSource = userListDataSource;\n        DialogBoxService = dialogBoxService;\n\n        /* ... */\n    }\n\n    public string WindowTitle { get; }\n    public IUserListDataSource UserListDataSource { get; }\n    public IDialogBoxService DialogBoxService { get; }\n\n    /* ... */\n}\n\npublic class CachedUserListDataSource : IUserListDataSource\n{\n    public void WarmUp() { /* ... */ }\n}\n\n[Fact]\npublic void Factory_parameters_overrides_matching_constructor_parameters()\n{\n    // Setup the container\n    var container = new UnityContainer()\n        .AddNewExtension\u003cUnityParameterizedAutoFactoryExtension\u003e();\n\n    // Here the extension kicks in and generates \n    // a parameterized factory of type Func\u003cstring, IUserListDataSource, UsersGridWindow\u003e.\n    // Of course, in a real app Func\u003cstring, IUserListDataSource, UsersGridWindow\u003e would \n    // likely have been a constructor parameter.\n    var createUsersGridWindow = container\n        .Resolve\u003cFunc\u003cstring, IUserListDataSource, UsersGridWindow\u003e\u003e();\n\n    // Now, let's try to show a scenario which illustrates why\n    // a parameterized auto-factory can be useful.\n\n    // Create and warm up a cached data source.\n    // We can re-use the warmed up cache for any number of UsersGridWindow instances\n    // or other windows which depend on IUserListDataSource.\n    var cachedUserListDataSource = new CachedUserListDataSource();\n    cachedUserListDataSource.WarmUp();\n\n    // Pick window title for this particular window instance.\n    // We can pick another title for another window instance.\n    const string windowTitle = \"Registered users\";\n\n    // Create the window.\n    var usersGridWindow = createUsersGridWindow(windowTitle, cachedUserListDataSource);\n\n    // Let's make sure the parameters were overridden as expected.\n    Assert.Equal(usersGridWindow.WindowTitle, windowTitle); // We overrode this one.\n    Assert.Same(usersGridWindow.UserListDataSource, cachedUserListDataSource); // And this one too.\n\n    Assert.NotNull(usersGridWindow.DialogBoxService); // We didn't override DialogBoxService,\n                                                      // and so it was resolved from the container.\n}\n```\n\n# Download and install\n\n## [Unity v4.0.1](https://github.com/unitycontainer/unity/tree/a370e3cd8c0f9aa5f505e896ef5225f42711d361)\n\nIn case [Unity v4.0.1](https://github.com/unitycontainer/unity/tree/a370e3cd8c0f9aa5f505e896ef5225f42711d361) is used in your project, install [ParameterizedAutoFactory.Unity4](https://www.nuget.org/packages/ParameterizedAutoFactory.Unity4) version of this extension.\n\nTo install it run the following command in the [NuGet Package Manager Console](https://docs.microsoft.com/en-us/nuget/tools/package-manager-console).\n\n```powershell\nInstall-Package ParameterizedAutoFactory.Unity4\n```\n\n## [Unity v5.x](https://github.com/unitycontainer/unity/tree/v5.x)\n\nIn case [Unity v5.x](https://github.com/unitycontainer/unity/tree/v5.x) is used in your project, install [ParameterizedAutoFactory.Unity5](https://www.nuget.org/packages/ParameterizedAutoFactory.Unity5) version of this extension.\n\nTo install it run the following command in the [NuGet Package Manager Console](https://docs.microsoft.com/en-us/nuget/tools/package-manager-console).\n\n```powershell\nInstall-Package ParameterizedAutoFactory.Unity5\n```\n   \nThis will download all the binaries, and add necessary references to your project.\n\n\n# How to use it?\n\nOne way to register the extension in container is to use the `Unity.UnityContainerExtensions.AddNewExtension` extension method.\n\n```csharp\nvar container = new UnityContainer()\n    .AddNewExtension\u003cUnityParameterizedAutoFactoryExtension\u003e();\n\n```\n\nA call to `Unity.UnityContainerExtensions.AddNewExtension` parameterized by `UnityParameterizedAutoFactoryExtension` is approximately equivalent to executing the snippet below. \n```csharp\nvar extension = (UnityParameterizedAutoFactoryExtension)container.Resolve(\n    typeof(UnityParameterizedAutoFactoryExtension));\ncontainer.AddExtension(extension);\n\n```\n\n# Thank you!\n\n- This extension is inspired by [Autofac's Parameterized Instantiation](http://docs.autofac.org/en/latest/resolve/relationships.html#parameterized-instantiation-func-x-y-b).\n- [UnityAutoMoq](https://github.com/thedersen/UnityAutoMoq) gave a great example of extension hooking into Unity's dependencies instantiation/resolution pipeline.\n\n# License\n\nThe extension is licensed under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmykolav%2Funitycontainer-param-autofactory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmykolav%2Funitycontainer-param-autofactory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmykolav%2Funitycontainer-param-autofactory/lists"}