LCJ*_*LCJ 7 .net c# design-patterns repository-pattern linq-to-sql
我有LINQ to SQL存储库实现如下.GetAll方法正在返回通用List而不是IQueryable.但是,在大多数示例和教程中,pit显示为返回IQueryable.回归IQueryable有什么好处?
using System.Linq;
namespace RepositoryLayer
{
public interface IRepository<T> where T : class
{
//System.Linq.IQueryable<T> GetAll();
System.Collections.Generic.List<T> GetAll();
}
public class Repository<T> : IRepository<T> where T : class
{
public System.Data.Linq.DataContext Context
{
get;
set;
}
//public virtual System.Linq.IQueryable<T> GetAll()
//{
// //GetAll is returning generic Queryable<T>.
// System.Linq.IQueryable<T> allItems = Context.GetTable<T>();
// return allItems;
//}
public virtual System.Collections.Generic.List<T> GetAll()
{
System.Linq.IQueryable<T> allItems = Context.GetTable<T>();
return allItems.ToList();
}
}
}
Run Code Online (Sandbox Code Playgroud)
业务层
namespace BusinessLayerProject
{
public class AccountBusiness
{
//IRepository<T>
RepositoryLayer.IRepository<RepositoryLayer.Account> accountRepository;
public AccountBusiness(RepositoryLayer.IRepository<RepositoryLayer.Account> repo)
{
accountRepository = repo;
}
//public List<RepositoryLayer.Account> GetAllAccounts()
//{
// //LibraryManagementClassesDataContext context = new LibraryManagementClassesDataContext();
// //List<RepositoryLayer.Account> accontList = context.Accounts.ToList();
// System.Linq.IQueryable<RepositoryLayer.Account> acc = accountRepository.GetAll();
// return acc.ToList();
//}
public List<RepositoryLayer.Account> GetAllAccounts()
{
List<RepositoryLayer.Account> acc = accountRepository.GetAll();
return acc;
}
}
}
Run Code Online (Sandbox Code Playgroud)
读
使用IQueryablelet LINQ通过创建不同的SQL查询将一些额外的工作移动到DB中.例如.当你尝试类似的东西GetAll().Where(condition)并使用List所有项目从DB查询和应用程序端检查条件.当你使用IQueryable它时,可以将它移动到DB,并且正确的物品直接从那里返回.
| 归档时间: |
|
| 查看次数: |
4558 次 |
| 最近记录: |