Categories: Design Pattern

Structural Patterns – Filter/Criteria

Filter/Criteria – According to the developer logic and decouple methods to connect, which uses the different standard rules to filter.

From Design Pattern – Structural Patterns

Structure

Filter/Criteria pattern – Class diagram UML

The Filter/Criteria pattern concept is from the Java design pattern concept. Some developers think it does not belong to the design pattern kinds, so no most .Net developers will take it and interprets it. Another interpretation which the filter concepts have been had the type of function in ASP.NET SDK, which means the Microsoft developers help the programmers achieve the Filter/Criteria pattern concepts. Still, it does not give the business logic layer(BLL) to apply because the ASP.NET SDK provides the filter, which is the ActionFilter class based on the HTTP procedures.

Suppose the developers want to combine a part of BLL to make the Filter/Criteria pattern that achieves one business management. In that case, the developers can add it to your solution reference list in your brain, and then it also can define any standard rule to filter the requests. It is not a bad idea. Sometimes, it is a good idea in a specific situation.

How to Implement

#1 Basic Sample

class Program
{
 static void Main(string[] args)
 {
  List<ObjClass> list = new List<ObjClass> {
   new ObjClass { Id = 1, FullName = "A",Sex ='F', DepartmentNo = 1, LeaveFlag = false },
   new ObjClass { Id = 2, FullName = "B",Sex ='M', DepartmentNo = 2, LeaveFlag = false },
   new ObjClass { Id = 3, FullName = "C",Sex ='F', DepartmentNo = 2, LeaveFlag = false }
  };

  IFiliterInterface iFiliterInterface = new DepartmentCriteria();

  var Result = iFiliterInterface.MeetCriteria(list,2);

  Console.WriteLine("Filiter for Department: 2");
  foreach (var item in Result)
  {
   Console.WriteLine(JsonConvert.SerializeObject(item));
  }

  iFiliterInterface = new SexyCriteria();
  Console.WriteLine("Filiter for Sex: F");
  Result = iFiliterInterface.MeetCriteria(list, 'F');

  foreach (var item in Result)
  {
   Console.WriteLine(JsonConvert.SerializeObject(item));
  }

  Console.ReadKey();
 }
}

public class ObjClass
{
 public int Id { get; set; } = 0;
 public string FullName { get; set; } = string.Empty;
 public char Sex { get; set; }
 public int DepartmentNo { get; set; } = 0;
 public bool LeaveFlag { get; set; } = false;
}

public interface IFiliterInterface
{
 List<ObjClass> MeetCriteria(List<ObjClass> obj, dynamic Target = (dynamic)null);
}

public class DepartmentCriteria : IFiliterInterface
{
 public List<ObjClass> MeetCriteria(List<ObjClass> obj, dynamic Target)
  => (from source in obj where source.DepartmentNo == Target select source).ToList();
}

public class SexyCriteria : IFiliterInterface
{
 public List<ObjClass> MeetCriteria(List<ObjClass> obj, dynamic Target)
  => (from source in obj where source.Sex.Equals(Target) select source).ToList();
}

#2 Web Application Project

Step 1 Create the Filter interface class, then adding the MeetCriteria function in the Core project.

Fig 1 Filter interface class source code

Step 2 Create the Filter class, then inherit the Filter interface class file in the Core project.

Fig 2 Create filter class object file – Solution Explorer
Fig 3 NameCriteria class file source code
Fig 4 AgeCriteria class file souce code

Step 3 Create the Filter service class, then statement the IGenericTypeRepository interface for initialization data and the Filter class.

Fig 5 FilterServices class file source code

Step 4 Create the Controller files and the View part files for the Filter pattern in the web project. It doesn’t need to register any object in the Startup.cs file.

Fig 6 Filter/Criteria pattern result

Reference

davidsky69

Recent Posts

API Gateway in .NET 5 with Ocelot

What is the API gateway? An API gateway is an API management tool that sits…

3 years ago

.NET 5 application with Onion architecture

The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…

3 years ago

SOLID Principles – Dependency inversion principle

In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…

4 years ago

SOLID Principles – Interface segregation principle

In the field of software engineering, the interface segregation principle (ISP) states that no code…

4 years ago

SOLID Principles – Liskov substitution principle

Subtype Requirement: Let  be a property provable about objects  of type T. Then  should be true for objects  of type S where S is…

4 years ago

SOLID Principles – Open-closed principle

In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…

4 years ago