Friday, February 19, 2016

LINQ Architecture

LINQ Beyond Queries

  • Fluent APIs
    • A readable API : Often uses method chaining to code to read like a sentence

  • Functional Validation
    • Using lambda expressions for a declarative approach
      • Keep the code in a data structure for passive evaluation
  • Long object construction could be made readable with Fluent API as shown in following two screen shots.
    • Building the Fluent API / Internal DSL
    • Heavy use of extension methods


  • Expression : When C# compiler sees an Expression, it no longer takes converts that coe into IL rather it generates an Abstract Syntax Tree i.e. Datastructure describing that code.
  • Static Reflection : Following code shows a method namely PropertySpecifier
    • This method can fetch name of the Property passed to it in a type safe manner. 
    • i.e. If it is invoked as "PropertySpecifier(m => m.Title)" then it can fetch string name "Title" from it. 
    • This is the technique behind Entity Framework's type safe Include method.
    • Expression class can yield rich metadata about a piece of code

  • Entity Framework : Class which is used as Entity should have properties as virtual. These are used by Entity Framework to inject proxy objects for change tracking mechanism.
  • Repository Pattern
    • Makes your data appear as if it is living in Memory
    • Purpose is to hide all connection and native command complications of remote data source
    • Build custom Repositories (like IRepository)
    • Use a built in abstraction (EntityFramework : IObjectSet)
  • Unit Of Work Pattern
    • Tracks changes made to set of Repositories and commits/reconciles these changes with remote data store
    • Built in abstractions (NHibernate : ISession, EntityFrmework : ObjectContext / DatabaseContext)
  • Build custom abstractions OR borrow it from underlying technology.
    • for e.g. custom IUnitOfWork vs NHibernate : ISession or Entity Framework's ObjectContext
    • ObjectContext is harder to test as there is no built in interface.
    • If built in abstraction is used then above layers get access to all the features provided by built in abstraction. For e.g. Attach,Detach, Include operations in Entity Framework
  • DALayer supporting Composable vs Non-Composable queries
    • Composable queries are achieved by returning IQuerable
    •  Composable Pros
      • Allows Linq Provider to take holistic view of a query and perform bulk of the query in remote data provider
      • Generated queries are as efficient as possible
      • Works well with small Applications and small databases
    • Composable Cons
      • Easier to miss an Index, could be very costly in large databases
      • Predictable queries. Thus helps database optimization
  • Stopping Deferred Execution (aka Lazyness)
    • Use one of the greedy operators
      • ToList,ToArray, ToDictionary
    • Produce a concrete type
      • Sum, First, Single, Count
  • Deferred Execution 
    • Pros
      • One may not need result. (Less common scenario)
    • Cons
      • It doesn't fail fast
      • It might execute more than once
  • Include method in Entity Framework is used for
    • Loads related entities
    • Solve (N + 1) Select Problem
  • Testing : Mocks or Fakes
    • Test doubles for IEnumberable and IQuerable are easy
    • For fakes use AsQueryable to convert IEnumberable to IQueryable
  • Moq : Open source Mocking Library


No comments:

Post a Comment