본문 바로가기

Backend/.NET

Generic Repository Specification Pattern - Adding Filtering

반응형

One of the drawbacks about using the specification pattern is that we lose some of the functions provided by Entity Framework. Filtering the result is one of them and we will see how we can manually create the feature with the specification pattern.

Creating a Generic Repository Pattern

 

Relational DB - Getting Data including Data from Other Entities (Generic Repository Pattern)

Let's see how we can get data from a primary entity that has reference to other entities with the generic repository pattern Implementation Creating an Application How to create .NET web-API Setting up development tools When working with .NET, we need tool

jin-co.tistory.com

 

Go to the individual specification class under the core folder and add parameters to filter (in this example, 'targetId' and 'typeId') and add the code that implement the where clause in the base as shown below

: base(x => 
  (!targetId.HasValue || x.ItemTargetId == targetId) &&
  (!typeId.HasValue || x.ItemTypeId == typeId)
)

Go to the controller and add filtering parameters and pass that parameters to the individual specification class

Run

Move to the API folder

cd /API

And run the app

dotnet watch

In this writing, we have seen one of ways to add filtering with the specification pattern.

 

728x90
반응형