I am writing an API which uses the factory pattern.
I have an interface:
public interface IAbstractProduct<TRes, TCom>
{
Task<TRes> ExecuteAsync(TCom command);
}
Task returns TRes
, but sometimes I'd like to just return Task
without TResults
.
Is it possible to insert a TRes
such that Task<TRes> = Task
?
Or use some other way to make the interface IAbstractProduct
handle Task
and Task<TRes>
?
No, ultimately. What you could do is:
where TRes : class
), perhaps return a Task<TRes>
where the result is null
where TRes : struct
), return a Task<TRes?>
But: if TRes
could be any type, then no: this is not possible. In that scenario, you could consider something like Task<(bool HasResult, TRes Result)>
- effectively a manually constructed optional type.