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

EF Core with Patterns

Avatar for Anthony Sneed Anthony Sneed
February 27, 2018

EF Core with Patterns

Talk by Tony Sneed and Long Le to ASP.NET Dev Group in Dallas Feb 2018.

Avatar for Anthony Sneed

Anthony Sneed

February 27, 2018
Tweet

More Decks by Anthony Sneed

Other Decks in Technology

Transcript

  1. About Tony • Independent Consultant @
 Pioneer Natural Resources •

    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 • Principle Architect @
 Pioneer Natural Resources •

    Currently geeking out w/ MEAN & .NET Core • Open Source:
 URF.Core, Trackable Entities • blog.longle.io • Twitter: @lelong37
  3. 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
  4. EF Core: What’s Hot? • Cross Platform, Container / Cloud

    Friendly • Batching update statements • In-memory provider (mockless testing) • Mixing client / server query evaluation • Mixing raw SQL and LINQ expressions • Customizable conventions, migrations
  5. EF Core: What’s Dropped? • EDMX Models, Entity SQL (vs

    code-based modeling) • Mapping stored procedures to CUD operations • Many-to-Many without intermediate entity • Full support for geospatial • Built-in design tools and wizards • Table-per-type inheritance mapping (TPH supported)
  6. EF Core vs EF 6 • EF 6 still supported,

    but considered legacy • Use EF Core for new ASP.NET Core API’s • Feature-by-feature EF 6 vs Core comparison:
 https://docs.microsoft.com/en-us/ef/efcore-and- ef6/features
  7. EF Core 2.x: What’s New? • GroupBy query translation •

    Lazy Loading • Scaffold context and entities separately* • System.Transactions support • Table-splitting, owned types, db scalar functions • Compiled queries, context pooling * Tony’s contrib :)
  8. EF Core Power Tools • Reverse engineer • Visualize model

    • Customize entity classes (Handlebars templates*) * Tony’s contrib :)
  9. Why Patterns? • Using EF directly couples your app to

    EF • Newing up DbContext makes your app
 less testable
  10. 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); } }
  11. 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); }
  12. Helpful Frameworks • Trackable Entities: N-Tier Support for EF Core

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