I'm refactoring a WPF application that uses Repository pattern to access the web services, I've got a tons of repository wrote as
public class DiagnosticsRepository : DefaultRepositoryBase, IDiagnosticsRepository
{
public Task<IList<MasterDiagnosticType>> GetMasterDiagnosticTypes(string username)
{
var client = GetServiceStackClient();
var request = new MasterDiagnosticTypesRequest
{
UserName = username
};
return client.PostAsync(request);
}
public Task<IList<MasterDiagnostic>> GetMasterDiagnosticsFromType(int type)
{
var client = GetServiceStackClient();
var request = new MasterDiagnosticRequest()
{ MasterType = type };
return client.PostAsync(request);
}
public Task<DiagnosticoDataResponse> GetMasterDiagnostic(string storedProcedure)
{
var client = GetServiceStackClient();
var request = new DiagnosticoDataRequest()
{
StoredProcedureName = storedProcedure,
Data = DateTime.Today
};
return client.PostAsync(request);
}
}
How can I review it so that I can avoid the repetition? the only part that changes is the request
PostAsync
. As I can see from your code this method can return many different types. I suggest to generalize return type of this method with kind ofIPostResponse
. As for parameter of the method I would define it asIPostRequest
. And all request types should implement this interface. After these changes refactoring of repositories will be trivial. \$\endgroup\$PostAsync<IPostResponse>
. I didn't work with ServiceStack but it looks configurable. \$\endgroup\$