Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Ideas for Registering Open Generics in ServiceCollection when they aren't "equal" #127609

Unanswered
luizfbicalho asked this question in Q&A
Discussion options

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

You must be logged in to vote

Replies: 1 comment

Comment options

Looks like there is this issue #41050 that solves my problem

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /