Reduce boilerplate with RepositoryPattern 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&lt;IList&lt;MasterDiagnosticType&gt;&gt; GetMasterDiagnosticTypes(string username)
    {
        var client = GetServiceStackClient();

        var request = new MasterDiagnosticTypesRequest
        {
            UserName = username
        };

        return client.PostAsync(request);
    }

    public Task&lt;IList&lt;MasterDiagnostic&gt;&gt; GetMasterDiagnosticsFromType(int type)
    {
        var client = GetServiceStackClient();

        var request = new MasterDiagnosticRequest()
        { MasterType = type };

        return client.PostAsync(request);
    }

    public Task&lt;DiagnosticoDataResponse&gt; 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
If you must keep the code the same way, I can think of only reducing the repeated

var client = GetServiceStackClient();
return client.PostAsync(request);


by doing something like this:

public class DiagnosticsRepository : DefaultRepositoryBase, IDiagnosticsRepository
{
    public Task&lt;IList&lt;MasterDiagnostic&gt;&gt; GetMasterDiagnosticsFromType(int type)
    {
        var request = new MasterDiagnosticRequest()
        {
            MasterType = type
        };

        return Refactor&lt;IList&lt;MasterDiagnostic&gt;, MasterDiagnosticRequest&gt;.Get(request);
    }

    public Task&lt;DiagnosticoDataResponse&gt; GetMasterDiagnostic(string storedProcedure)
    {
        var request = new DiagnosticoDataRequest()
        {
            StoredProcedureName = storedProcedure,
            Data = DateTime.Today
        };

        return Refactor&lt;DiagnosticoDataResponse, DiagnosticoDataRequest&gt;.Get(request);
    }

    // get rid of the repetition... 
    private class Refactor&lt;TRespone, TRequest&gt;
    {
        public static Task&lt;TRespone&gt; Get(TRequest request)
        {
            var client = GetServiceStackClient();
            return client.PostAsync(request);
        }
    }
}


Frankly, I don't think you should have all these various methods and signatures at all. You could reduce the repository to just the methods that are usually found in a repository (ex: Add, Update, GetById, Delete, Etc...

Which would mean your repository would potentially just look like this instead:

public class DiagnosticsRepository&lt;T&gt; : IDiagnosticsRepository
{
    public static Task&lt;T&gt; Get(object request = default(object))
    {
        var client = GetServiceStackClient();
        return client.PostAsync(request);
    }
}


-The below is unconfirmed for your particular library, but based on my quick read of the documentation, this should work-
With the above minimized repository,  if you like, you could go even further and swap your requests to anoymous types to reduce the amount of classes you are generating -- which is not uncommon especially for JSON requests. For example:

string storedProcedure = "...";
object request = new { StoredProcedureName = storedProcedure, Data = DateTime.Today };
var response = DiagnosticsRepository&lt;DiagnosticoDataResponse&gt;.Get(request);