MVC 6 - the new unified Web programming model

23

Transcript of MVC 6 - the new unified Web programming model

Page 1: MVC 6 - the new unified Web programming model
Page 2: MVC 6 - the new unified Web programming model

Alex ThissenLead Consultant Xpirit

ASP.NET MVC 6A new unified programming model for the web

Page 3: MVC 6 - the new unified Web programming model

Agenda• Short overview ASP.NET 5• A unified programming model• Controllers• Dependency Injection• Routing

• Summary• Questions and Answers

Page 4: MVC 6 - the new unified Web programming model

An overviewViewing from 10.000 feet

Page 5: MVC 6 - the new unified Web programming model

ASP.NET components

MVC 6Unified Web Stack

Page 6: MVC 6 - the new unified Web programming model

Characteristics of MVC 6

• One set of concepts – remove duplication• Web UI and APIs combined• Smooth transition from Web Pages to MVC

Unified

• Run on IIS or self-host or cross-platform• Based on new request pipeline in ASP.NET 5• Runs in cloud-optimized Core CLR

Built on top of ASP.NET 5

• Pay as you use• Middleware • Built with Dependency Injection first

Modular

Getting familiar

Page 7: MVC 6 - the new unified Web programming model

.NET Framework vNext

Full .NET CLR•Entire API set in machine wide install at 200 MB•Updated with OS•Ecosystem of existing packages•Backward compatibility•Default for Visual Studio 2015 projects

Cloud Optimized CLR•Lean and modular runtime•Optimized for server•Small memory footprint•Libraries in NuGet packages•Framework deployed with app•Different versions can run side-by-side•11 MB

Mono•Cross-Platform runtime for Linux and Mac OS X•Together with Mono community

Standing on the shoulders of a new giant

ASP.NET 5.0 ASP.NET 5.0 ASP.NET 5.0

Page 8: MVC 6 - the new unified Web programming model

Getting started

• Middleware model introduced by OWIN

Ready, set, start – a lap around MVC 6

Page 9: MVC 6 - the new unified Web programming model

Middleware conceptually

• Middleware stack• Static files• Security, authentication,

CORS• Diagnostics, logging• Other cross-cutting

concernsHost process and server

Application and framework

Host

ApplicationApplication Framework

Server

RequestDelegatedelegate Task RequestDelegate(HttpContext context);

Page 10: MVC 6 - the new unified Web programming model

public void Configure(IApplicationBuilder app) {

app.Map("/Nancy",  builder => { builder.UseRuntimeInfo();

builder.RunNancyFx(); });

app.UseCors();

app.UseMvc(cfg);

Cors

Use, Map and Run

ErrorPageapp.UseErrorPage();

RuntimeInfo

NancyFX

MVC+WebAPI

Application FX

Middleware

Page 11: MVC 6 - the new unified Web programming model

Controllers• Single base class for

MVC and Web API• Not required

• POCO Controllers• No base class

• Combine Web API and MVC in 1 class• UI and REST endpoints

at one base URI

Not your typical gaming gear

Page 12: MVC 6 - the new unified Web programming model

Dependency InjectionIt all depends

Page 13: MVC 6 - the new unified Web programming model

Dependency Injection• DI is core part of runtime• Built-in DI for configuration and services

– Default lightweight version available• Wire up your own favorite IoC container ...

– Autofac, Ninject, StructureMap, Unity, Castle Windsorpublic void ConfigureServices(IServiceCollection services)

{ // Add EF services to the services container. services.AddEntityFramework(Configuration) .AddSqlServer() .AddDbContext<ApplicationDbContext>();

Page 14: MVC 6 - the new unified Web programming model

Built-in DI Container• Register mappings in ConfigureServices of

Startup• Default registrations• Lifetimes

– Transient– Scoped– Singleton– Instance

• Rules for registering mapping– First one wins– Allows you to get ahead off default mappings

Sometimes better is worse than good enough

Page 15: MVC 6 - the new unified Web programming model

ActivationActivation method Example

Constructor on classes MyController(ILog logger)

Attribute on field or property

[Activate]ILog Logger { get;set; }

Attribute in action [FromServices] ILog logger

Directive in Razor page ILog logger

Inject those objects

@inject

Page 16: MVC 6 - the new unified Web programming model

Tag Helpers• Allow server-side code to participate in rendering

of HTML elements in Razor files• Target HTML elements based on element and

attribute names• Reduce explicit transitions between HTML and C#• Take advantage of composition and tooling

benefits in Visual Studio Code and 2015

Getting some help in Razor

Page 17: MVC 6 - the new unified Web programming model

Custom Tag Helpersnamespace TechDays.TagHelpers{ [TargetElement("datumtijd")] public class DateTimeTagHelper : TagHelper { public override void Process(TagHelperContext context, TagHelperOutput output) { output.Content.SetContent(DateTime.Now.ToString()); } }}

// Inside Razor .cshtml file@addTagHelper "*, TechDays.TagHelpers"

Page 18: MVC 6 - the new unified Web programming model

Routing• Familiar MVC 5 attribute based

routing• Enabled by default• Special tokens: [controller] and

[action]

Show me the way

[Route("api/Hello")]public class HelloController : Controller {      [Route] public string Get() { return "hello"; }}

[Route("api/[controller]")]public class HelloController : Controller {      [Route] public string Get() { return "hello"; }}

Page 19: MVC 6 - the new unified Web programming model

View components

• Comparable to Web Forms’ UserControls• Different from Partial Views• Combines code and view as logical unit

User controls in MVC style

Page 20: MVC 6 - the new unified Web programming model

Logging

• Pluggable logging infrastructure provided• ILoggerFactory• Logging levels

– from Debug up to Criticial

Page 21: MVC 6 - the new unified Web programming model

Summary• Unified programming model• Built on ASP.NET 5• Learn Dependency Injection• Command-line vs. Visual Studio

Key takeaways from ASP.NET MVC 6

Page 22: MVC 6 - the new unified Web programming model

Your feedback is important!Scan the QR Code and let us know via the TechDays App.

Laat ons weten wat u van de sessie vindt!Scan the QR Code via de TechDays App.

Bent u al lid van de Microsot Virtual Academy?! Op MVA kunt u altijd iets nieuws leren over de laatste technologie van Microsoft. Meld u vandaag aan op de MVA Stand. MVA biedt 7/24 gratis online training on-demand voor IT-Professionals en Ontwikkelaars.

Page 23: MVC 6 - the new unified Web programming model