加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:26:29 所属栏目:百科 来源:网络整理
导读:我是Repository Pattern的新手,我想正确地做到这一点.我也在尝试使用Inversion of Control(也是新的). 我想确保我正确使用存储库模式. 我选择了这个作为我的存储库的基本接口的示例. public interface IRepositoryT where T : class{ IEnumerableT Find(Expr
我是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().根据列表的大小,这应该快一个数量级.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读