How to Create Singleton to Dependency Inject Azure Storage Client and Container

Viewed 7928

I have a class that has a method that does Azure Storage things and currently, I am creating the StorageContainer, BlobClient, and Container every single time it is called:

private readonly IAzureStorageConfig _config;

public SaveImageBlob(IAzureStorageConfig config)
{
    _config = config;
}

public async Task<T> ExecuteAsync(ImageSaveInput input)
{

    //get the storage account from the connection string
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_config.ConnectionString);

    //instantiate the client
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    //set the container
    CloudBlobContainer container = blobClient.GetContainerReference(_config.ImagesContainerName);

    //... do things and stuff
}

Now, I want to rip that out of the SaveImageBlob.ExecuteAsync method and dependency inject it into the class, so the Azure Storage items are instantiated only once.

I currently have the interface as such:

namespace Domain.Interfaces
{
    public interface IAzureStorage
    {
        CloudStorageAccount StorageAccount { get; }

        CloudBlobClient BlobClient { get; }

        CloudBlobContainer Container { get; }
    }
}

And now, I am clueless as to how to implement that interface.

Once the interface is implemented, I imagine I'm going to change the SaveImageBlob.ExecuteAsync method to this:

private readonly IAzureStorageConfig _config;
private readonly IAzureStorage _storage;

public SaveImageBlob(IAzureStorageConfig config,
                     IAzureStorage storage)
{
    _config = config;
    _storage = storage;
}

public async Task<T> ExecuteAsync(ImageSaveInput input)
{

    //now, since I don't have to create an instance of the storageAccount, blobClient, and container, I can just access them from _storage
    //create the blockBlob
    CloudBlockBlob blockBlob = _storage.Container.GetBlockBlobReference(input.BlobUrl); 

    //... do things and stuff with a Stream (doesn't really matter)

    //upload the blob
    blockBlob.UploadFromStreamAsync(stream);

    //do more things and stuff and return something 
}

I just need to know how to implement the interface that will allow me to DI the AzureStorage class into SaveImageBlob as a Singleton. Not that it matters, but for posterity sake, I'm using Autofac for DI.

2 Answers
Related