I am little stuck with achieving something with multiple generic parameters. I have a service interface which will take a messagecontext interface as input to method.
public interface IService<T,U> where T : IMessageContext<U>
{
void ExecuteJob(T data);
bool CancelJob();
}
Then an abtract class which implements the interface.
public abstract class AbstractService<T,U> : IService<T,U> where T: IMessageContext<U>
{
public virtual bool CancelJob()
{
return true;
}
public abstract void ExecuteJob(T data);
}
The IMessage interface and default implementation is as shown below
public interface IMessageContext<U>
{
U Data { get; set; }
}
public class DefaultMessageContext : IMessageContext<string>
{
public string Data { get ; set; }
}
In this case, if we inherit the AbstractService class, it would be like below with the Generic parameters for T as DefaultMessageContext & U as string
public class ConcereteService : AbstractService<DefaultMessageContext,string>
{
public override void ExecuteJob(DefaultMessageContext data)
{
//Execute the job
}
}
Is there a way where I can just only mention the type for T as DefaultMessageContext as the type of U is already set in the class while implementing IMessageContext
Something like this
public class ConcereteService : AbstractNewService<DefaultMessageContext>
{
public override void ExecuteJob(DefaultMessageContext data)
{
//Execute the job
}
}
I could introduce another layer of abstraction where the type of U is set as string and only keep T open for implementation like AbstractStringService<T> : AbstractService<T,string>. But I don't want to do that.
Is there any other way to achieve it.