concept ICourse in category dependency injection

appears as: ICourse, ICourse
Dependency Injection

This is an excerpt from Manning's book Dependency Injection.

This example maps IIngredient to SauceBéarnaise, and ICourse to Course. There’s no overlap of types, so it should be pretty evident what’s going on. But you can also register the same Abstraction several times:

This maps IIngredient to SauceBéarnaise, and ICourse to Course. There’s no overlap of types, so it should be pretty evident what’s going on. But you can also register the same Abstraction several times:

You already saw this AddTransient method overload previously, when we discussed torn Lifestyles in section 15.1.2. The ChiliConCarne constructor is invoked with a hot Spiciness every time the ICourse service is resolved. The following example shows the definition of this AddTransient<TService> extension method:

public static IServiceCollection AddTransient<TService>(
    this IServiceCollection services,
    Func<IServiceProvider, TService> implementationFactory)
    where TService : class;

As you can see, this AddTransient method accepts a parameter of type Func<IServiceProvider, TService>. With respect to the previous registration, when an ICourse is resolved, MS.DI will call the supplied delegate and supply it with the IServiceProvider belonging to the current IServiceScope. With it, your code block can resolve instances that originate from the same IServiceScope. We’ll demonstrate this in the next section.

Dependency Injection in .NET

This is an excerpt from Manning's book Dependency Injection in .NET.

A simple generalization moves us toward an implementation of IMeal that takes an arbitrary number of ICourse instances instead of exactly three, as was the case with the ThreeCourseMeal class:

public Meal(IEnumerable<ICourse> courses)

Notice that, instead of requiring three distinct ICourse instances in the constructor, the single DEPENDENCY on an IEnumerable<ICourse> instance allows you to provide any number of courses to the Meal class—from zero to ... a lot! This solves the issue with ambiguity because there’s now only a single DEPENDENCY. In addition, it also improves the API and implementation by providing a single, general-purpose class that can model many different types of meal, from a simple meal with a single course to an elaborate, 12-course dinner.

Although the example to the right uses two successive calls to configure, the example to the left passes a code block with more statements to a single Configure method call. Both code examples end up registering correct mappings for both ICourse and IIngredient interfaces. However, configuring the same ABSTRACTION multiple times has some interesting results:

In this section, we’ll look at how you can configure StructureMap to wire up Meal instances with appropriate ICourse DEPENDENCIES. When we’re done, you should have a good idea of the options available to you when you need to configure instances with sequences of DEPENDENCIES.

StructureMap has a rather good understanding of sequences. If we want to use all configured instances of a given plug-in, AUTO-WIRING just works. As an example, given the configured ICourse instances in listing 11.9, you can configure the IMeal plug-in like this:

container.Configure(r => r.For<IMeal>().Use<Meal>());

Notice that this is a completely standard mapping from an ABSTRACTION to a concrete type. StructureMap will automatically understand the Meal constructor and determine that the correct course of action is to resolve all ICourse instances. When you resolve IMeal you get a Meal instance with the ICourse instances from listing 11.9: Rillettes, CordonBleu, and MousseAuChocolat.