ASP.NET Core Demos Part 2

20
ASP.NET CORE QUICK START

Transcript of ASP.NET Core Demos Part 2

Page 1: ASP.NET Core Demos Part 2

ASP.NET CORE QUICK START

Page 2: ASP.NET Core Demos Part 2

ERIK NORENSolutions ArchitectSoftware Developer 15+ YearsSoftware Tinkerer Since Commodore 64Hardware HobbyistTwitter: @ErikNorenGitHub: ErikNoren

Page 3: ASP.NET Core Demos Part 2

ASP.NET CORE QUICK STARTMiddleware

Page 4: ASP.NET Core Demos Part 2

MIDDLEWARE• Serve as the “pipeline” for handling requests• Order of middleware is significant and should be carefully considered• Requests pass through middleware in the order defined and responses in reverse order

• A single request cycle uses the same middleware class instance• Data and information stored in member variables during request available during

response

• Each middleware component is responsible for passing on or short circuiting• Short circuiting means completing work without calling next middleware in pipeline• Ex. Static File middleware can serve a CSS file without passing the request to MVC• Short circuiting improves response time for some requests

Page 5: ASP.NET Core Demos Part 2

MIDDLEWAREWhen a request comes in, instances of the middleware components are created as needed. Logic prior to awaiting on the RequestDelegate is thought of as reading the incoming request. Logic after that point is thought of as processing the response. This is convention; really you have access to both on either side of the delegate.A HttpContext object is passed from one component to the next giving each a chance to read, alter, or handle a request. Middleware that can fully handle a request does not have to invoke the RequestDelegate. That component becomes the last in the pipeline for the request. That’s what “short circuit” means: taking a shorter path than planned.

Page 6: ASP.NET Core Demos Part 2

MIDDLEWARE• Dependency injection works as expected• Configuring middleware should follow Options Pattern• Define a “configuring action” to set parameters• Ensures any pre-configuration is not lost by user creating new options

object• Unambiguous on lifetime of options object• Cleaner, more fluent style code• Exception: 0 to 2 options is easier to pass as parameters; no extra options

class and action• Not a hard rule, just a style guideline

Page 7: ASP.NET Core Demos Part 2

ASP.NET CORE QUICK STARTMiddleware Demo

Page 8: ASP.NET Core Demos Part 2

ASP.NET CORE QUICK STARTControllers

Page 9: ASP.NET Core Demos Part 2

CONTROLLERS

• No separate controllers for View vs API actions• Don’t have to use base class at all but provides convenient methods• A single controller can serve data to API requests and HTML to browsers• *Even though it can, that doesn’t mean it should

• Route configuration still available but attribute routing is becoming more common

Page 10: ASP.NET Core Demos Part 2

CONTROLLERS

• Web API HttpRequestMessage and HttpResponseMessage are gone (Yay!)• There is a shim available for people porting WebAPI code that used these• Preference is either concrete type or IActionResult

• ASP.NET MVC Controllers are largely unchanged except new pipeline for requests• Returning a View performs the same search for pages as before

• Razor Views have new Tag Helpers (But that’s UI and outside the scope of this talk!)• HTML tag and attribute based – similar to AngularJS• Allows validation of HTML by parsers, fewer Intellisense complaints about mismatched tags• Can still use @Html helpers; good for porting but I don’t recommend using these in new code

Page 11: ASP.NET Core Demos Part 2

ASP.NET CORE QUICK STARTControllers Demo

Page 12: ASP.NET Core Demos Part 2

ASP.NET CORE QUICK STARTEntity Framework Core

Page 13: ASP.NET Core Demos Part 2

• Everything you’ve heard is true (probably)• Complete rewrite• New architecture• Regression of capabilities

• Despite regressed functionality, the design is much more flexible with more capability• Providers allow LINQ to be translated for different data stores• Relational database base classes and SQL Server already in place; non-

relational coming soon• Developers aren’t concerned with how data gets persisted and restored when

writing logic• Smarter query evaluation, batch operations, …

ENTITY FRAMEWORK CORE

Page 14: ASP.NET Core Demos Part 2

• In Memory Provider• So great it deserved its own slide• Developers doing code first can work independent of data stores• Iterate on features without making database changes that affect others• Faster cycle to make changes and see affect without modifying and restoring

database schema• “Unit testing” becomes easier

• Most people are actually writing integration tests• Database state is deterministic• No need to reset database or worry about modifying others’ data• It becomes necessary to seed data in order to test code but bad data is no longer

cause of bugs

ENTITY FRAMEWORK CORE

Page 15: ASP.NET Core Demos Part 2

ASP.NET CORE QUICK STARTEntity Framework Core Demo

Page 16: ASP.NET Core Demos Part 2

BONUS: VISUAL STUDIO 2017 TIPSProject Dependencies & Packages

Page 17: ASP.NET Core Demos Part 2

• Adding a project reference to a project that creates a NuGet package:• Making changes in the referenced project are reflected on recompile• No need to generate the package, publish to repository, update NuGet in

dependent project• When building dependent solution as package, referenced project DLLs are

not included• Referenced project NuGet package is added as a dependency of dependent

solution’s package

• This tooling change makes it much easier to create Abstraction / Contract packages• Can be referenced by many projects, includes only Interfaces or Constants• Only the “running” project needs the implementation package• Shared logic is much more easily handled; versioning handled via package

management

VISUAL STUDIO 2017: PROJECT PACKAGES

Page 18: ASP.NET Core Demos Part 2

BONUS: VISUAL STUDIO 2017 TIPSProject Dependencies & Packages Demo

Page 19: ASP.NET Core Demos Part 2

QUESTIONS?Erik NorenSolutions ArchitectSoftware Developer 15+ YearsSoftware Tinkerer Since Commodore 64Hardware HobbyistTwitter: @ErikNorenGitHub: ErikNoren

Page 20: ASP.NET Core Demos Part 2

RESOURCES

• Demo Code Samples - https://github.com/ErikNoren/AspNetCoreDemos• ASP.NET Core – https://www.asp.net/core• ASP.NET Core Source – https://github.com/aspnet/home• Microsoft Docs – https://docs.microsoft.com/• Julie Lerman, Fantastic Data & Entity Framework Resource –

http://thedatafarm.com/blog/• Julie on Pluralsight – https://www.pluralsight.com/search?

q=julie+lerman&categories=aem-author