I have a BlobStorageRepository class which is being used to interact with Azure blob storage. It implements an interface IBlobStorageRepository.
I have the need to use two instances of BlobStorageRepository but they are setup with different credentials:
In my Startup.cs:
// Normal storage
var storageCredentials = new StorageCredentials(_appSettings.BlobStorageAccountName, _appSettings.BlobStorageAccountKey);
var cloudBlobStorageAccount = new CloudStorageAccount(storageCredentials, true);
var cloudBlobClient = cloudBlobStorageAccount.CreateCloudBlobClient();
var blobRepository = new BlobStorageRepository(cloudBlobClient);
// Quarantine storage
var quarantineStorageCredentials = new StorageCredentials(_appSettings.QuarantineBlobStorageAccountName, _appSettings.QuarantineBlobStorageAccountKey);
var quarantineCloudBlobStorageAccount = new CloudStorageAccount(quarantineStorageCredentials, true);
var quarantineCloudBlobClient = quarantineCloudBlobStorageAccount.CreateCloudBlobClient();
var quarantineBlobRepository = new BlobStorageRepository(quarantineCloudBlobClient);
services.AddSingleton<IBlobStorageRepository>(blobRepository);
services.AddSingleton<IBlobStorageRepository>(quarantineBlobRepository);
How can I configure the above so that I can resolve the dependencies in my class and get the two different instances i.e:
private readonly IBlobStorageRepository _blobStorageRepository;
private readonly IBlobStorageRepository _quarantineBlobStorageRepository;
public DocumentService(IBlobStorageRepository blobStorageRepository, IBlobStorageRepository quarantineBlobStorageRepository)
{
_blobStorageRepository = blobStorageRepository;
_quarantineBlobStorageRepository = quarantineBlobStorageRepository;
}
Edit: My question is not a duplicate of How to register multiple implementations of the same interface in Asp.Net Core? because I want to pass the instantiated class when adding the service i.e. services.AddSingleton<IBlobStorageRepository>(blobRepository) and you cannot pass the instantiated class to serviceProvider.GetService<BlobStorageRepository>().
Out of the box, all you can do is inject all instances of the interface using:
public MyClass(List<IBlobStorageRepository> repos)
Then, you'll need to decide for yourself how to pull the right instance out of the list for each ivar you want to set.
It's generally better in this type of scenario to create a factory class and inject that instead. For example, you could have a BlobStorageRepositoryFactory and provide a way to return a repository instance for the particular scenario you require. That could be a method, property, etc. It's up to you. The point is that the factory coordinates the logic for both managing the repositories and providing an API to retrieve the right one.
Alternatively, you can supplement Microsoft.Extensions.DependencyInjection with a more robust DI container. A list of supported containers can be found at: https://github.com/aspnet/Extensions/tree/master/src/DependencyInjection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With