Dotnetdi: Difference between revisions
Jump to navigation
Jump to search
Created page with "=Registering a Service= ==Constructor Injection== With constructor Injection we define the list of required dependencies as parameters of the constructor for the class <syntax..." |
No edit summary |
||
Line 19: | Line 19: | ||
{ | { | ||
services.AddTransient<IIainsWidget, IainsWidget>(); | services.AddTransient<IIainsWidget, IainsWidget>(); | ||
} | |||
</syntaxhighlight> | |||
=Microsoft Dependency Injection Container= | |||
==Introduction== | |||
When building a site try and build a dependency graph which will help determine what services you will need | |||
[[File:Di graph.png|400px]] | |||
==Configuration== | |||
An options class: | |||
*Must be non-abstract with a public parameterless constructor. | |||
*All public read-write properties of the type are bound. | |||
*Fields are not bound. In the preceding code, Position is not bound. The Position property is used so the string "Position" doesn't need to be hard coded in the app when binding the class to a configuration provider. | |||
Example | |||
<syntaxhighlight lang="c#"> | |||
public class PositionOptions | |||
{ | |||
public const string Position = "Position"; | |||
public string Title { get; set; } | |||
public string Name { get; set; } | |||
} | |||
</syntaxhighlight> | |||
Now this can be passed using Dependency Injection using the Startup | |||
<syntaxhighlight lang="c#"> | |||
public void ConfigureServices(IServiceCollection services) | |||
{ | |||
services.Configure<PositionOptions>(Configuration.GetSection( | |||
PositionOptions.Position)); | |||
services.AddRazorPages(); | |||
} | |||
</syntaxhighlight> | |||
Add in the Controller | |||
<syntaxhighlight lang="c#"> | |||
public void HomeController : Controller | |||
{ | |||
private PostionalOptions _positionalOptions; | |||
public HomeController(IOptions<PositionOptions> options) | |||
{ | |||
_positionalOptions = options; | |||
} | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 04:10, 10 August 2020
Registering a Service
Constructor Injection
With constructor Injection we define the list of required dependencies as parameters of the constructor for the class
class IainsController : Controller
{
private IIainsWidget _iainsWidget;
public IainsController(IIainsWidget iainsWidet)
{
_iainsWidget = iainsWidget;
}
}
Registering a Service
In ASP Core the service must be registered with the IServiceCollection in the startup class configureServices.
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IIainsWidget, IainsWidget>();
}
Microsoft Dependency Injection Container
Introduction
When building a site try and build a dependency graph which will help determine what services you will need
Configuration
An options class:
- Must be non-abstract with a public parameterless constructor.
- All public read-write properties of the type are bound.
- Fields are not bound. In the preceding code, Position is not bound. The Position property is used so the string "Position" doesn't need to be hard coded in the app when binding the class to a configuration provider.
Example
public class PositionOptions
{
public const string Position = "Position";
public string Title { get; set; }
public string Name { get; set; }
}
Now this can be passed using Dependency Injection using the Startup
public void ConfigureServices(IServiceCollection services)
{
services.Configure<PositionOptions>(Configuration.GetSection(
PositionOptions.Position));
services.AddRazorPages();
}
Add in the Controller
public void HomeController : Controller
{
private PostionalOptions _positionalOptions;
public HomeController(IOptions<PositionOptions> options)
{
_positionalOptions = options;
}
}