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 7561549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:11:05+00:00 2026-05-30T13:11:05+00:00

I am using the repository pattern where I have one repository class per database

  • 0

I am using the repository pattern where I have one repository class per database table. I was wondering how you guys approach queries that only need to return a specific number of columns

For example say I have the following

Item Table (fictional table)

ItemId
Name
PurchaseDate
Description
Price

In my code I create an object with the fields above called Item.cs (currently not using an orm).

If I have multiple scenarios where I need to return

  1. ItemId
  2. A combination of PurchaseDate and Name
  3. ItemId and price

Which would be the best approach?

  1. Get all fields from the items table and return an Item object (1 repo query)
  2. Create three queries in Repo and return an Item object for each one
  3. Create three queries in Repo and return only what is needed?

Now imagine this scenario with a table with over 10 fields.

Personally, I like option one, but I’m not sure if there is a better way to go about this.

  • 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-30T13:11:07+00:00Added an answer on May 30, 2026 at 1:11 pm

    I personally use a generic type repository and and have my read AsQueryable()

    Here’s the interface.

    interface IRepository<T>
    {
        void Create(T item);
        IQueryable<T> Retrieve();
        void Update(T item);
        void Delete(T item);
        void SubmitChanges();
    }
    

    and here’s the implementation.

    public class PersonsRepository : IRepository<Person>
    {
        private DataContext dc;
    
        public PersonsRepository(DataContext dataContext)
        {
            dc = dataContext;
        }
    
        public void Create(Person Person)
        {
            dc.Persons.Add(Person);
        }
    
        public IQueryable<Person> Retrieve()
        {
            IQueryable<Person> Person = (from s in dc.Persons
                                           select s);
            return Person.AsQueryable();
    
        }
    
        public void Update(Person Person)
        {
            Person _Person = (from s in dc.Persons
                                where s.ID == Person.ID
                                select s).Single();
            {
                _Person.LastLogin = Person.LastLogin;
                _Person.Password = Person.Password;
                _Person.LastUpdate = Person.LastUpdate;
                // Cannot change your username.
            }
        }
    
        public void Delete(Person Person)
        {
            dc.Persons.Remove(Person);
        }
    
        public void SubmitChanges()
        {
            dc.SaveChanges();
        }
    }
    

    Now if you need to query the repository, you want to do something like this.
    forgive me, the below code is untested and I’m actually more of a VB guy 🙁
    hopefully you get the point

    public class PersonsService
    {
        private PersonRepository<Person> personRepository;
    
        public PersonService()
        {
            personRepository = new PersonRepository<Person>();
        }
    
        public UsablePerson GetPersonByID(int ID)
        {
            UsablePerson person = (from p in personRepository<Person>.Retrieve
                                   where p.ID = ID
                                   select new UsablePerson { p.FirstName, 
                                                             p.LastName, 
                                                             p.EmailAddress }).FirstOrDefault();
    
            return person;
        }
    }
    

    For my purposes, I’m using LINQ on this particular project, but this can be adapted to whatever data layer you like… that’s the beauty of a repository layer.

    From here I “personally” also have a Service Layer that deals with the nuances of data connection… things like GetPersonByID or GetPeopleSince(DateTime marker). This is where I strip out the information that I don’t need (IE: passwords) and store the remaining information in either a ViewModel or some other POCO.

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

Sidebar

Related Questions

When using the repository pattern is it recommended to have one Repository class for
I am using a simple repository pattern and have objects with a LazyList such
I'm working on a new project and I'm using the repository pattern, I have
I have an MVC-based site, which is using a Repository/Service pattern for data access.
I have implemented a DAL using Rob Conery's spin on the repository pattern (from
What is a typical approach to using the repository pattern with .NET 1.1 (C#)?
I'm trying to implement the repository pattern using a generic repository like the one
I knew than one of the benefits from using repository pattern make it easy
I have looked over the Repository pattern and I recognized some ideas that I
I am using Nhibernate for my ORM. I have a class Control that has

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.