Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve extensibility of type generators #288

Merged
merged 1 commit into from
Feb 24, 2025
Merged

Conversation

brantburnett
Copy link
Contributor

Motivation

Make it easier for extensions to control type generation. In particular, allow an extension to override the default type generation without requiring modification after code generation using an enricher.

Modifications

  • Allow more than one ITypeGeneratorFactory to be registered per category. These are then tried in order, based on Priority and order of registration, and the first that doesn't return null is chosen. Built-in factories are registered with a priority of 0 but added last.
  • Add new service registration methods that support registering multiple ITypeGeneratorFactory instances, obsolete the old methods.
  • Drop the TGeneratorCategory generic type parameter from all related interfaces and classes. Instead, using the support for keyed service registration added in .NET 8.
  • Update all instances of type generator categories to use the new approach.
  • Make NoopTypeGenerator and NoopTypeGeneratorFactory internal.

Results

  • Extensions can now register a type generator factory which takes precendence over the built-in type generator factory, but they can still fallback to built-in behaviors by returning null.
  • Extensions can much more easily create custom generator categories using string keys instead of complicated generics.

Breaking Change

Existing extensions which register custom generator categories must be rewritten to use string based keys instead of generic types.

For example:

- .TryAddTypeGeneratorFactory<OpenApiSchema, MyCategory, MyFactory>();
+ .AddTypeGeneratorFactory<OpenApiSchema, MyFactory>("MyCategory");
- internal class MyFactory : ITypeGeneratorFactory<OpenApiSchema, MyCategory>
+ internal class MyFactory : ITypeGeneratorFactory<OpenApiSchema>
- public MyConsumer(ITypeGeneratorRegistry<OpenApiSchema, MyCategory> registry)
+ public MyConsumer([FromKeyedServices("MyCategory")] ITypeGeneratorRegistry<OpenApiSchema> registry)

Motivation
----------
Make it easier for extensions to control type generation. In particular,
allow an extension to override the default type generation without
requiring modification after code generation using an enricher.

Modifications
-------------
- Allow more than one ITypeGeneratorFactory<T> to be registered per
  category. These are then tried in order, based on Priority and order
  of registration, and the first that doesn't return null is chosen.
  Built-in factories are registered with a priority of 0 but added last.
- Add new service registration methods that support registering multiple
  ITypeGeneratorFactory<T> instances, obsolete the old methods.
- Drop the TGeneratorCategory generic type parameter from all related
  interfaces and classes. Instead, using the support for keyed service
  registration added in .NET 8.
- Update all instances of type generator categories to use the new
  approach.
- Make NoopTypeGenerator and NoopTypeGeneratorFactory internal.

Results
-------
- Extensions can now register a type generator factory which takes
  precendence over the built-in type generator factory, but they can
  still fallback to built-in behaviors by returning null.
- Extensions can much more easily create custom generator categories
  using string keys instead of complicated generics.

Breaking Change
---------------
Existing extensions which register custom generator categories must be
rewritten to use string based keys instead of generic types.

For example:

```diff
- .TryAddTypeGeneratorFactory<OpenApiSchema, MyCategory, MyFactory>();
+ .AddTypeGeneratorFactory<OpenApiSchema, MyFactory>("MyCategory");
```

```diff
- internal class MyFactory : ITypeGeneratorFactory<OpenApiSchema, MyCategory>
+ internal class MyFactory : ITypeGeneratorFactory<OpenApiSchema>
```

```diff
- public MyConsumer(ITypeGeneratorRegistry<OpenApiSchema, MyCategory> registry)
+ public MyConsumer([FromKeyedServices("MyCategory") ITypeGeneratorRegistry<OpenApiSchema> registry)
```
@brantburnett
Copy link
Contributor Author

/merge

@centeredgebot centeredgebot bot merged commit 93bc397 into main Feb 24, 2025
11 checks passed
@centeredgebot centeredgebot bot deleted the multi-type-generator branch February 24, 2025 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant