{"id":21203885,"url":"https://github.com/czmirek/addfactoryextension","last_synced_at":"2025-03-14T22:44:50.289Z","repository":{"id":143379756,"uuid":"303983260","full_name":"czmirek/AddFactoryExtension","owner":"czmirek","description":"This project simulates the Ninject's ToFactory functionality but with IServiceCollection.","archived":false,"fork":false,"pushed_at":"2020-10-23T18:22:32.000Z","size":42,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-21T10:39:50.234Z","etag":null,"topics":["csharp","dotnet-core","factory","ioc"],"latest_commit_sha":null,"homepage":"https://github.com/czmirek/IServiceCollection.AddFactory-extension","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/czmirek.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":"2020-10-14T10:41:56.000Z","updated_at":"2022-03-17T19:24:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"b9a0a6d7-76cf-4fef-957c-5aff4b602c14","html_url":"https://github.com/czmirek/AddFactoryExtension","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czmirek%2FAddFactoryExtension","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czmirek%2FAddFactoryExtension/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czmirek%2FAddFactoryExtension/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czmirek%2FAddFactoryExtension/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/czmirek","download_url":"https://codeload.github.com/czmirek/AddFactoryExtension/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243658203,"owners_count":20326464,"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":["csharp","dotnet-core","factory","ioc"],"created_at":"2024-11-20T20:27:39.807Z","updated_at":"2025-03-14T22:44:49.540Z","avatar_url":"https://github.com/czmirek.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AddFactory extension method\r\nThis project simulates the Ninject's [`ToFactory`](https://github.com/ninject/Ninject.Extensions.Factory/wiki/Factory-interface) functionality\r\nbut on `IServiceCollection`.\r\n\r\n[![NuGet](http://img.shields.io/nuget/v/AddFactoryExtension.svg)](https://www.nuget.org/packages/AddFactoryExtension/)\r\n\r\n## Example\r\n\r\nTypes.cs\r\n```csharp\r\npublic interface IBar \r\n{ \r\n    void DoSomething(); \r\n}\r\npublic class Bar : IBar\r\n{ \r\n    public void DoSomething() \r\n        =\u003e Console.WriteLine(\"It works!!!\");\r\n}\r\n// Let's add implementation of IBarFactory dynamically!\r\npublic interface IBarFactory\r\n{ \r\n    IBar Factory(); \r\n}\r\n```\r\n\r\nProgram.cs\r\n```csharp\r\nusing Microsoft.Extensions.DependencyInjection;\r\n\r\npublic static class Program \r\n{\r\n    static void Main() \r\n    {\r\n        ServiceCollection sc = new ServiceCollection();\r\n        sc.AddTransient\u003cIBar, Bar\u003e();\r\n\r\n        // dynamic factory service lifetime \r\n        // is singleton by default and can be changed\r\n        sc.AddFactory\u003cIBarFactory\u003e();\r\n\r\n        var sp = sc.BuildServiceProvider();\r\n        var barFactory = sp.GetRequiredService\u003cIBarFactory\u003e();\r\n        \r\n        IBar bar = barFactory.Factory();\r\n        \r\n        // outputs 'It works!!!!'\r\n        bar.DoSomething();\r\n    }\r\n}\r\n```\r\n## How it works\r\n\r\nMagic. (`System.Reflection.Emit`)\r\n\r\n## Assembly scan for implemented interfaces\r\n\r\nThe `AddFactory` method scans for implementations of the \r\n`Factory` method return types inside the assembly of \r\nthe factory interface type.\r\n\r\nE.g. from the example above, the `AddFactory` will look\r\nfor implementations of `IBar` inside the assembly with\r\n`IBarFactory`.\r\n\r\nIt is possible to change that behvaiour and specify \r\nwhich assemblies to scan.\r\n\r\n```csharp\r\nsc.AddFactory\u003cIBarFactory\u003e(params Assembly[] assemblies)\r\n```\r\n\r\n## Automatic injection of other services\r\n\r\n`AddFactory` supports automatic injection of other services\r\ninside the dynamically created factory and then feeding those\r\nservices to the newly factored services.\r\n\r\nNote that the dependent services must come **after** any \r\nsimple types (ints/longs/other num types, enums, structs, strings).\r\n\r\nExample:\r\n\r\n```csharp\r\npubli\r\npublic interface ISomeDependency { }\r\npublic interface IBar1 { }\r\n\r\npublic class Bar1 : IBar1 \r\n{ \r\n    private int someParameter;\r\n    private ISomeDependency someDependency;\r\n\r\n    // ISomeDependency will be automatically fed by\r\n    // the dynamic factory\r\n    public Bar1(int someParameter, ISomeDependency someDependency) \r\n    {\r\n        this.someParameter = someParameter;\r\n        this.someDependency = someDependency;\r\n    }\r\n    public void DoSomething() \r\n    {\r\n        Console.WriteLine(\"It works!!!\");\r\n    }\r\n}\r\n\r\npublic interface IBar1Factory \r\n{ \r\n    // dynamic factory will inject ISomeDependency\r\n    // and then invoke the ctor with int\r\n    IBar1 Factory(int someParameter); \r\n}\r\n```\r\n\r\n## Limitations\r\n\r\n- The project searches only for methods named `Factory` inside the factory interface.\r\n- The return type of the `Factory` method must be an interface\r\n- `AddFactory` will throw an exception if there are multiple constructors matching the factory method signature.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczmirek%2Faddfactoryextension","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fczmirek%2Faddfactoryextension","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczmirek%2Faddfactoryextension/lists"}