{"id":15092097,"url":"https://github.com/genryianchev/annotationservicebuilder","last_synced_at":"2025-04-12T06:32:07.604Z","repository":{"id":254516686,"uuid":"846330319","full_name":"genryianchev/AnnotationServiceBuilder","owner":"genryianchev","description":"AnnotationServiceBuilder - Simplifying .NET Service Registration with Annotations","archived":false,"fork":false,"pushed_at":"2024-09-11T00:48:36.000Z","size":4438,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-07T11:04:12.489Z","etag":null,"topics":["annotations","blazor","core","dependency","injection","refit","visual-studio"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/genryianchev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-08-23T01:34:14.000Z","updated_at":"2024-09-11T00:48:39.000Z","dependencies_parsed_at":"2024-09-11T04:32:23.255Z","dependency_job_id":"0324187a-25de-49ca-96e5-454f0dc59f0b","html_url":"https://github.com/genryianchev/AnnotationServiceBuilder","commit_stats":{"total_commits":53,"total_committers":3,"mean_commits":"17.666666666666668","dds":"0.49056603773584906","last_synced_commit":"35f7401b8835fb6b702eaca046ae62e5815ab0b1"},"previous_names":["genryianchev/annationservicebilder","genryianchev/annotationservicebuilder"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/genryianchev%2FAnnotationServiceBuilder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/genryianchev%2FAnnotationServiceBuilder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/genryianchev%2FAnnotationServiceBuilder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/genryianchev%2FAnnotationServiceBuilder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/genryianchev","download_url":"https://codeload.github.com/genryianchev/AnnotationServiceBuilder/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223502361,"owners_count":17155938,"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":["annotations","blazor","core","dependency","injection","refit","visual-studio"],"created_at":"2024-09-25T11:00:51.378Z","updated_at":"2024-11-07T11:04:59.498Z","avatar_url":"https://github.com/genryianchev.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ![AnnotationServiceBuilder Icon](https://github.com/genryianchev/AnnotationServiceBuilder/raw/main/AnnotationServiceBuilder/icon.png) AnnotationServiceBuilder\n\nAnnotation Service Builder is an ASP.NET library that simplifies dependency injection by using custom annotations to automatically register services in the DI container.\n\n## Table of Contents\n\n1. [Prerequisites](#prerequisites)\n   - [Web Application](#web-application)\n   - [Important Note](#important-note)\n2. [Setting up Annotations](#setting-up-annotations)\n3. [Installing AnnotationServiceBuilder](#installing-annotationservicebuilder)\n   - [Step 1: Install the AnnotationServiceBuilder NuGet Package](#step-1-install-the-annotationservicebuilder-nuget-package)\n     - [Using .NET Core CLI](#using-net-core-cli)\n4. [Usage](#usage)\n   - [1. Using Scoped Services](#1-using-scoped-services)\n   - [2. Using Singleton Services](#2-using-singleton-services)\n   - [3. Using Transient Services](#3-using-transient-services)\n   - [4. Example of a Refit Client](#4-example-of-a-refit-client)\n   - [5. Example with Different `baseUrl` for Multiple Refit Clients](#5-example-with-different-baseurl-for-multiple-refit-clients)\n5. [Trimming Safety Considerations](#trimming-safety-considerations)\n6. [ASB: Observing the Logic of Pattern Standards](#asb-observing-the-logic-of-pattern-standards)\n7. [Benefits of Using AnnotationServiceBuilder](#benefits-of-using-annotationservicebuilder)\n\n---\n\n## Prerequisites\n\n### Web Application\n\n1. **.NET 6.0 or later**\n2. **Visual Studio 2022** (or any other IDE with .NET support)\n\n### Important Note\n\nEnsure that your .NET SDK is up-to-date. This project requires .NET 6.0 or later.\n\n## Setting up Annotations\n\nThe `Annotations` folder in the project contains the key attributes used for automatically registering services in the DI container. Below are the files and their purposes, along with examples of how they are used.\n\n---\n\n## Installing AnnotationServiceBuilder\n\n### Step 1: Install the AnnotationServiceBuilder NuGet Package\n\nYou can install the AnnotationServiceBuilder package via NuGet.\n\n#### Using .NET Core CLI\n\n```bash\ndotnet add package AnnotationServiceBuilder\n```\n\n---\n\n## Usage\n\n### Registering Services with `AnnotationServiceRegistrar`\n\nTo register your services, follow these steps:\n\n1. Initialize the `AnnotationServiceRegistrar`:\n\n```csharp\nAnnotationServiceRegistrar.Initialize(Assembly.GetExecutingAssembly());\n```\n\n2. Add your services to the DI container:\n\n```csharp\nAnnotationServiceRegistrar.AddSingletonServices(services);\nAnnotationServiceRegistrar.AddScopedServices(services);\nAnnotationServiceRegistrar.AddTransientServices(services);\nAnnotationServiceRegistrar.AddRefitClients(services, \"https://api.yourservice.com\"); // Replace with your API base URL\n```\n\nIf you need to use a custom `DelegatingHandler`, use the following:\n\n```csharp\nvar customHandler = new MyCustomHandler();\nAnnotationServiceRegistrar.AddRefitClients(services, \"https://api.yourservice.com\", customHandler);\n```\n\n---\n\n## Other Examples\n\n### 1. Using Scoped Services\n\n```csharp\nusing AnnotationServiceBuilder.Annotations.Scoped;\n\n[ScopedService(typeof(IMyScopedService))]\npublic class MyScopedService : IMyScopedService\n{\n    // Implementation...\n}\n```\n\n### 2. Using Singleton Services\n\n```csharp\nusing AnnotationServiceBuilder.Annotations.Singleton;\n\n[SingletonService]\npublic class MySingletonService\n{\n    // Implementation...\n}\n```\n\n### 3. Using Transient Services\n\n```csharp\nusing AnnotationServiceBuilder.Annotations.Transient;\n\n[TransientService]\npublic class MyTransientService\n{\n    // Implementation...\n}\n```\n\n### 4. Example of a Refit Client\n\n```csharp\nusing AnnotationServiceBuilder.Annotations.Refit;\nusing AnnotationServiceBuilder.Data.Models;\nusing Refit;\n\nnamespace AnnotationServiceBuilder.Network.Repositories\n{\n    [RefitClient]\n    public interface IPostsApi\n    {\n        [Get(\"/posts\")]\n        Task\u003cList\u003cPost\u003e\u003e GetPostsAsync();\n\n        [Get(\"/posts/{id}\")]\n        Task\u003cPost\u003e GetPostByIdAsync(int id);\n    }\n}\n```\n\n### 5. Example with Different `baseUrl` for Multiple Refit Clients\n\n```csharp\nusing AnnotationServiceBuilder.Annotations.Refit;\nusing Refit;\n\nnamespace AnnotationServiceBuilder.Network.Repositories\n{\n    [RefitClient(BaseUrl = \"https://api.service1.com\")]\n    public interface IService1Api\n    {\n        [Get(\"/endpoint1\")]\n        Task\u003cList\u003cResponseModel1\u003e\u003e GetService1DataAsync();\n    }\n\n    [RefitClient(BaseUrl = \"https://api.service2.com\")]\n    public interface IService2Api\n    {\n        [Get(\"/endpoint2\")]\n        Task\u003cList\u003cResponseModel2\u003e\u003e GetService2DataAsync();\n    }\n\n    [RefitClient(BaseUrl = \"https://api.service3.com\")]\n    public interface IService3Api\n    {\n        [Get(\"/endpoint3\")]\n        Task\u003cList\u003cResponseModel3\u003e\u003e GetService3DataAsync();\n    }\n}\n```\n\n---\n\n## Trimming Safety Considerations\n\nTo ensure that your services are not trimmed during the optimization process, use the trimming-safe registration methods provided by **AnnotationServiceBuilder**. These methods will guarantee that necessary types are preserved and registered correctly.\n\n```csharp\nAnnotationServiceRegistrar.Initialize(Assembly.GetExecutingAssembly());\n\nAnnotationServiceRegistrar.AddSingletonServicesWithTrimmingSafety(services);\nAnnotationServiceRegistrar.AddScopedServicesWithTrimmingSafety(services);\nAnnotationServiceRegistrar.AddTransientServicesWithTrimmingSafety(services);\nAnnotationServiceRegistrar.AddRefitClientsWithTrimmingSafety(services, \"https://api.yourservice.com\"); // Replace with your API base URL\n```\n\n### Manual Trimming Considerations\n\nIf this approach doesn't help, you may try to manually apply trimming safety considerations.\n\n#### Example of Using `DynamicDependency`\n\n```csharp\nusing System.Diagnostics.CodeAnalysis;\n\n[SingletonService]\npublic class StockPartsService\n{\n    [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(MyDependentService))]\n    public StockPartsService()\n    {\n        // Method implementation ensuring MyDependentService is retained\n    }\n}\n```\n\n#### Example of Using `Preserve`\n\n```csharp\nusing System.Runtime.CompilerServices;\n\n[Preserve]\n[SingletonService]\npublic class MyDependentService\n{\n    public void PerformOperation()\n    {\n        // Implementation that must be retained during trimming\n    }\n}\n```\n\n---\n\n## ASB: Observing the Logic of Pattern Standards\n\n\u003e **Note**: Factory patterns have been moved to a new library, **AnnotationServiceBuilder.Patterns**. Please update your code accordingly to use the new library. For more details, see: [AnnotationServiceBuilder.Patterns](https://github.com/genryianchev/AnnotationServiceBuilder.Patterns).\n\n---\n\n## Benefits of Using AnnotationServiceBuilder\n\n### 1. Automation of Service Registration\n\nAutomatically register your services in the DI container without needing to manually add each service in `Startup.cs` or `Program.cs`. This reduces boilerplate code and makes your setup process much more streamlined.\n\n### 2. Clear and Organized Codebase\n\nAnnotations define the lifetime of services (Singleton, Scoped, Transient), making your code more organized and easier to maintain.\n\n### 3. Time Efficiency\n\nAutomating service registration saves time, especially in large projects. Developers can focus on building features instead of managing service registrations manually.\n\n### 4. Ease of Use\n\nThe library provides a simple, intuitive API for registering services and Refit clients, making the process user-friendly.\n\n### 5. Caching for Performance\n\nRegistered classes and interfaces are cached to improve performance and reduce the overhead of repeated reflection operations.\n\n---\n\n## Video Guides\n\nFor video guides on how to use AnnotationServiceBuilder, you can watch these YouTube videos:\n- [AnnotationServiceBuilder Guide 1](https://www.youtube.com/watch?v=kofPf606OBE)\n- [AnnotationServiceBuilder Guide 2](https://www.youtube.com/watch?v=tspUekM_UHg\u0026t=3s)\n\n---\n\n## Additional Resources\n\n- [AnnotationServiceBuilder Examples](https://github.com/genryianchev/AnnotationServiceBuilderExamples)\n\n---\n\n## Contributing\n\nWe welcome contributions! Please submit a pull request or open an issue to discuss your ideas or report bugs.\n\n---\n\n## License\n\nThis project is licensed under the MIT License.\n\n```text\nMIT License\n\nCopyright (c) 2024 Gennadii Ianchev\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgenryianchev%2Fannotationservicebuilder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgenryianchev%2Fannotationservicebuilder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgenryianchev%2Fannotationservicebuilder/lists"}