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 can’t the compiler infer the correct types, if I try and write something like the following:
var service = new HandlingService();
var request = new GetCustomerByIdRequest { CustomerId = 1234 };
var response = service.Handle(request); // Shouldn't this know that response is going to be CustomerResponse?
I just get the ‘type arguments cannot be inferred’ message. Is this a limitation with generic type inference in general, or is there a way to make this work?
You have the constraint
TRequest : IRequest<TResponse>, but that doesn’t mean thatTResponsecan be automatically inferred fromTRequest. Consider that classes can implement multiple interfaces andTRequestmay implement severalIRequest<TResponse>types; you may not be doing this in your own design, but it would be pretty complicated for the compiler to have to trudge through the entire class hierarchy to infer that particular parameter.Long story short, the
Handlemethod takes two generic type parameters (TRequestandTResponse) and you’re only giving it one that it can actually use. Inferrence only happens on the actual type arguments, not the types that they inherit or implement.