简体   繁体   中英

Injecting multiple instances of a class with settings

I'm having a bit of trouble with using Azure storage. I have an implementation at the moment which is fine but I want to expand it so that I am able to use multiple storage accounts/containers in one solution. I can't get my head around how to do that and still allow for dependency injection. I also need to be able to pass in settings which define the connection string and container name

This is how I'm doing it at the moment:

builder.Services.AddSingleton<IAzureStorageClient, AzureStorageClient>();

builder.Services.Configure<AzureStorageSettings>(configuration.GetSection("AzureStorageSettings"));

and then in the constructor

public AzureStorageClient(IOptions<AzureStorageSettings> options)
{
    var azureStorageSettings = options.Value;
    var cloudStorageAccount = GetCloudStorageAccount(azureStorageSettings.ConnectionString);

    _blobClient = cloudStorageAccount.CreateCloudBlobClient();
    _blobContainer = GetBlobContainer(azureStorageSettings.ContainerName);
}

I've read a lot of similar posts which mention using named registrations but I am using the built in IoC container and it doesn't allow for that. I've also seen posts saying to use a factory which looks good but I am hoping to package this logic and share it among different solutions and the factory solution would require a lot of configuration which I would like to avoid to make the package easy to consume.

Update: I made the settings an interface to force it to be implemented each time it is required and I used the generic T to pass that into my storage client as follows:

public sealed class AzureStorageClient<T> : IAzureStorageClient<T> where T : class, IAzureStorageSettings, new()

public AzureStorageClient(IOptions<T> options) 
{
    var azureStorageSettings = options.Value;
    var cloudStorageAccount = GetCloudStorageAccount(azureStorageSettings.ConnectionString);

    _blobClient = cloudStorageAccount.CreateCloudBlobClient();
    _blobContainer = GetBlobContainer(azureStorageSettings.ContainerName);
}

Then it could be injected like this:

builder.Services.AddSingleton<IAzureStorageClient<SpecificStorageSettings>, AzureStorageClient<SpecificStorageSettings>>();

Just an example, you can use settings like this:

Settings.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace UseDifferentSettings
    {
        public abstract class Settings
        {
            public abstract string connectingstring {
                get;
            }
            public abstract string containername {
                get;
            }
            public abstract string blobname {
                get;
            }
        }
    }

Setting1.cs

 using System;
    using System.Collections.Generic;
    using System.Text;

    namespace UseDifferentSettings
    {
        class Setting1 : Settings
        {
            string _connectingstring = "DefaultEndpointsProtocol=https;AccountName=xxx;EndpointSuffix=core.windows.net";
            string _containername = "video1";
            string _blobname = "test.txt";
            public override string connectingstring
            {
                get { return _connectingstring; }
            }
            public override string containername
            {
                get { return _containername; }
            }
            public override string blobname
            {
                get { return _blobname; }
            }
        }
    }

Setting2.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace UseDifferentSettings
{
    class Setting2 : Settings
    {
        private string _connectingstring = "DefaultEndpointsProtocol=https;AccountName=xxx;EndpointSuffix=core.windows.net";
        private string _containername = "test";
        private string _blobname = "test.txt";
        public override string connectingstring
        {
            get { return _connectingstring; }
        }
        public override string containername
        {
            get { return _containername; }
        }
        public override string blobname
        {
            get { return _blobname; }
        }
    }
}

UploadToStorage.cs

using Azure.Storage.Blobs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace UseDifferentSettings
{
    public class UploadToStorage
    {
        Settings setting;
        public UploadToStorage(Settings setting) {
            this.setting = setting;
        }
        public void GoUpload() {
            string connectingstring = setting.connectingstring;
            string containername = setting.containername;
            string blobname = setting.blobname;
            string filecontent = "This is my test file content";
            byte[] array = Encoding.ASCII.GetBytes(filecontent);
            MemoryStream filestream = new MemoryStream(array);
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectingstring);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containername);
            BlobClient blobClient = containerClient.GetBlobClient(blobname);
            blobClient.Upload(filestream);
        }
    }
}

Program.cs(The main method class)

using System;

namespace UseDifferentSettings
{
    class Program
    {
        static void Main(string[] args)
        {
            Settings setting1 = new Setting1();
            Settings setting2 = new Setting2();
            UploadToStorage uploadtostorage = new UploadToStorage(setting1);
            uploadtostorage.GoUpload();
            Console.WriteLine("Hello World!");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM