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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:50:10+00:00 2026-05-27T08:50:10+00:00

I am fairly new to the Repository Pattern and I would like to do

  • 0

I am fairly new to the Repository Pattern and I would like to do this correctly. I am also trying to make use of Inversion of Control (also new).

I would like to make sure I am using the repository pattern correctly.

I picked this up as an example of a base interface for my repositories.

public interface IRepository<T> where T : class
{
    IEnumerable<T> Find(Expression<Func<T, bool>> where);

    IEnumerable<T> GetAll();

    void Create(T p);

    void Update(T p);
}

IPaymentRepository is intended for extensions to IRepository (although I don’t see why I would need this if I have the Find method above)

public interface IPaymentRepository : IRepository<Payment>
{
}

PaymentRepository simply reads a text file and builds a POCO.

public class PaymentRepository : IPaymentRepository
{
    #region Members

    private FileInfo paymentFile;
    private StreamReader reader;
    private List<Payment> payments;

    #endregion Members

    #region Constructors

    #endregion Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="PaymentRepository"/> class.
    /// </summary>
    /// <param name="paymentFile">The payment file.</param>
    public PaymentRepository(FileInfo paymentFile)
    {
        if (!paymentFile.Exists)
            throw new FileNotFoundException("Could not find the payment file to process.");

        this.paymentFile = paymentFile;
    }

    #region Properties

    #endregion Properties

    #region Methods

    public IEnumerable<Payment> Find(Expression<Func<Payment, bool>> where)
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Gets all payments from payment file.
    /// </summary>
    /// <returns>Collection of payment objects.</returns>
    public IEnumerable<Payment> GetAll()
    {
        this.reader = new StreamReader(this.paymentFile.FullName);
        this.payments = new List<Payment>();

        while (!reader.EndOfStream)
        {
            string line = reader.ReadLine();
            Payment payment = new Payment()
            {
                AccountNo = line.Substring(0, 11),
                Amount = double.Parse(line.Substring(11, 10))
            };

            this.payments.Add(payment);
        }

        return this.payments;
    }

    public void Create(Payment p)
    {
        throw new NotImplementedException();
    }

    public void Update(Payment p)
    {
        throw new NotImplementedException();
    }

    #endregion Methods

I would like to know how to implement the Find method. I am assuming I would call GetAll and build an internal cache to the repository. For example, I would like to find all accounts that have payments greater than $50.

  • 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-27T08:50:11+00:00Added an answer on May 27, 2026 at 8:50 am

    With your current IRepository signature you would implement it like this:

    public IEnumerable<Payment> Find(Expression<Func<Payment, bool>> where)
    {
        this.reader = new StreamReader(this.paymentFile.FullName);
        this.payments = new List<Payment>();
    
        while (!reader.EndOfStream)
        {
            string line = reader.ReadLine();
            Payment payment = new Payment()
            {
                AccountNo = line.Substring(0, 11),
                Amount = double.Parse(line.Substring(11, 10))
            };
            if (where(payment) 
            {
               this.payments.Add(payment);
            }
        }
    
        return this.payments;
    }
    

    However, If your system memory allows it, you could keep a cached list (from GetAll()) and use Find() on the list. This should be an order of magnitude faster depending on the size of your list.

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

Sidebar

Related Questions

Fairly new to Spring, so I'm having some trouble with this. I'm trying to
Fairly new to Objective C and trying to work within Storyboards given it's the
I fairly new to JQuery and perhaps trying to achieve something that might be
I'm fairly new to ruby and I've got a hash that looks like so:
I'm fairly new to entity framework and patterns. How would I insert data into
This is a bit of noob question - I'm still fairly new to C#
Fairly new to mysql, and I am lost at the moment. This is my
Fairly new to python, forgive me if this is a basic question about learning
I am using a fairly simple DI pattern to inject my data repository into
I have developed a fairly small asp.net MVC application using the repository pattern and

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.