Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 965807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:03:27+00:00 2026-05-16T02:03:27+00:00

I have repositories (e.g. ContactRepository, UserRepository and so forth) which encapsulate data access to

  • 0

I have repositories (e.g. ContactRepository, UserRepository and so forth) which encapsulate data access to the domain model.

When I was looking at searching for data, e.g.

  • finding a contact whose first name
    starts with XYZ
  • a contact whose birthday is after
    1960

    (etc),

I started implementing repository methods such as FirstNameStartsWith(string prefix) and YoungerThanBirthYear(int year), basically following the many examples out there.

Then I hit a problem – what if I have to combine multiple searches? Each of my repository search methods, such as above, only return a finite set of actual domain objects. In search for a better way, I started writing extension methods on IQueryable<T>, e.g. this:

public static IQueryable<Contact> FirstNameStartsWith(
               this IQueryable<Contact> contacts, String prefix)
{
    return contacts.Where(
        contact => contact.FirstName.StartsWith(prefix));
}        

Now I can do things such as

ContactRepository.GetAll().FirstNameStartsWith("tex").YoungerThanBirthYear(1960);

However, I found myself writing extension methods (and inventing crazy classes such as ContactsQueryableExtensions all over, and I lose the “nice grouping” by having everything in the appropriate repository.

Is this really the way to do it, or is there a better way to achieve the same goal?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:03:28+00:00Added an answer on May 16, 2026 at 2:03 am

    @Alex – i know this is an old question, but what I would be doing would be letting the Repository do really simple stuff only. This means, get all records for a table or view.

    Then, in the SERVICES layer (you are using an n-tiered solution, right? 🙂 ) i would be handling all the ‘special’ query stuff there.

    Ok, example time.

    Repository Layer

    ContactRepository.cs
    
    public IQueryable<Contact> GetContacts()
    {
        return (from q in SqlContext.Contacts
                select q).AsQueryable();
    }
    

    Nice and simple. SqlContext is the instance of your EF Context .. which has an Entity on it called Contacts .. which is basically your sql Contacts class.

    This means, that method basically is doing: SELECT * FROM CONTACTS … but it’s not hitting the database with that query .. it’s only a query right now.

    Ok .. next layer.. KICK … up we go (Inception anyone?)

    Services Layer

    ContactService.cs
    
    public  ICollection<Contact> FindContacts(string name)
    {
        return FindContacts(name, null)
    }
    
    public ICollection<Contact> FindContacts(string name, int? year)
    {
       IQueryable<Contact> query = _contactRepository.GetContacts();
       
       if (!string.IsNullOrEmpty(name))
       {
           query = from q in query
                   where q.FirstName.StartsWith(name)
                   select q;
       }
    
       if (int.HasValue)
       {
           query = from q in query
                   where q.Birthday.Year <= year.Value
                   select q);
        }
    
        return (from q in query
                select q).ToList();
    }
    

    Done.

    So lets recap. First, we start our with a simple ‘Get everything from contacts‘ query. Now, if we have a name provided, lets add a filter to filter all contacts by name. Next, if we have a year provided, then we filter the birthday by Year. Etc. Finally, we then hit the DB (with this modified query) and see what results we get back.

    NOTES:-

    • I’ve omitted any Dependency Injection for simplicity. It’s more than highly recommended.
    • This is all pseduo-code. Untested (against a compiler) but you get the idea ….

    Takeaway points

    • The Services layer handles all the smarts. That is where you decide what data you require.
    • The Repository is a simple SELECT * FROM TABLE or a simple INSERT/UPDATE into TABLE.

    Good luck 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have repositories which have a Save method. They also throw a Created event
Many languages have standard repositories where people donate useful libraries that they want others
Is it possible to have 2 git repositories in one directory? I'd think not,
I have a set of mercurial repositories being served online with hgwebdir.cgi. I would
I have used traditional version control systems to maintain source code repositories on past
I currently have a couple of SVN repositories hosted at Unfuddle and I'd like
we have a lot of users running in different shared and solo-owned repositories in
Where I work I have several projects in separate repositories. Each project shares a
The problem: I often have to update two or more repositories: One for the
Around here we have been working with a bunch of Visual Source Safe repositories

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.