0

I need to implement a upload file service, so i have a classe named UploadService with one method named Upload. I have 3 integrations 3rd party to use: AwsS3, Google Storage and Azure Storage, but my class service dont need to know which intergration it will use and even know how to integrate with them. I was looking to the following patterns: Abstract Factory, to choose and create my upload integration class in runtime and the pattern Adapter to create a solid contract between my UploadService class domain and 3rd party integrations classes. So my code looked like this:

UploadService class:

public class UploadService
{
    private IUploadAdapter _adapter;

    public UploadService(IUploadFactory factory)
    {
        _adapter = factory.Create();
    }

    public void Upload(File file)
    {
        _adapter.Upload(file);
    }
}

UploadFactory class which switch a enum UploadApi that come from environments variables:

public class UploadFactory : IUploadFactory
{
    private IAwsS3Adapter _awsS3Adapter;
    private IGoogleStorageAdapter _googleStorageAdapter;
    private IAzureStorageAdapter _azureStorageAdapter;

    public UploadFactory(IAwsS3Adapter awsS3Adapter, IGoogleStorageAdapter googleStorageAdapter, IAzureStorageAdapter azureStorageAdapter)
    {
        _awsS3Adapter = awsS3Adapter;
        _googleStorageAdapter = googleStorageAdapter;
        _azureStorageAdapter = azureStorageAdapter;
    }

    public IUploadAdapter Create()
    {
        switch(UploadApi)
        {
            case "AwsS3": return _awsS3Adapter;
            case "GoogleStorage": return _googleStorageAdapter;
            case "AzureStorage": return _azureStorageAdapter;
        }
    }
}

Each interface IAwsS3Adapter, IGoogleStorageAdapter and IAzureStorageAdapter implements IUploadAdapter.

Adolfok3
  • 304
  • 3
  • 14
  • 1
    `switch(UploadApi)` where does UploadApi come from? – Fildor May 18 '22 at 14:06
  • Environment variables, its a enum. – Adolfok3 May 18 '22 at 14:09
  • So, there can ever only be one of those? Why not handle it through Dependency Injection? `services.AddSingleton();` Done. You can even put that in a convenience ExtensionMethod. `AddAwsUploadAdapter()`, ... Or use the Env-Var to determine which Concrete Implementation to register. – Fildor May 18 '22 at 14:13
  • I dont have any issues to make this work, my doubt is more theoric. Named ```UploadFactory``` make sense? And all the Adapter classes still a ```Adapter``` pattern? – Adolfok3 May 18 '22 at 14:22
  • 1
    Sure. The factory is superfluent, though. And: this forces the DI to actually create all three instances, unless you do some magic with `Lazy`. – Fildor May 18 '22 at 14:23
  • Great! If I need to choose a adapter in runtime and not come from env variables, how do you design and which desgin patterns would you use in this case? – Adolfok3 May 18 '22 at 14:28
  • Ok, that's a different story. In that case, a Factory makes more sense, probably. But there are more similiar patterns. Maybe Strategy Pattern. Depends on how it is supposed to be used. – Fildor May 18 '22 at 14:29
  • 1
    See [Appropriate design pattern for the payment modules c#](https://stackoverflow.com/a/48809896) and the linked references for examples of the strategy pattern that are somewhat similar to this one. – NightOwl888 May 18 '22 at 14:44

0 Answers0