Published on

Review: Specification Pattern in C# pluralsight course

Reading time
5 min read

Introduction

I am refreshing my Domain-Driven Design knowledge with Domain-Driven Design learning path on Pluralsight. And thought to try to take notes from some of the courses and put them together on a general review.

The subject of this review is Specification Pattern in C# by Vladimir Khorikov. It can also be used as summary of the whole course.

General information about the course:

  • By the time of writing this, the course was last updated on 27 Jun 2017. While the course is old, the concept is not outdated. The Source code is built with .Net 4.6 uses NHibernate v4 for data persistence.
    If you are interested in the course. It would be a nice practice to transform the code to .Net 6 and EfCore while following up with the demos.
  • Length: Short (1h 27m)
  • Rating: 5/5
  • Level: Intermediate. It requires basic knowledge of Domain-Driven Design. And intermediate knowledge of C# programming language.

Review

Module 2 Introduction (~23min) starts with explaining what's specification pattern. It's usage scenarios. And gives introduction to demo application used in the course.
The author will implement in-memory validation and data retrieval without using the specification pattern.
This is useful as the author will later discuss the drawbacks of this implementation while refactoring it using specification pattern.

This is going to be the nature of this course. Writing code and refactoring it later. It will give you a good overview of patterns and anti-patterns. As well as why particular refactoring is needed.

Module 3 Implementing the Specification Pattern the Naïve Way (~24min) will show you how to use C# Expressions to implement specification pattern, well in a Naïve way as the title says. C# LINQ is great and powerful .Net feature. And this module explain how it works. Then you'll see some common anti-pattern when using IQueryable

Vladimir goes on about LINQ very nicely:

  • Explains difference between IEnumerable and IQueryable
  • Overview of LINQ, Expressions and Delegates
  • Overview of how LINQ works with ORMs with highlight of limitations when ORMs translates LINQ to SQL operations
  • The connection between Expressions and Delegates

After setting up the ground knowledge about LINQ. You'll refactor the code from previous module 2 using C# plain expressions.

Things I liked here are:

  • Removing duplication code in domain model
  • Overcome inefficient database queries (introduced in Module 2)
  • Explains the drawbacks of this approach. And how plain C# expressions don't provide enough encapsulation. Which results in client code (WPF/MVVM in this case) has to deal with low level implementation details like compiling expressions using Expression<T>.Compile() method.

To improve on top of C# expressions. You'll go through another round of refactoring to increase level of abstraction by replacing plain C# expressions with Generic Specification. With this update in place. You'll learn the drawbacks of this approach. And why this solution isn't much different from previous one as Generic Specification doesn't contain any knowledge about the domain, thus it doesn't provide actual encapsulation and acts as thin wrapper on top of expressions.

Here the author takes the opportunity to explains how specifications should be look like and the usage of strongly typed specifications. This will be covered on next module (Module 04).

Closing this module with a nice highlight of common anti-pattern by returning IQueryable from repository methods. Despite it seems like powerful approach. You'll learn why it is considered anti-pattern:

  • How it could cause bugs/errors/crashes in your code.
  • How it violates Liskov Substitution Principle (LSP from SOLID principles) by providing the ability to execute functionality it cannot provide.

Finishing up by showing the difference between returning IQueryable, IEnumerable and IReadOnlyList from public methods. And what issues you could run into when returning IEnumerable or IQueryable from repository public methods.

Module 4 Refactoring Towards Better Encapsulation (~38m) is the last module. Here you'll wrap up by refactoring the previous code to properly implement specification pattern while avoiding domain knowledge duplication. And working efficiently with the database.

Major learning points here are:

  • Strongly typed specification. And how it overcomes anti-patterns (presented in previous module). Highlighting similarities and differences between base Specification class implementation and Generic Specification class.
  • Quick walk-through over General guidelines when implementing Specifications
  • Create combine specification (using And, Or and Not) to create more sophisticated queries.
  • When not to use specifications
  • Combine specification with regular filtration/pagination
  • How to use specification with aggregate of classes

Verdict

What I liked about this course:

  • Each demo is gradually built by updating/enhancing previous demo
  • A recap of each demo to highlight changes/enhancements
  • Provide explanations/reasons of design decisions made in different demos.

Vladimir Khorikov is really good at taking you step by step on refactoring demos. Exploring Whys and showing Hows. I have noticed that in several of his courses. Very consistent author/trainer.

The course didn't need a review :). It has high rating plus it has been there for quite sometime. It's a useful learning material. Totally worth the time. And It didn't just refresh my knowledge but also taught me why certain areas are done they way they are.