Skip to content

DependencyInjection

Ian Johnson edited this page Sep 7, 2018 · 1 revision

Constructor injection

Service support construction injection by default using the built in DI container or 3rd party container.

Interface as method parameter

By default all non enumerable interfaces parameters will be automatically located from the IServiceProvider. The parameters are not shown in the documentation and expected not to be part of the request.

// callers provide x and y
public int Method(IIntMathService mathService, int x, int y)
{
   return mathService.Add(x, y);
}

[FromServices]

Sometimes you need to request concrete types from the container and not from the caller

public int SomeMethod([FromServices]SomeClass someC, int x, int y)
{
  return someC.Method(x, y);
}

Clone this wiki locally