dependency injection in asp.net core
TRANSCRIPT
PowerPoint
Dependency Injection in ASP.NET CoreLinBill2016/8/19
S.O.L.I.DHigh Cohesion & Low Coupling
1
2
3High Cohesion()Low Coupling()
High Cohesion & Low Coupling
4High Coupling
5High CouplingLow Coupling
6Low Cohesion
7Extremely Low Cohesion
8High Cohesion
9
Low Coupling & High Cohesion
Inversion of Control &Dependency Injection10
11IoC ()2004 Martin Fowler
12
13Dependency Injection()
Program to an interface, not an implementation!abstraction layer: interface, abstract class
14 Context BuilderClassAIServiceAServiceA creates
uses injects dependencies
15public class ObjA{ private IObj obj; public void ObjA(IObj obj) { this.obj = obj; } public void SomeAction() { obj.DoWork(); }}DI Constructor Injection n n
16public class ObjA{ private IObj obj;
public IObj Obj { get { return this.obj; } set { this.obj = value; } } public void SomeAction() { if (this.obj == null) { throw new ArgumentNullException("obj", "obj is null"); } obj.DoWork(); }} public void SomeAction() { obj.DoWork(); }}Property Injection
17public class ObjA{ public void SomeAction(IObj obj) { if (obj == null) { throw new ArgumentNullException("obj", "obj is null"); } obj.DoWork(); }}Method Injection
.NET Core18Mobile First, Cloud FirstOpen SourceCross Platform
19
In a press and analyst briefing last year, Microsoft CEO Satya Nadella put up a slide proclaiming Microsoft Linux.
20 Core
21
.NET FrameworkAPIsSharedAPIs.NET CoreAPIsWindows APIsWindows UpdatesGUI ArchitectureCross PlatformGitHubNuGetOpen Source
22
23
Getting StartedInstall.NET Core
Create a new .NET Core project:mkdir aspnetcoreappcd aspnetcoreappdotnet new
24{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true }, "dependencies": {}, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0" }, "Microsoft.AspNetCore.Server.Kestrel": "1.0.0" }, "imports": "dnxcore50" } }}Update theproject.jsonfile to add the Kestrel HTTP server package as a dependency:
25Restore the packages:
Add aStartup.csfile that defines the request handling logic:
dotnet restoreusing System;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;
namespace aspnetcoreapp{ public class Startup { public void Configure(IApplicationBuilder app) { app.Run(context => { return context.Response.WriteAsync("Hello from ASP.NET Core!"); }); } }}
26Update the code in Program.cs to setup and start the Web host:
Run the app (the command will build the app when its out of date):using System;using Microsoft.AspNetCore.Hosting;
namespace aspnetcoreapp{ public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseStartup() .Build();
host.Run(); } }}dotnet rundotnet run
27Browse to http://localhost:5000:
28
ASP.NET Core Initialization
Dependency Injection in .NET Core29
30.NET Core DI Container
31
ASP.NET Core Web Application
32
Web
33
34
project.json
MVC Web API Hosting
35Microsoft project.json .csproj
https://www.infoq.com/news/2016/05/project-json
36
Startup.cs
ConfigureServices ASP.NET DI services IServiceCollection ASP.NET 5 DI IServiceCollection Microsoft.Extensions.DependencyInjection
37
Configure app IApplicationBuilder UseMvc MVC/Web API ASP.NET
38
Run^2 See!
39
API Controller
40
41
Web API Controller Constructor ASP.NET controller ITimeService
42
Run^2 SeeOops!
43
Startup ConfigureServices ITimeService
44
Run^2 SeeYeah!
45 IServiceCollection AddScope(why?) ASP.NET DI (LIFO) ITimeService TimeService MVC ValuesController ITimeService DI ITimeService ValuesController
ValueController+ValueController(ItimeService)
ITimeService+Now():string+Now():stringTimeService.NET DI Container()MVC Frameworkcreatesusesinjects dependencies
Bye
null5120.0117null81972.43