C# Dependency Injection – Callback Injection Techniques

ccallbackscircular-dependencydependency-injectionservices

I have a service that needs to make a callback. Basically, it is an event that is expected to be handled in exactly one place, and that is too important to be optional.

The obvious approach seems to be to inject an Action. In the context of dependency injection, is it considered good (or acceptable) practice to do so?

I'm also eager to hear why (not), or what alternatives you would consider.

One particular problem that comes to mind is in the following scenario:

  • Parent's constructor takes an IChild.
  • Child's constructor takes an Action, the callback.
  • Parent has the method that is to receive the callback.
  • To instantiate the Child, we need the Parent's method, and thus the Parent instance. But to create that, we first need the Child instance. Problem.

One solution I can think of is to inject an IChildFactory instead. Parent's constructor can then use that factory to create the Child instance. At this point, Parent exists, and thus it can pass its callback method to the factory.

This solution seems to get the job done, but I'm curious about alternatives.

Best Answer

Yes, injecting a callback is absolutely acceptable. I would usually prefer to do it this way.

An alternative, which you've already pointed out, is to define an event on the service object and use a factory to create it - making sure that it's the only way to create it and then have the factory handle the event, or delegate it further (which brings us back to square one).*

The specific parent/child example may have other solutions, depending on your context. For example, you could create the parent, and pass it to the child constructor, which could insert itself as the parent's child.

* Clarification: My suggestion was to either inject a callback or use a factory that registers an event handler in the regular way. In this case, to enforce your requirement that the event must be handled, only the factory should create service objects. Then no-one can create a service object without registering an event handler.

You may want to use a factory even with the injected callback for other reasons (e.g. to make sure the correct callback is provided). Also you may have multiple such factories. Those are separate considerations.

Related Topic