如何实现 Repository FindAll() 方法?

时间:2023-02-04
本文介绍了如何实现 Repository FindAll() 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I have the following Repository Pattern. Requirement is to "Find All accounts whose owner’s name is Lijo". So, I need to write a FindAll function. How to write this function?

约束条件是:

1) 客户端BankAccountService"不应使用来自DBML_Project"的类.

1) The client "BankAccountService" should not use classes from 'DBML_Project'.

2) 我们不应该使用 GetAll 方法来收回完整的帐户列表,然后进行过滤.

2) We should NOT use GetAll method to retireve complete list of accounts and then do a filter.

注意:我在解决这个问题时遇到了这个问题 多态:ORM 实体是领域实体还是数据实体?

Note: I confronted this problem while working on the question 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. 返回 IEnumerable对比 IQueryable

如何设计 Repository 模式以便以后轻松切换到另一个 ORM?

推荐答案

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

A simple approach is to just build the query by hand:

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;
}

如果不想直接返回生成的对象,请在返回之前映射到别的东西:

If you don't want to return the generated objects directly, map to something else before you return:

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

这篇关于如何实现 Repository FindAll() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:违反 PRIMARY KEY 约束 下一篇:Linq to SQL .Sum() without group ... into

相关文章

最新文章