0

I need to perform the following task: for a user [email protected], store a blob of data into their dedicated data store.

  • DataStoreService is what actually stores the blob of data in the user's store, however it needs a StoreClientFactory which owns the logic for picking which store belongs to the given user.
  • StoreClientFactory retrieves 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 cloud
  • ICredentialFactory has several concrete implementations, for example LocalCredentialFactory, 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(); } } 
10
  • Show us the code used to construct this. Commented Jul 8, 2023 at 19:08
  • @candied_orange With the exception of class / method names, this is the actual code. Commented Jul 8, 2023 at 19:16
  • You haven't shown us how you drive this code. Which means it's dead and lifeless. You want to know if this is the "best solution". Give me something to judge. Even a unit test would help. Commented Jul 8, 2023 at 19:20
  • @candied_orange Updated with sample controller. Commented Jul 8, 2023 at 19:29
  • 1
    @async the point of factories is to build objects. Yet you wont show us the building of these objects. Makes it hard to tell if the factories are doing anything useful. Commented Jul 11, 2023 at 12:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.