asp.net core demos part 2
TRANSCRIPT
ASP.NET CORE QUICK START
ERIK NORENSolutions ArchitectSoftware Developer 15+ YearsSoftware Tinkerer Since Commodore 64Hardware HobbyistTwitter: @ErikNorenGitHub: ErikNoren
ASP.NET CORE QUICK STARTMiddleware
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
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.
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
ASP.NET CORE QUICK STARTMiddleware Demo
ASP.NET CORE QUICK STARTControllers
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
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
ASP.NET CORE QUICK STARTControllers Demo
ASP.NET CORE QUICK STARTEntity Framework Core
• 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
• 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
ASP.NET CORE QUICK STARTEntity Framework Core Demo
BONUS: VISUAL STUDIO 2017 TIPSProject Dependencies & Packages
• 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
BONUS: VISUAL STUDIO 2017 TIPSProject Dependencies & Packages Demo
QUESTIONS?Erik NorenSolutions ArchitectSoftware Developer 15+ YearsSoftware Tinkerer Since Commodore 64Hardware HobbyistTwitter: @ErikNorenGitHub: ErikNoren
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