0

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,

Sairam
  • 29
  • 3
  • What you are trying to achieve, is closely related to the Decorator pattern: https://stackoverflow.com/questions/6109646/how-do-i-use-the-decorator-pattern-with-unity-without-explicitly-specifying-ever – Steven Aug 28 '18 at 07:13
  • Hi Steve, thanks for responding. My handlers demand different dependencies as they call multiple REST services and only one handler calls the back end database. – Sairam Sep 12 '18 at 04:35
  • I know its been a long time from your question, but I've run on the same issue. [I've made an implementation](https://stackoverflow.com/questions/55476378/how-to-inject-the-dependency-of-the-next-handler-in-a-chain-of-responsibility), if you want to check out: – Ortiga Apr 02 '19 at 16:40

0 Answers0