如何实现资源库的FindAll()方法? [英] How to Implement Repository FindAll() Method?
问题描述
我有以下Repository模式。要求是查找所有帐户的主人的名字是Lijo。所以,我需要编写一个函数findAll。如何写这个功能呢?
约束是:
1)客户端BankAccountService不应该DBML_Project使用类。
2)我们不应该用GETALL方法retireve帐户的完整列表,然后做一个过滤器。
注:我面对这个问题,当工作在这个问题<一href="http://stackoverflow.com/questions/11257484/polymorphism-is-orm-entity-a-domain-entity-or-data-entity">Polymorphism:是ORM实体域实体或实体的数据?
code
命名空间ApplicationService_Bank
{
公共类BankAccountService
{
RepositoryLayer.ILijosBankRepository accountRepository =新RepositoryLayer.LijosSimpleBankRepository();
公共无效FreezeAllAccountsForUser(用户名字符串)
{
//不宜使用汇编DBML_Project。
IEnumerable的&LT; DomainEntitiesForBank.IBankAccount&GT; accountsForUserWithNameLIJO = NULL;
// accountsForUserWithNameLIJO = accountRepository.FindAll(P =&GT; p.BankUser.Name ==Lijo);
}
}
}
命名空间RepositoryLayer
{
公共接口ILijosBankRepository
{
名单&LT; DomainEntitiesForBank.IBankAccount&GT; GETALL();
IEnumerable的&LT; DBML_Project.BankAccount&GT;的FindAll(System.Func&LT; DBML_Project.BankAccount,布尔&GT; predicate);
无效的SubmitChanges();
}
公共类LijosSimpleBankRepository:ILijosBankRepository
{
私人IBankAccountFactory bankFactory =新MySimpleBankAccountFactory();
公共System.Data.Linq.DataContext上下文
{
得到;
组;
}
公共虚拟目录&LT; DomainEntitiesForBank.IBankAccount&GT; GETALL()
{
名单&LT; DBML_Project.BankAccount&GT; allItems = Context.GetTable&LT; DBML_Project.BankAccount&GT;()了ToList();
名单&LT; DomainEntitiesForBank.IBankAccount&GT; bankAccounts =新的名单,其中,DomainEntitiesForBank.IBankAccount&GT;();
的foreach(DBML_Project.BankAccount ACC在allItems)
{
DomainEntitiesForBank.IBankAccount theAccount = bankFactory.CreateAccount(acc.AccountType,acc.BankAccountID,acc.Status,acc.OpenedDate,acc.AccountOwnerID);
bankAccounts.Add(theAccount);
}
返回bankAccounts;
}
公开的IEnumerable&LT; DBML_Project.BankAccount&GT;的FindAll(System.Func&LT; DBML_Project.BankAccount,布尔&GT; predicate)
{
//哪里
VAR的结果= Context.GetTable&LT; DBML_Project.BankAccount&GT;()式(predicate)。
返回结果;
}
公共虚拟无效的SubmitChanges()
{
Context.SubmitChanges();
}
}
}
阅读:
-
<一个href="http://stackoverflow.com/questions/2876616/returning-ienumerablet-vs-iqueryablet/2876655#2876655">Returning IEnumerable的&LT; T&GT; VS IQueryable的&LT; T&GT;
-
<一个href="http://stackoverflow.com/questions/1507286/how-to-design-repository-pattern-to-be-easy-switch-to-another-orm-later?rq=1">how设计Repository模式很容易切换到另一个ORM以后呢?
一个简单的方法是只建立手工查询:
公共类SearchCriteria
{
公共字符串名称{;组; }
// ...更多
}
公开的IEnumerable&LT;实体&GT;的FindAll(SearchCriteria标准)
{
IQueryable的&LT;实体&GT;实体= _datasource.Entities; //替换为L2S相当于
如果(criteria.Name!= NULL)
实体= entities.Where(E =&GT; e.Name == criteria.Name);
// ...更多
返回实体;
}
如果你不想直接返回生成的对象,映射到之前你一件事返回:
返回地图(实体); // IEnumerable的&LT; CustomObject&GT;地图(IEnumerable的&LT;实体&GT;实体)
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?
Constraints are:
1) The client "BankAccountService" should not use classes from 'DBML_Project'.
2) We should NOT use GetAll method to retireve complete list of accounts and then do a filter.
Note: I confronted this problem while working on the question Polymorphism: Is ORM entity a Domain Entity or Data Entity?
CODE
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();
}
}
}
READING:
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)
这篇关于如何实现资源库的FindAll()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!