mercredi 25 février 2015

What is a better more testable way for Repository based on tenants

I have a repository that receives a data layer as parameter and a tenantID like this. (Code is simplefied)



public class MyRepsitory{

private readonly IDataAccess _dataAccess;
private readonly string _tenantID;

public MyRepsitory(IDataAccess dataAccess, string tenantID)
{
_dataAccess = dataAccess;
_tenantID = tenantID;

}
}
}


This repository has also a method GetClientsForTenantID which is private and this is basically my point because every method relies on GetClientsForTenantID and this not ideal for unit testing because I cannot stub or mock GetClientsForTenantID in my current design. This is my current signature of the GetClientsForTenantID method at the moment. private IEnumerable<string> GetClientsForTenant(string tenantID) I want the repository to take care of it as internal behavior, that's why it is private. I do not want it as public member because then it is mutable for other developer and my api would not be reliable. I could have made it virtual so that a Mocking Framework could proxy it, but that didn't feel right, either.


It would need to have some fakeClients injected only for testing, or something else...


This shows the issue I am having in code. For example I cannot test my GetProducts() -Method



public IEnumerable<Product> GetProducts(string someParameter){

var clients = GetClientsForTenant(this._tenantId);

//do some logic that needs to be tested
}


The other thought that I had was Injecting an ITenantStore basically a class that takes care of resolving the clients base on the tenantID. But this would also needs to get the IDataAccess passed in and would do things that should do the repository.


Like this:



TenantStore(IDataAccess dataAccess, string tenantID)

MyRepository(IDataAccess dataAccess, ITenantStore tenantStore)


does also not feel right because of the IDataAccess all over the place...


What would be you recommended way to solve this? Any hints tips?


Aucun commentaire:

Enregistrer un commentaire