-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Ideas for Registering Open Generics in ServiceCollection when they aren't "equal" #127609
Unanswered
luizfbicalho
asked this question in
Q&A
-
I have an open generic registration that work fine
services.AddScoped(typeof(IAsyncRepository<>), typeof(AsyncRepository<>));
My point is that I have this other case
public interface IAsyncEvent<in TEvent> where TEvent : class { Task HandleAsync(TEvent eventArg, CancellationToken ct); } public class CreateAction<TModel>(TModel model) : ICloneable where TModel : class { public TModel Model { get; } = model; } public class CreateEventAsync<TModel>(IAsyncRepository<TModel> repository) : IAsyncEvent<CreateAction<TModel>> where TModel : class { public async Task HandleAsync(CreateAction<TModel> eventArg, CancellationToken ct) { await repository.CreateAsync(eventArg.Model, userContext, ct); } }
and what I wanted was to register the open generic type and resolve it
services.AddScoped( typeof(IAsyncEvent<>).MakeGenericType(typeof(CreateAction<>)), typeof(CreateEventAsync<>) );
this code doesn't run at all
services.AddScoped( typeof(IAsyncEvent<CreateAction<>>)), typeof(CreateEventAsync<>) );
this one doesn't compile
the other option with a function could work, but I needed more parameters, for example receive the type that was requested, the closed generic one
or one alternative would be to have a scoped that instead of returning the factory would return the type to be constructed instead.
It could be transient in this case also
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Looks like there is this issue #41050 that solves my problem
Beta Was this translation helpful? Give feedback.
All reactions
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment