❔ Builder with generics
Is possible create a builder that have classes with different parameters?
An example
Compiler error,
Argument type 'Account.Importer.Domain.Commands.Contracts.ICommandHandler<Account.Importer.Domain.Commands.Contracts.CreateGoogleAccountCommand>' is not assignable to parameter type 'Account.Importer.Domain.Commands.Contracts.ICommandHandler<T>'
7 Replies
Generally, yes, but not with your
Build<T>
method, because T could be more derived than CreateGoogleAccountCommand
, in which case its not validSo in this case how can I deal with this?
essentially you can't, you either make a hack or you use object and casting
but effectively i see the
in T
🤔 ah no it's for ExecuteYeah, I think it's not good use object or something casting
i know
the problem (of contravariance) is builder would be of the type of the interface with the less derived type
Execute(ICommand command)
but you are assigning it an interface with a more derived type
Execute(CreateYahooAccountCommand command)
so it can't cast because it would expose ICommand parameter which really would be for example a CreateYahooAccountCommand
since it being ICommand you could really pass anything that inherits from that, not just CreateYahooAccountCommandYeah.. i changed the approach.. I appreciate your time, thank you
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.