c#-4.0 – 用于访问文本文件的存储库模式

我是Repository Pattern的新手,我想正确地做到这一点.我也在尝试使用Inversion of Control(也是新的).

我想确保我正确使用存储库模式.

我选择了这个作为我的存储库的基本接口的示例.

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用于IRepository的扩展(虽然我不明白为什么我需要这个,如果我有上面的Find方法)

public interface IPaymentRepository : IRepository<Payment>
{
}

PaymentRepository只是读取一个文本文件并构建一个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

我想知道如何实现Find方法.我假设我会调用GetAll并为存储库构建内部缓存.例如,我想查找付款金额超过50美元的所有帐户.

解决方法

使用您当前的IRepository签名,您可以像这样实现它:

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,10))
        };
        if (where(payment) 
        {
           this.payments.Add(payment);
        }
    }

    return this.payments;
}

但是,如果您的系统内存允许,您可以保留缓存列表(来自GetAll())并使用列表中的Find().根据列表的大小,这应该快一个数量级.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...