Dorian
Dorian

Reputation: 983

blazor can i pass some parameters to inject attribute?

i have some service that downloads my dictionaries i have enum of my dicts

public enum EnumIntStrDict
{
Dostawca_Dict = 1,
ZazSiecia_Dict = 2,
Usluga_podst_Dict = 3,
}

and service...

public class DictService    
{
private readonly ILogger<DictService> _logger;
private readonly IHttpService _ihttp;

public DictService(IHttpService ihttp, ILogger<DictService> logger)
{
    _ihttp = ihttp;
    _logger = logger;     
}
static ConcurrentDictionary<EnumIntStrDict, IntStrDict> IntStrDictLocalCache = new();

private async Task FillIntStrDict(EnumIntStrDict DictName)
{}

so on this inject i would like to to FillIntStrDict if it is not already filled. and if it is just do nothing cause it is already in this cache so does not need to be awaited

so i would like to be able like

 @inject DictService(new EnumIntStrDict[]{Dostawca_Dict ,Usluga_podst_Dict })

so on injects it await load if need

how can i do that?

thanks and regards

Upvotes: 1

Views: 364

Answers (1)

Leandro Toloza
Leandro Toloza

Reputation: 2020

You could DI inject a factory that creates and exposes an instance of your provider class. If not you can set the enum as a parameter on the constructor and using DI you can inject that when you declare the service. (Remember that you need the interface of your service)

services.AddSingleton<IDictService>(httpclient,logger,enumvalues);

Also you can implement ActivatorUtilitiesExtensions of DI, but also needs to set the parameter on the constructor

Upvotes: 1

Related Questions