0

In .net Core 3 project, i have to use the CloudStorageAccount class for interacting with the Blob containers.

Here is sample code :-

  public class CloudStorageManager : ICloudStorageManager
{
    private readonly CloudStorageAccount cloudStorageAccount; 
   
    public CloudStorageManager(TIMConfiguration configuration)
    {
        if (!CloudStorageAccount.TryParse(configuration.AzureAdConfigurationSection.StorageConnectionString, out cloudStorageAccount))
            throw new FormatException("bad cloud connection string");
      }  }

I have defined various methods inside this class. Inside the startup.cs class, the registration is added like this :-

services.AddTransient<ICloudStorageManager, CloudStorageManager>();

I would like to know how can i make the CloudStorageManager class constructor better by removing the logic from it. Shall i replace it with the static constructor, since i need this initialization only once.

Secondly, can i use the Singleton scope for the registration because it will be same for all the clients.

Please advise on the resolution.

Karan
  • 3,265
  • 9
  • 54
  • 82
  • If you only want to execute the initialization once, then you can use a static constructor, please refer to:https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors. And using Singleton is the right choice, you can refer to: https://stackoverflow.com/a/38139500/12884742 – LouraQ Jun 26 '20 at 08:34
  • Can you suggest how can i refactor this code - if (!CloudStorageAccount.TryParse(configuration.AzureAdConfigurationSection.StorageConnectionString, out cloudStorageAccount)) throw new FormatException("bad cloud connection string"); – Karan Jun 26 '20 at 10:29

0 Answers0