I need to perform the following task: for a user [email protected], store a blob of data into their dedicated data store.
DataStoreServiceis what actually stores the blob of data in the user's store, however it needs aStoreClientFactorywhich owns the logic for picking which store belongs to the given user.StoreClientFactoryretrieves the store based on the user's email address, however it needs to inject a credential, which will be different when running locally vs the cloudICredentialFactoryhas several concrete implementations, for exampleLocalCredentialFactory,CloudCredentialFactory, which is injected depending on the environment where the app runs.
My service ends up like this:
public class DataStoreService : IDataStoreService { private IStoreClientFactory StoreClientFactory { get; set; } public DataStoreService(IStoreClientFactory storeClientFactory) { StoreClientFactory = storeClientFactory; } public async Task StoreBlobAsync(string email, DataBlob blob) { var storeClient = StoreClientFactory.GetUserStore(email); await storeClient.StoreAsync(blob); } } public class StoreClientFactory : IStoreClientFactory { private ICredentialFactory CredentialFactory { get; set; } public StoreClientFactory(ICredentialFactory credentialFactory) { CredentialFactory = credentialFactory; } public StoreClient GetUserStore(string email) { var credential = CredentialFactory.GetCredential(); var urlForUserStore = StoreUtils.GetUrlForUserStore(email); return StoreClient(url, credential); } } public class LocalCredentialFactory : ICredentialFactory { public ICredential GetCredential() { return new LocalCredential(); } } This could continue with 1-2 more layers. Is this the best solution in my case or can I improve it?
Edit: the code is used in an ASP.NET Core API Controller:
public class DataStoreController { private DataStoreService DataStoreService { get; set; } public DataStoreController(DataStoreService dataStoreService) { DataStoreService = dataStoreService; } [HttpPost] public async Task<IActionResult> Post(DataBlob blob) { await DataStoreService.StoreBlobAsync(User.Identity.Name, blob); return Ok(); } }