【问题标题】:How to Implement Repository FindAll() Method?如何实现存储库 FindAll() 方法?
【发布时间】:2012-06-29 11:47:15
【问题描述】:

我有以下存储库模式。要求是“查找所有者姓名为 Lijo 的所有帐户”。所以,我需要编写一个 FindAll 函数。这个函数怎么写?

约束是:

1) 客户端“BankAccountService”不应使用“DBML_Project”中的类。

2) 我们不应该使用 GetAll 方法来检索完整的帐户列表然后进行过滤。

注意:我在处理问题Polymorphism: Is ORM entity a Domain Entity or Data Entity?时遇到了这个问题

代码

namespace ApplicationService_Bank
{
public class BankAccountService
{
    RepositoryLayer.ILijosBankRepository accountRepository = new RepositoryLayer.LijosSimpleBankRepository();

    public void FreezeAllAccountsForUser(string userName)
    {
        //Should not use assembly 'DBML_Project'.

        IEnumerable<DomainEntitiesForBank.IBankAccount> accountsForUserWithNameLIJO = null;
        //accountsForUserWithNameLIJO = accountRepository.FindAll(p => p.BankUser.Name == "Lijo");
    }

}

}


namespace RepositoryLayer
{
public interface ILijosBankRepository
{
    List<DomainEntitiesForBank.IBankAccount> GetAll();
    IEnumerable<DBML_Project.BankAccount> FindAll(System.Func<DBML_Project.BankAccount, bool> predicate);
    void SubmitChanges();
        }

public class LijosSimpleBankRepository : ILijosBankRepository
{

    private IBankAccountFactory bankFactory = new MySimpleBankAccountFactory();
    public System.Data.Linq.DataContext Context
    {
        get;
        set;
    }

    public virtual List<DomainEntitiesForBank.IBankAccount> GetAll()
    {

        List<DBML_Project.BankAccount> allItems = Context.GetTable<DBML_Project.BankAccount>().ToList();
        List<DomainEntitiesForBank.IBankAccount> bankAccounts = new List<DomainEntitiesForBank.IBankAccount>();
        foreach (DBML_Project.BankAccount acc in allItems)
        {
            DomainEntitiesForBank.IBankAccount theAccount = bankFactory.CreateAccount(acc.AccountType, acc.BankAccountID, acc.Status, acc.OpenedDate, acc.AccountOwnerID);
            bankAccounts.Add(theAccount);
        }
        return bankAccounts;
    }


    public IEnumerable<DBML_Project.BankAccount> FindAll(System.Func<DBML_Project.BankAccount, bool> predicate)
    {
        //Where
        var results = Context.GetTable<DBML_Project.BankAccount>().Where(predicate);
        return results;
    }

    public virtual void SubmitChanges()
    {
        Context.SubmitChanges();
    }

}

}

阅读:

  1. Returning IEnumerable<T> vs. IQueryable<T>

  2. how to design Repository pattern to be easy switch to another ORM later?

【问题讨论】:

  • 是作业还是面试作业?
  • @JakubKonecki。不是家庭作业;不是面试问题。我只是想学习 LINQ to SQL 和分层架构与存储库模式。请看stackoverflow.com/questions/11257484/…

标签: c# .net design-patterns linq-to-sql domain-driven-design


【解决方案1】:

一种简单的方法是手动构建查询:

public class SearchCriteria
{
    public string Name { get; set; }
    // ...more
}

public IEnumerable<Entity> FindAll(SearchCriteria criteria)
{
    IQueryable<Entity> entities = _datasource.Entities; // replace with your L2S equivalent

    if (criteria.Name != null)
        entities = entities.Where(e => e.Name == criteria.Name);

    // ...more

    return entities;
}

如果您不想直接返回生成的对象,请在返回之前映射到其他对象:

return Map(entities); // IEnumerable<CustomObject> Map(IEnumerable<Entity> entities)

【讨论】:

  • 谢谢,亨利。我会试试这个。
猜你喜欢
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 1970-01-01
  • 2010-11-27
  • 2019-07-31
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多