C
C#5d ago
kurumi

✅ Register services based on Mediator registrated RequestClassHandlerWrapper &CommandClassHandlerWra

Hello folks 👋. I am not good at Roslyn source gens at all. Can somebody point me what should I do to make IValidationErrorFactory<> instances source generated by Roslyn. Or send me a guide lines / docs I needed for this. Appreciate ur help When the node (record, class, struct, whatever...) which implements interface Mediator.IRequest<Task_Manager.Common.Result<@TResponse, Task_Manager.Common.OneOfError<@TError, Task_Manager.Common.ValidationError>>> ┌─────────────┬───────────┴────────────────────────────────────┘ found, it is getting it @TResponse and @TError types in generic and it's name and produce the following code:
$"""
// <auto-generated />

// using's here

namespace Task_Manager.Identity.Application;

public class ${@name}_ValidationErrorFactory
: Task_Manager.Common.IValidationErrorFactory<Task_Manager.Common.Result<${@TResponse}, Task_Manager.Common.OneOfError<${@Terror}, Task_Manager.Common.ValidationError>>>
{
public Task_Manager.Common.Result<${@TResponse}, Task_Manager.Common.OneOfError<${@Terror}, Task_Manager.Common.ValidationError>> Create(Task_Manager.Common.ValidationError error)
=> new Task_Manager.Common.OneOfError<${@Terror}, Task_Manager.Common.ValidationError>(error);
}
"""
$"""
// <auto-generated />

// using's here

namespace Task_Manager.Identity.Application;

public class ${@name}_ValidationErrorFactory
: Task_Manager.Common.IValidationErrorFactory<Task_Manager.Common.Result<${@TResponse}, Task_Manager.Common.OneOfError<${@Terror}, Task_Manager.Common.ValidationError>>>
{
public Task_Manager.Common.Result<${@TResponse}, Task_Manager.Common.OneOfError<${@Terror}, Task_Manager.Common.ValidationError>> Create(Task_Manager.Common.ValidationError error)
=> new Task_Manager.Common.OneOfError<${@Terror}, Task_Manager.Common.ValidationError>(error);
}
"""
Example of code which should be generated: https://github.com/tokKurumi/Task-Manager/blob/5ec15cc628bd5d61bd98a10dd7ff6e4306c7fa80/Task%20Manager.Identity.Application/DependencyInjection.cs#L76
GitHub
Task-Manager/Task Manager.Identity.Application/DependencyInjection....
Task Manager. Contribute to tokKurumi/Task-Manager development by creating an account on GitHub.
11 Replies
333fred
333fred5d ago
$ig
MODiX
MODiX5d ago
If you want to make an incremental source generator, please make sure to read through both the design document and the cookbook before starting.
SleepWellPupper
@kurumi If you see me in vc I can help you out a little.
333fred
333fred4d ago
FWIW, this is extremely out of date, and does not follow any current practices
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
SleepWellPupper
enough I hope
kurumi
kurumiOP4d ago
thx @SleepWellPupper, I jump into VC, once u are able. And then once it's done I post the final result here and close this thread We prepared this plan for working // 1. List of handlers from services ! // 2. List of implementations from assembly // 3. Extract TResponse to: T and TError it goes to step 4 // 4. Construct GenericValidationErrorFactory<T, TError> // 5. Services.TryAddSingleton<IValidationErrorFactory<TResponse>, GenericValidationErrorFactory<T, TError>>
SleepWellPupper
The plan is to not use SGs for now.
kurumi
kurumiOP4d ago
Here is high overview how to achieve this registration: 0. Create generic factory 1. Get all services which implements RequestClassHandlerWrapper<TCommand, Result<T, OneOfError<TError, ValidationError>>> CommandClassHandlerWrapper<TCommand, Result<T, OneOfError<TError, ValidationError>>> 2. Get it's T and TError 3. Register IValidationErrorFactory<Result<T, OneOfError<TError, ValidationError>>> Here is the source code of service scanning This approach makes me able to remove Scrutor package dependency in addition thx to @SleepWellPupper P. S. code is a bit nasty, I gonna improve it soon. But it works
GitHub
Task-Manager/Task Manager.Identity.Application/DependencyInjection....
Task Manager. Contribute to tokKurumi/Task-Manager development by creating an account on GitHub.
SleepWellPupper
GitHub
Task-Manager/Task Manager.Identity.Application/DependencyInjection....
Task Manager. Contribute to tokKurumi/Task-Manager development by creating an account on GitHub.
kurumi
kurumiOP4d ago
just copilot auto completion, I remove it

Did you find this page helpful?