{"id":13629644,"url":"https://github.com/Yeah69/MrMeeseeks.StaticDelegateGenerator","last_synced_at":"2025-04-17T09:35:28.008Z","repository":{"id":118651823,"uuid":"380019182","full_name":"Yeah69/MrMeeseeks.StaticDelegateGenerator","owner":"Yeah69","description":null,"archived":false,"fork":false,"pushed_at":"2021-06-27T18:45:34.000Z","size":44,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-07T11:47:45.512Z","etag":null,"topics":["csharp-sourcegenerator"],"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/Yeah69.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":"2021-06-24T18:35:46.000Z","updated_at":"2024-09-01T16:31:23.000Z","dependencies_parsed_at":"2023-06-09T09:15:28.230Z","dependency_job_id":null,"html_url":"https://github.com/Yeah69/MrMeeseeks.StaticDelegateGenerator","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yeah69%2FMrMeeseeks.StaticDelegateGenerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yeah69%2FMrMeeseeks.StaticDelegateGenerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yeah69%2FMrMeeseeks.StaticDelegateGenerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yeah69%2FMrMeeseeks.StaticDelegateGenerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Yeah69","download_url":"https://codeload.github.com/Yeah69/MrMeeseeks.StaticDelegateGenerator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223751334,"owners_count":17196615,"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-sourcegenerator"],"created_at":"2024-08-01T22:01:15.591Z","updated_at":"2024-11-08T20:31:33.765Z","avatar_url":"https://github.com/Yeah69.png","language":"C#","readme":"# MrMeeseeks.StaticDelegateGenerator\n\nThis Generator applies the Delegate pattern (similar or synonomous to Proxy, Wrapper, Adapter patterns) to the static elements (properties and methods) of a given type.\n\nThe explanations of the whys and hows here won't go much into the details of the basics. It's recommended to have a look into the Dependency Inversion Principle (DIP; it's one of the SOLID principles) \u0026 Dependency Injection and Object Oriented Programming (OOP) patterns like Proxy, Wrapper, Adapter and Delegate patterns. The patterns are all similar in the way how they are implemtented. They just differ in use cases. This project chose to go with the term Delegate, because it seemed to be most fitting.\n\n## Why and which problem does it solve?\n\nLet's directly start with a bad example:\n\n```C#\ninternal class BadExampleUsingStaticDependencyInConstructor\n{\n    public BadExampleUsingStaticDependencyInConstructor()\n    {\n        Console.WriteLine(DateTime.Now);\n    }\n}\n```\n\nThis is a violation of the DIP, because `DateTime.Now` is a dependency on a concrete implementation rather than an abstraction. That is always the case with references to static code. The issue here is that you cannot switch implementations. For example, this would make unit tests which depend on `DateTime.Now` returning a very concrete value impossible. \n\n\nThis is just indirectly the problem which `MrMeeseeks.StaticDelegateGenerator` solves. The problem which this project solves directly can be inferred from the solution to the problem of the bad example abover. Meaning the solution being the next problem. Here the solution:\n\n```C#\ninternal interface IDateTimeNowStaticDelegate\n{\n    DateTime Now { get; }\n}\n\ninternal class DateTimeNowStaticDelegate : IDateTimeNowStaticDelegate\n{\n    public DateTime Now =\u003e DateTime.Now;\n}\n\ninternal class SolvedExampleWithConstructorInjection\n{\n    public SolvedExampleWithConstructorInjection(\n        IDateTimeNowStaticDelegate dateTimeNowStaticDelegate)\n    {\n        Console.WriteLine(dateTimeNowStaticDelegate.Now);\n    }\n}\n```\n\nSolution: we delegate to the static code and wrap it into an concrete implementation implementing an interface which finally gets injected by constructor injection (DI). Let's call that the (Static) Delegate pattern. \n\nDIP is happy, problem solved, right? Not yet. Here comes the human component into play. Maintaining the Delegate pattern for all static references becomes tedious and monotonous fast. Also it bloats the code base (more code, more files) without doing something new (it just delegates to already existing functionality). \n\nAnd that is exactly the problem which `MrMeeseeks.StaticDelegateGenerator` is trying to solves.\n\n## Usage\n\nFirst, get the latest release version of the nuget package:\n\n```\nInstall-Package MrMeeseeks.StaticDelegateGenerator\n```\n\nThen declare which type you want to get a Static Delegate from via the `StaticDelegate`-Attribute. For `DateTime` it would look like:\n\n```C#\nusing System;\nusing MrMeeseeks.StaticDelegateGenerator;\n\n[assembly: StaticDelegate(typeof(DateTime))]\n```\n\nThe Source Generator will then generate an interface called `IDateTimeStaticDelegate` and a corresponding concrete implementation `DateTimeStaticDelegate` which you can use inplace of `DateTime`:\n\n```C#\ninternal class SolvedExampleWithStaticDelegate\n{\n    public SolvedExampleWithStaticDelegate(\n        IDateTimeStaticDelegate dateTimeNowStaticDelegate)\n    {\n        Console.WriteLine(dateTimeNowStaticDelegate.Now);\n    }\n}\n```\n\nOf course you would need to setup your DI to inject `DateTimeStaticDelegate` into this constructor parameter. However, this is a topic of its own which won't be covered here.\n","funding_links":[],"categories":["Do not want to test 112 ( old ISourceGenerator )","Source Generators"],"sub_categories":["1. [ThisAssembly](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ThisAssembly) , in the [EnhancementProject](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#enhancementproject) category","Other"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FYeah69%2FMrMeeseeks.StaticDelegateGenerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FYeah69%2FMrMeeseeks.StaticDelegateGenerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FYeah69%2FMrMeeseeks.StaticDelegateGenerator/lists"}