Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/enisn/sourcegen.registrator
An advanced dependency service registration logic with Source Generators. Tens of times faster than reflection registrations
https://github.com/enisn/sourcegen.registrator
Last synced: 15 days ago
JSON representation
An advanced dependency service registration logic with Source Generators. Tens of times faster than reflection registrations
- Host: GitHub
- URL: https://github.com/enisn/sourcegen.registrator
- Owner: enisn
- License: mit
- Created: 2024-02-26T13:41:12.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-07-31T15:23:23.000Z (3 months ago)
- Last Synced: 2024-08-01T19:39:16.523Z (3 months ago)
- Language: C#
- Homepage:
- Size: 31.3 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SourceGen.Registrator
An advanced dependency service registration logic with Source Generators. Tens of times faster than reflection registrations.## Features
- **Source Generators**.
- _Easy to debug. No runtime reflection errors._
- **AOT** compatible:
- _Everything is written in your project and compiled with your assembly according to your target architecture._
- ...## Usage
1. Mark services with attributes to notify source generator.
```cs
[RegisterAsTransient]
class MyService
{
}
```2. The file will be generated in the same assembly with the name `{YourAssemblyName}Registrator.g.cs` in your root namespace. You can see the generated code by enabling the `Show All Files` option in the Solution Explorer.
![source generator solution preview](assets/generated-file-in-solution-preview.png)
```cs
using Microsoft.Extensions.DependencyInjection;namespace MyServiceConsoleApp;
public static class MyServiceConsoleAppRegistrator
{
public static IServiceCollection RegisterMyServiceConsoleAppServices(this IServiceCollection services)
{
services.AddTransient();
return services;
}
}
```1. The source generator will generate an extension method for IServiceCollection. Method name will be `Register{YourAssemblyName}Services()`. Use it on your ServiceCollection instance.
```cs
services.RegisterMyProjectServices();
```### Flexible Customization
Registrator provides flexible registration options unlike alternatives, you can use multiple service types or lifetime by combining attributes:```cs
[RegisterAsTransient(typeof(IServiceA), typeof(IServiceB))]
[RegisterAsScoped(typeof(IServiceC))]
class MyServiceA : IServiceA, IServiceB, IServiceC
{
}
```- And the generated code will be:
```csharp
using Microsoft.Extensions.DependencyInjection;namespace MyServiceConsoleApp;
public static class MyServiceConsoleAppRegistrator
{
public static IServiceCollection RegisterMyServiceConsoleAppServices(this IServiceCollection services)
{
services.AddScoped();
services.AddTransient();
services.AddTransient();
return services;
}
}
```## Work in progress
This is a simple PoC of source generator service registration and **work in progress**. The project is not ready to use for production yet...