Click here to Skip to main content
Licence CPOL
First Posted 8 Jan 2009
Views 8,917
Downloads 74
Bookmarked 15 times

Querying In Memory Objects

By | 8 Jan 2009 | Article
How to use custom criteria classes to perform queries over in memory objects

Introduction

Querying in memory objects presents a wide range of advantages over traditional database queries. Some of the most interesting advantages are listed below:

  • Business developers need not get any knowledge about query languages like T-SQL or XPath.
  • Promote decoupling between the data access and business layer.
  • Decrease the numbers of open connections to database servers.
  • Increase the performance by reducing the cost of IO operations.
  • More domain specific, etc., etc. 

In this article, I'll show you how to create criteria classes to perform queries against generics collections.

Background

Basic knowledge of .NET Generics.

Using the Code

For this article, I used AdventureWorks database, which you can download from here.
The sample project is pretty straightforward. It's just about performing queries like...

SELECT * FROM Employees WHERE FirstName LIKE 'Vilma'

...but instead of using SQL database, we'll use a generic collection of Employees as a data source.

The sample application consists of four libraries:

  • BusinessLayer (Business objects, criteria classes)
  • DataAccessLayer (Raw data access)
  • Repository (This library kept in memory all of the Employee instances within a generic collection)
  • WindowsFrontEnd (A simple Windows App to find customers based on a specific search criteria). 

Now let's take a look at the main code blocks:

//This class should be the base class for all of the criteria classes.
//At runtime 'T' will be supplanted for a concrete type in our case 'Employee'.
public abstract class BaseCriteria<T>
{
    //We mark this method as abstract to force derived classes to override it.
    public abstract bool IsMatch(T entity);
}

A derived class from BaseCriteria should look like this:

public class FindByFirstNameLike : BaseCriteria<Employee>
{
    _firstName;

    public FindByFirstNameLike(string firstName)
    {
        _firstName = firstName.ToLower().Trim();
    }
 
    public override bool IsMatch(Employee entity)
    {
        return entity.FirstName.ToLower().StartsWith(_firstName) ||
        string.IsNullOrEmpty(_firstName);
    }
}

And finally we can see a criteria class in action:

private void ApplyFilters()
{
    //Get all of the employees from the repository.
    List<Employee> employees = Repository.Employees.GetEmployees();

    //Create an empty list to store the search results.
    List<Employee> searchResult = new List<Employee>();

    //Here we use an instance of the criteria class to find all of the Employees
    //within the collection who's first name is Peter.        
    searchResult = employees.FindAll(
    new Employee.FindByFirstNameLike("Peter").IsMatch);

    //Finally show the results in a DataGridView
    dgvResult.DataSource = searchResult;
    dgvResult.Refresh();
}

In the download code, I included a Winform App that contains a sample of how to perform nested queries over the search results list.

I hope this article helps you to explore the power of .NET Generics.
If you have any doubts or suggestions about the code, please let me know.

History

  • 9th January, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Alejandro Miralles

Architect
FPA Software
Argentina Argentina

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralOne little point PinmemberMustafa Ismail Mustafa21:07 12 Jan '09  
GeneralRe: One little point PinmemberAlejandro Miralles0:54 13 Jan '09  
GeneralLambda Expression PinmemberPavel Pawlowski2:08 9 Jan '09  
GeneralRe: Lambda Expression PinmemberAlejandro Miralles5:41 9 Jan '09  
GeneralRe: Lambda Expression Pinmembertkrafael_net6:01 9 Jan '09  
GeneralExcellent idea! Pinmemberratygan1:07 9 Jan '09  
GeneralRe: Excellent idea! PinmemberAlejandro Miralles6:05 9 Jan '09  
GeneralInterface? [modified] Pinmemberbombdrop0:52 9 Jan '09  
GeneralRe: Interface? PinmemberAlejandro Miralles6:02 9 Jan '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 9 Jan 2009
Article Copyright 2009 by Alejandro Miralles
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid