How do IServiceClient interfers the type?

Hello,
I’m using the repository pattern to make call from my WPF application to a webservice using IServiceClient… My repositories inherits from a base class and all the methods are in the form

 public Task<IList<Institute>> GetInstitutesAsync()
    {
        var client = GetServiceStackClient();

        var request = new InstituteRequest();

        return client.PostAsync(request);
    }

    public Task<IList<Status>> GetStatusAsync()
    {
        var client = GetServiceStackClient();

        var request = new StatusRequest();

        return client.PostAsync(request);
    }

I’ve created a generic method that wraps the Servicestack creation and the PostAsync so I can call the methods in the form

public Task<IList<Portfolio>> GetPortfoliosAsync(string username)
    {
        var request = new PortfolioRequest
        {
            UserName = username
        };

         return Get<IList<Portfolio>, PortfolioRequest>(request);
    }

And the generic method is defined inside a DefaultRepositoryBase as

  public class DefaultRepositoryBase : RepositoryBase
{
    private static IServiceClient GetServiceStackClient(string path = "")
    {
        var defaultServerSettings = ServiceLocator.Default.ResolveType<ITesoreriaServerSettings>();
        return defaultServerSettings.GetServiceStackClient(path);
    }

    internal static Task<TResponse> Get<TResponse, TRequest>(TRequest request)
        
    {
        var client = GetServiceStackClient();
        return client.PostAsync<TResponse>(request);
    }
}

I was wondering how the PostAsync can infers the types while I can’t (so I’ve to write the <TResponse,TRequest>

Is there a way I can avoid to write them?

Thanks
Paolo

Firstly please don’t use interfaces like IList in DTOs they have zero value and can cause runtime issues, use a concrete List instead.

If you’re asking why you need to specify the Response Type on the call site, it’s because you need the IReturn Interface marker on Request DTOs.

I’m migrating my code to use List, it’s a quite huge work !
I mean Servicestack is so wounderfull, I’ve got this DTO

  public class AccountUnwrappedRequest : IReturn<List<AccountUnwrapped>>
{
}

it’s used here

    public Task<IList<AccountUnwrapped>> GetAccountsUnwrapped()
    {
        var client = GetServiceStackClient();

        var request = new AccountUnwrappedRequest();

        return client.PostAsync(request);
    }

And in the PostAsync I don’t have to specify the return type, since it’s inferred I think from the Request

How can I have the same behavior here

 public Task<List<Portfolio>> GetPortfoliosAsync(string username)
    {
        var request = new PortfolioRequest
        {
            UserName = username
        };

        return Get<List<Portfolio>, PortfolioRequest>(request);
    }

to be able to do

  return Get(request);

Is it possible? How?

This is how C# generics works, either all generic types need to be inferred or you need to specify all of them on the call-site.

If you want them inferred your Get should look like IRestClient’s Get method:

TResponse Get<TResponse>(IReturn<TResponse> requestDto);

Where it accepts a Typed Request DTO with the IReturn<T> interface marker which holds the generic Type of the Response which is how it can be inferred.

1 Like