Tags: Autofac, IoC, Dependency Injection

Although Autofac is not really part of Orchard's public API, it does come in handy when you need to customize how some of your classes are created and managed by Autofac.

In general, when you write your own classes that are injectable, you would take the following steps:

  1. Define an interface that derives from IDependency;
  2. Define a class that implements your interface

However, in certain cases, you may want to inject concrete classes directly into your constructor instead of an interface. One example might be when you implement the Command Pattern. You would generally have multiple classes that ultimately implement some ICommand interface. Next, consider having a controller that has a dependency on some command, e.g. SaveCustomerCommand. One solution could be to create a specific interface for each command, which would be perfectly fine (and perhaps even preferrable for unit testing classes that depend on these commands). But let's say that for some reason you don't want to write these interfaces. How would you be able to inject an UpdateCustomerCommand or a SaveCustomerCommand?

The answer is to tell Autofac about it!

Software IP management and project development governance provided by Outercurve Foundation