$30 off During Our Annual Pro Sale. View Details »

Build Web APIs Faster with .NET Core Scaffolding

Build Web APIs Faster with .NET Core Scaffolding

By Tony Sneed and Long Le

Avatar for Anthony Sneed

Anthony Sneed

June 14, 2018
Tweet

More Decks by Anthony Sneed

Other Decks in Technology

Transcript

  1. About Tony • Sr. Solutions Architect
 @ www.hilti.com • Coding

    C# since v1 • Author / Instructor:
 DevelopMentor, Wintellect • Open Source:
 URF, Trackable Entities
 ASPNET Core, EF Core • blog.TonySneed.com • Twitter: @tonysneed
  2. About Le • Sr. Solutions Architect
 @ www.hilti.com • Currently

    geeking out with MEAN & .NET Core • Open Source:
 URF.Core, Trackable Entities • blog.longle.io • Twitter: @lelong37
  3. What is .NET Core Scaffolding? • Reverse engineer entity and

    EF Core context classes from existing database • Create Web API controllers with CRUD operations based on EF Core context class
  4. Scaffolding Approaches • Visual Studio wizards
 - EF Core Power

    Tools
 - Add New Controller wizard
 - Visual Studio only
 - Windows only • Command line
 - .NET Core CLI
 - VS Code or Visual Studio
 - Cross-platform
  5. EF Core: Not Your Father’s EF • Re-written from the

    ground up! • .NET Standard 2.0 • Cross-Platform, Containers, Cloud Friendly • Modern, Modular, Extensible • Lightweight, Performant
  6. EF Core Power Tools • Reverse engineer • Visualize model

    • Customize entity classes (Handlebars templates*) * Tony’s contrib :)
  7. EF Core CLI • Cross Platform
 - Compatible with VS

    Code on Windows or Mac • Flexible Options
 - Output entities and context to different projects
  8. Project Structure • Entities: NET Standard 2.0 class library
 -

    Compatible across all flavors of .NET • DbContext: .NET Core class library
 - EF Core CLI requires .NET Core • Web API: ASP.NET Core Project
 - Reference Entities and Data projects
  9. Customizing Code Generation • Handlebars Templates
 - Double curly model

    binding
 - Partial templates
 - Helper methods • IDesignTimeServices implementation
 - services.AddHandlebarsScaffolding • https://github.com/TrackableEntities/ EntityFrameworkCore.Scaffolding.Handlebars
  10. Scaffolding Web API Controllers • Razor Templates
 - C# Syntax


    - Partial templates
 - Helper methods • .NET Core CLI
 - dotnet new -i “AspNetCore.WebApi.Templates::*" • https://github.com/TrackableEntities/ AspNetCore.ApiControllers.Templates
  11. Why Patterns? • Using EF directly couples your app to

    EF • Newing up DbContext makes your app
 less testable
  12. Sample Repository public class ProductRepository : IProductRepository { private readonly

    NorthwindContext _dbContext; public ProductRepository(NorthwindContext dbContext)
 { _dbContext = dbContext; } public async Task<Product> FindAsync(int id) { return await _dbContext.Products.FindAsync(id); } }
  13. Marking Changes public void Insert(Product product) { // Mark Added

    _dbContext.Products.Add(product); } public void Update(Product product) { // Mark Modified _dbContext.Products.Update(product); } public async Task Delete(int id) { // Mark Deleted var product = await _dbContext.Products.FindAsync(id) _dbContext.Products.Remove(product); }
  14. Helpful Frameworks • Trackable Entities: N-Tier Support for EF Core

    https://github.com/TrackableEntities/ TrackableEntities.Core