Thursday, April 18, 2019

Five days ago I saw Chris Woodruff speak on .NET Core 2.2 APIs at the Twin Cities Code Camp.

Chris works for JetBrains on ReSharper, Rider, and DataGrip which like Rider is a cross-platform IDE only DataGrip is slanted for database stuff. There is the concept of compiled events in Entity Framework wherein LINQ statements are precompiled making them thirty to forty percent faster. Chinook is a database of music information. I think you may use it as a dummy database to make a project with instead of the Northwind database for example. .NET Core does not used the GAC due to being cross-platform. Stack Exchange, the owner of Stack Overflow, developed Dapper and Stack Overflow runs on top of Dapper. Chris used the term "Supervisor" to denote how Entity Models get mapped to what he called "View Models" (DTOs). One might use AutoMapper orchestrations for the "Supervisor" in an application I suppose. In Chris' implementation he had a giant class for it. This was to avoid circular references. If a person has albums and each album has a person... two separate mapping classes could cause heartache. Entity Framework Core 2.2 has DBContext pooling which will offer some performance improvements. Caching is important when designing architecture and there are three shapes to consider, response caching, in-memory caching, and distributed caching.

Cache-Control: public, max-age: 60

 
 

...is an example of response caching in an HTTP response header. You may have an attribute at a Controller for this sort of thing like such:

[ResponseCache(Duration = 60)]

 
 

Alternatively, you may explicitly not cache.

[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]

 
 

An example of in-memory caching is having something like this in the middleware:

services.AddMemoryCache();

 
 

Chris projected the following example of using the cache:

public async Task<Genre> GetByIdAsync(int id, CancellationToken ct =
      default(CancellationToken))
{
   var cachedGenre = _cache.Get<Genre>(id);
   if (cachedGenre != null)
   {
      return cachedGenre;
   }
   else
   {
      var dbGenre = await _context.Genre.FindAsync(id);
      var cachedEntryOptions = new MemoryCacheEntryOptions().
            SetSlidingExpiration(TimeSpan.FromSeconds(604800));
      _cache.Set(dbGenre.GenreId, dbGenre, cacheEntryOptions);
      return dbGenre;
   }
}

 
 

Microsoft.Extensions.Caching.Memory is the namespace for this stuff. Distributed caching is really external caching so think of local Redis Cache, using SQL Server to cache, Azure Redis Caching, Amazon ElastiCache, Google Cloud Memcache, etc. Chris Woodruff has some Channel 9 appearances (training videos) you can find. For testing Chris recommends looping in both the Alba of Jeremy Miller and Microsoft.AspNetCore.TestHost. You may have a Theory instead of a Fact in an xUnit test as seen here:

Theory implies that the test is an integration test. I don't really see any other distinction. I don't see a distinction in other syntax. The talk by Chris Woodruff was one of a handful I saw at the Twin Cities Code Camp. This details the first talk I saw that day and kinda speaks to what the event was in the big picture some too.

 
 

Addendum 2/10/2020: The description of Theory instead of a Fact here is probably bad. See this instead.

No comments:

Post a Comment