Owin model and Kestrell request handling

  • OWIN – Open Web Interface for .NET
  • allows web apps to be decoupled from web servers
  • defines standard way for middleware to be used in a pipeline to handle requests and provide responses
  • ASP.NET Core applications can interoperate with OWIN-based applications, servers and middleware

Microsoft.AspNetCore.Owin package provides two adapter implementations:

  • ASP.NET Core to OWIN
  • OWIN to ASP.NET Core

This allows ASP.NET Core to be hosted on top of an OWIN compatible server/host or for other OWIN compatible components to be run on top of ASP.NET Core.

Further read:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/owin?view=aspnetcore-6.0

Expression Trees

  • ‘tree like’ data structure
  • each node is expression (method call, equality comparison, binary operation etc.) representing a piece of code
  • you can compile expression tree and run code it represent
  • enables dynamic code generation at runtime
  • LINQ queries on DBs are implemented with Expression Trees
  • used in the dynamic language runtime (DLR)
  • anonymous lambda expression can be converted to ExpressionTree via compiler (i.e. x => x*2 can be represented as tree instead of delegate or anonymous lambda function)

Further read:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/

Linq

  • consistent model for working with data from various kinds of sources and formats (XML, SQL, ADO.NET, Datasets, .NET collections etc)
  • 3 stages of working with LINQ:
    • obtain data source
    • create the query
    • execute the query
  • data sources implements IEnumerable<> and are called queryable types
  • IQueryable<> interface inherits from IEnumerable<>
  • generally it is better to use LINQ via IQueryable<> which produces deferred query instead of producing result immediately
  • queries can be executed multiple times
  • queries can be forced immediate execution via Count(), Max(), ToList() or ToArray() etc.
  • LINQ to DB libraries (like EntityFramework) usually are implemented via Expression Trees

Entity Framework: eager and lazy loading

  • lazy loading:
    • delaying load of data until we specifically request for it
    • i.e. accessing customer.Address loads Address of a customer in the background via separate SQL query
    • property needs to be public, virtual
    • context.Configuration.ProxyCreationEnabled should be true
    • context.Configuration.LazyLoadingEnabled should be true
    • to disable lazy loading completely Context.Configuration.LazyLoadingEnabled = false;
  • eager loading
    • opposite of lazy loading
    • loads related entities as part of the query
    • done via .Include() query

Further read:

https://www.entityframeworktutorial.net/lazyloading-in-entity-framework.aspx

https://www.entityframeworktutorial.net/eager-loading-in-entity-framework.aspx