I got a webservice that accepts multiple calls that require different handling and validation, using generics I've managed to create a common class that accepts a handler and a validator and it looks like this
public class PetitionService<T1, T2>
where T1 : Headers, Petition
where T2 : Headers
{
PetitionHandler<T1, T2> petitionHandler;
PetitionValidator<T1> petitionValidator;
public PetitionService(PetitionHandler<T1, T2> handler, PetitionValidator<T1> validator)
{
if (handler == null)
{ throw new ArgumentNullException("petitionHandler cannot be null"); }
if (validator == null)
{ throw new ArgumentNullException("petitionValidator cannot be null"); }
petitionHandler = handler;
petitionValidator = validator;
}
public T2 ProcessPetition(T1 petition)
{
petitionValidator.Validate(petition);
return petitionHandler.Handle(petition);
}
}
That's all fine and dandy, and it's working just fine, but I wanted to add another layer on top of it. Either a factory class or a message hub where the service class is mapped by the message type id. The problem with that is that since each handler return a different child of Headers it'll lead to the caller needing to unbox the response. Is there a way to avoid unboxing, is unboxing not so bad or am I just overdoing it?.