Skip to content

Instantly share code, notes, and snippets.

@haritha99ch
haritha99ch / Extending Specification Pattern with Projections.md
Last active April 21, 2024 10:19
.NET Entity Framework: Extending Specification Pattern with Projections

Extending Specification Pattern with Projections

Specification pattern is a design pattern that allows to encapsulate some pieces of domain logics into a single unit, that can be passed around the system. It's a way to take some conditional logic and put it into a reusable, composable unit.

It is important to have a good understanding of the Repository pattern and the Specification pattern in Entity Framework Core.

Ordinary Specification

In its typical form, a specification is composed of one or more criteria, allowing entities to be filtered based on these criteria. Additional properties like sorting and pagination can also be incorporated. Here's a basic implementation of a Specification class.

@haritha99ch
haritha99ch / Entity Framework: Selector Pattern In .NET.md
Last active October 19, 2023 09:27
Entity Framework: Selector pattern - Enhancing Generic Repository Pattern

Selector Pattern: Enhancing Generic Repository Pattern with Type-Safe Data Selection in Entity Framework

Introduction

The Selector Pattern is a novel approach that provides a type-safe solution for selecting properties in Entity Framework queries. It avoids the use of anonymous types, which can lead to unpredictable return types and potential issues with type safety.

Problem

When working with Entity Framework (EF), it is often necessary to select specific properties from entities. A common approach is to use anonymous types: