微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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().根据列表的大小,这应该快一个数量级.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐