Since posting last time about using two file separate subfolders for development and production I've decided to modify the approach somewhat. I still want to be able to develop and test directly against Azure but realize that there are times when I might not want to use the cloud for development. One scenario is that I might be disconnected from the internet and I don't want to be unable to develop and debug code. The other consideration is cost. While file storage is fairly cheap on Azure it still costs more than just using my local hard drive.
So (with some inspiration from this blog post) I decided to create an abstraction layer. I created a generic storage interface for the basic storage functionality I needed (list files/create/delete/read/write) and then specific wrappers for Azure Files and local files.
Here is a simple UML for the StorageFolder interface:
In Startup.cs I can either instantiate one of the concrete implementations based on the environment and then use Dependency Injection with the interface.
services.AddScoped<IStorageClient, AzureStorageClient>(client => {
var shareName = WebEnvironment.IsDevelopment() ? "dev" : "prod";
var connectionString = Configuration.GetConnectionString("StorageConnection");
var shareClient = new ShareClient(connectionString, shareName);
return new AzureStorageClient(shareClient);
});
I've uploaded the full implementation at this Github Gist but here are the two implementations for creating a subfolder
Here is the Azure Files implementation. The StorageFolder class is instantiated pointing to a specific folder. So creating a subfolder is relative to that directory.
public class AzureFolder : IStorageFolder {
private readonly ShareDirectoryClient _dirClient;
public AzureFolder(ShareDirectoryClient directoryClient) {
_dirClient = directoryClient;
}
public async Task<IStorageFolder> CreateSubfolderAsync(string folderName) {
var directoryClient = await _dirClient.CreateSubdirectoryAsync(folderName);
return await Task.FromResult(new AzureFolder(directoryClient.Value));
}
}
Here is the local storage implementation. Since most of the calling code is from a website I wanted to use async where available. The local file API doesn't use async but using Task.Run() you can simulate that behavior and allow for a common interface.
public class LocalFolder : IStorageFolder {
private readonly DirectoryInfo _dirInfo;
public LocalFolder(DirectoryInfo dirInfo) {
_dirInfo = dirInfo;
}
public async Task<IStorageFolder> CreateSubfolderAsync(string folderName) {
var path = Path.Combine(_dirInfo.FullName, folderName);
await Task.Run(() => Directory.CreateDirectory(path));
return new LocalFolder(new DirectoryInfo(path));
}
}
No comments:
Post a Comment