Given the following interfaces / classes:
public interface IRequest<TResponse> { }
public interface IHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
TResponse Handle(TRequest request);
}
public class HandlingService
{
public TResponse Handle<TRequest, TResponse>(TRequest request)
where TRequest : IRequest<TResponse>
{
var handler = container.GetInstance<IHandler<TRequest, TResponse>>();
return handler.Handle(request);
}
}
public class CustomerResponse
{
public Customer Customer { get; set; }
}
public class GetCustomerByIdRequest : IRequest<CustomerResponse>
{
public int CustomerId { get; set; }
}
Why the compiler cannot infer the correct types if I try to write something like the following:
var service = new HandlingService();
var request = new GetCustomerByIdRequest { CustomerId = 1234 };
var response = service.Handle(request);
I just get the message "type arguments cannot be deduced." Is this a limitation with the general type of output in general, or is there a way to make this work?
Jon m source
share