https://github.com/ivmazurenko/factory-generator
factory-generator
https://github.com/ivmazurenko/factory-generator
Last synced: 29 days ago
JSON representation
factory-generator
- Host: GitHub
- URL: https://github.com/ivmazurenko/factory-generator
- Owner: ivmazurenko
- Created: 2025-01-30T19:00:14.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-02-02T17:37:50.000Z (about 1 year ago)
- Last Synced: 2025-10-20T22:45:35.136Z (4 months ago)
- Language: C#
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- RSCG_Examples - https://github.com/ivmazurenko/factory-generator
README
# FactoryGenerator
.NET source generator that automatically
generates and registers [factories](https://github.com/ivmazurenko/factory-generator/blob/master/FactoryGenerator.Abstractions/IFactory.cs)
## Installation
Add the following packages to your project:
```bash
$ dotnet add package FactoryGenerator.Abstractions
$ dotnet add package FactoryGenerator.Microsoft.Extensions.DependencyInjection
```
## Usage
### Add the attribute to your class
Use one of the `GenerateIFactory` attributes to specify how your factory should be
generated:
```c#
[GenerateIFactory]
public class Service(int value, Dependency dependency)
{
// ...
}
```
This will generate an implementation of `IFactory`, allowing you to create instances of `Service` with an
`int` parameter while automatically resolving other dependencies from the DI container. FactoryGenerator
provides [multiple attribute variations](https://github.com/ivmazurenko/factory-generator/blob/master/FactoryGenerator.Abstractions/GenerateIFactoryAttribute.cs)
depending on the number of parameters your factory should accept.
### Register generated factories in the DI container
The `RegisterGeneratedFactories()` method automatically registers all factories created by the source
generator.
```c#
var serviceCollection = new ServiceCollection()
.RegisterGeneratedFactories();
```
### Use the factory
```c#
using var serviceProvider = serviceCollection.BuildServiceProvider();
var factory = serviceProvider.GetRequiredService>();
var service = factory.Create(1);
```
Full sample can be found [here](https://github.com/ivmazurenko/factory-generator/blob/master/Samples/Program.cs).