I am working on a project where I am using Unity, DI. Also I use COR (Chain of Responsibility) pattern which my application demands. I ran into an issue and got stuck with DI. I am hoping someone would shed some light.
public interface IReport
{
int ReportId {get; set;}
}
public Interface IHandler
{
void Process(IReport report);
}
public class HandlerA : IHandler
{
public IHandler Successor;
public DependentX Xxx;
public HandlerA(IHandler successor)
{
Successor = successor;
}
public void Process(IReport report)
{
// Business logic goes here
if(Successor != null)
{
Successor.Process(report);
}
}
}
public class HandlerB : IHandler
{
public IHandler Successor;
public DependentY Yyy;
public HandlerB(IHandler successor)
{
Successor = successor;
}
public void Process(IReport report)
{
// Business logic goes here
if(Successor != null)
{
Successor.Process(report);
}
}
}
public class HandlerC : IHandler
{
public IHandler Successor;
public DependentZ Zzz;
public Repository repo;
public HandlerC(IHandler successor)
{
Successor = successor;
}
public void Process(IReport report)
{
// Business logic goes here
if(Successor != null)
{
Successor.Process(report);
}
}
}
Could someone suggest how to inject the dependencies DependentX, DependentY, DependentZ and Repository to these handlers in the chain? Remember I only mentioned few dependenies here. Some handlers have more than one dependency.
Since I am not explicitly creating instances of these handlers for execution and chain would take care of it, I have no clue on how to inject these dependencies. Please note, each handler's dependencies are different.
Any help would be appreciated.
Thanks,