如何在运行时使用参数解析依赖项?

时间:2018-12-10 17:43:56

标签: c# dependency-injection

我有一个简单的接口ICloudStorageRepository,它由两个具体的类实现:

public class AWSStorageRepository : ICloudStorageRepository
{
   private readonly IS3Client _client;
   public AWSStorageRepository(AWSCredentials credentials)
   {
       _client = new AWSStorageFactory(credentials).Create();
   }
}

和:

public class AzureStorageRepository : ICloudStorageRepository
{
   private readonly IAzureClient _client;
   public AzureStorageRepository(AzureCredentials credentials)
   {
       _client = new AzureStorageFactory(credentials).Create();
   }

}

然后在我的客户中我有一项服务:

public class CloudService
{
    public CloudService(ICloudStorageRepository repository)
    ....

    public Task<ListFiles> GetFiles(GetFilesInput input)
    {
         if(input.Provider == 'aws')
           resolve with AWSStorageRepository
         if(input.Provider == 'azure')
           resolve with AzureStorageRepository
    }
}

我使用Castle Windsor作为容器,在运行时会在输入参数中获取凭据,只能使用一次,因此我需要将其作为参数依赖项传递。

如何在运行时不将其注册到容器中呢?

0 个答案:

没有答案