C# ASP.NET MVC通用存储库
我搜索了几个小时,读了几篇关于构建通用存储库(GR)的文章。据我所知,GRs用于存在类似代码的情况。例如,根据表的id从表中获取一行或整个表。然而,我仍然无法理解如何实现这一点C# ASP.NET MVC通用存储库,c#,.net,asp.net,asp.net-mvc,repository-pattern,C#,.net,Asp.net,Asp.net Mvc,Repository Pattern,我搜索了几个小时,读了几篇关于构建通用存储库(GR)的文章。据我所知,GRs用于存在类似代码的情况。例如,根据表的id从表中获取一行或整个表。然而,我仍然无法理解如何实现这一点 public interface IEntity<T> where T : class{ IQueryable<T> getAll(); T GetById(int id); } public class Repository<T> where T : IEnti
public interface IEntity<T> where T : class{
IQueryable<T> getAll();
T GetById(int id);
}
public class Repository<T> where T : IEntity<T>{
northWindDataContext nwdc = new northWindDataContext();
public IQueryable<T> getAll()
{
//code to retrive the whole table
}
public T GetById(int id)
{
//code to retrieve a single row (don't know how to do)
}
}
公共接口接口,其中T:class{
IQueryable getAll();
T GetById(int-id);
}
公共类存储库,其中T:IEntity{
northWindDataContext nwdc=新的northWindDataContext();
公共IQueryable getAll()
{
//用于检索整个表的代码
}
公共T GetById(int-id)
{
//检索单行的代码(不知道如何做)
}
}
然后,我想做一些类似的事情:
Repository<User> rep = new Repository<User>();
IQueryable<User> = rep.getAll<User>;
User user = rep.GetById(35);
Repository=newrepository();
IQueryable=rep.getAll;
User=rep.GetById(35);
请任何人向我解释如何完成这项任务?我一直在使用通用存储库,如前所述。我一直在使用作者的设计来编写代码,效果非常好。以下是我一直使用的代码: i假设性
namespace New_Repository_Design.Repositories
{
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using Specifications;
public interface IRepository
{
/// <summary>
/// Gets entity by key.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="keyValue">The key value.</param>
/// <returns></returns>
TEntity GetByKey<TEntity>(object keyValue) where TEntity : class;
/// <summary>
/// Gets the query.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <returns></returns>
IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class;
/// <summary>
/// Gets the query.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
IQueryable<TEntity> GetQuery<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class;
/// <summary>
/// Gets the query.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <returns></returns>
IQueryable<TEntity> GetQuery<TEntity>(ISpecification<TEntity> criteria) where TEntity : class;
/// <summary>
/// Gets one entity based on matching criteria
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <returns></returns>
TEntity Single<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class;
/// <summary>
/// Gets single entity using specification
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <returns></returns>
TEntity Single<TEntity>(ISpecification<TEntity> criteria) where TEntity : class;
/// <summary>
/// Firsts the specified predicate.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
TEntity First<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class;
/// <summary>
/// Gets first entity with specification.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <returns></returns>
TEntity First<TEntity>(ISpecification<TEntity> criteria) where TEntity : class;
/// <summary>
/// Adds the specified entity.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="entity">The entity.</param>
void Add<TEntity>(TEntity entity) where TEntity : class;
/// <summary>
/// Attaches the specified entity.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="entity">The entity.</param>
void Attach<TEntity>(TEntity entity) where TEntity : class;
/// <summary>
/// Deletes the specified entity.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="entity">The entity.</param>
void Delete<TEntity>(TEntity entity) where TEntity : class;
/// <summary>
/// Deletes one or many entities matching the specified criteria
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
void Delete<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class;
/// <summary>
/// Deletes entities which satify specificatiion
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
void Delete<TEntity>(ISpecification<TEntity> criteria) where TEntity : class;
/// <summary>
/// Updates changes of the existing entity.
/// The caller must later call SaveChanges() on the repository explicitly to save the entity to database
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="entity">The entity.</param>
void Update<TEntity>(TEntity entity) where TEntity : class;
/// <summary>
/// Finds entities based on provided criteria.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <returns></returns>
IEnumerable<TEntity> Find<TEntity>(ISpecification<TEntity> criteria) where TEntity : class;
/// <summary>
/// Finds entities based on provided criteria.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <returns></returns>
IEnumerable<TEntity> Find<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class;
/// <summary>
/// Finds one entity based on provided criteria.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <returns></returns>
TEntity FindOne<TEntity>(ISpecification<TEntity> criteria) where TEntity : class;
/// <summary>
/// Finds one entity based on provided criteria.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <returns></returns>
TEntity FindOne<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class;
/// <summary>
/// Gets all.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <returns></returns>
IEnumerable<TEntity> GetAll<TEntity>() where TEntity : class;
/// <summary>
/// Gets a collection of entity with paging support
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="orderBy">The order by.</param>
/// <param name="pageIndex">Index of the page.</param>
/// <param name="pageSize">Size of the page.</param>
/// <param name="sortOrder">The sort order.</param>
/// <returns></returns>
IEnumerable<TEntity> Get<TEntity>(Expression<Func<TEntity, string>> orderBy, int pageIndex, int pageSize, SortOrder sortOrder = SortOrder.Ascending) where TEntity : class;
/// <summary>
/// Gets a collection of entity base on criteria with paging support
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <param name="orderBy">The order by.</param>
/// <param name="pageIndex">Index of the page.</param>
/// <param name="pageSize">Size of the page.</param>
/// <param name="sortOrder">The sort order.</param>
/// <returns></returns>
IEnumerable<TEntity> Get<TEntity>(Expression<Func<TEntity, bool>> criteria, Expression<Func<TEntity, string>> orderBy, int pageIndex, int pageSize, SortOrder sortOrder = SortOrder.Ascending) where TEntity : class;
/// <summary>
/// Gets entities which satify specification
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <param name="orderBy">The order by.</param>
/// <param name="pageIndex">Index of the page.</param>
/// <param name="pageSize">Size of the page.</param>
/// <param name="sortOrder">The sort order.</param>
/// <returns></returns>
IEnumerable<TEntity> Get<TEntity>(ISpecification<TEntity> criteria, Expression<Func<TEntity, string>> orderBy, int pageIndex, int pageSize, SortOrder sortOrder = SortOrder.Ascending) where TEntity : class;
/// <summary>
/// Counts the specified entities.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <returns></returns>
int Count<TEntity>() where TEntity : class;
/// <summary>
/// Counts entities with the specified criteria.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <returns></returns>
int Count<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class;
/// <summary>
/// Counts entities satifying specification.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="criteria">The criteria.</param>
/// <returns></returns>
int Count<TEntity>(ISpecification<TEntity> criteria) where TEntity : class;
}
}
namespace New_Repository_Design.Repositories
{
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Design.PluralizationServices;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using Specifications;
public sealed class DomainRepository : IRepository
{
private readonly PluralizationService _pluralizer = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en"));
private readonly string _connectionStringName;
private ObjectContext _objectContext;
/// <summary>
/// Initializes a new instance of the <see cref="GenericRepository<TEntity>"/> class.
/// </summary>
public DomainRepository()
: this(string.Empty)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="GenericRepository<TEntity>"/> class.
/// </summary>
/// <param name="connectionStringName">Name of the connection string.</param>
public DomainRepository(string connectionStringName)
{
this._connectionStringName = connectionStringName;
}
/// <summary>
/// Initializes a new instance of the <see cref="GenericRepository"/> class.
/// </summary>
/// <param name="objectContext">The object context.</param>
public DomainRepository(ObjectContext objectContext)
{
if (objectContext == null)
throw new ArgumentNullException("objectContext");
this._objectContext = objectContext;
}
public TEntity GetByKey<TEntity>(object keyValue) where TEntity : class
{
EntityKey key = GetEntityKey<TEntity>(keyValue);
object originalItem;
if (ObjectContext.TryGetObjectByKey(key, out originalItem))
{
return (TEntity)originalItem;
}
return default(TEntity);
}
public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
{
var entityName = GetEntityName<TEntity>();
var q = ObjectContext.CreateQuery<TEntity>(entityName);
//return ObjectContext.CreateQuery<TEntity>(entityName);
return q;
}
public IQueryable<TEntity> GetQuery<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
{
return GetQuery<TEntity>().Where(predicate);
}
public IQueryable<TEntity> GetQuery<TEntity>(ISpecification<TEntity> specification) where TEntity : class
{
return specification.SatisfyingEntitiesFrom(GetQuery<TEntity>());
}
public IEnumerable<TEntity> Get<TEntity>(Expression<Func<TEntity, string>> orderBy, int pageIndex, int pageSize, SortOrder sortOrder = SortOrder.Ascending) where TEntity : class
{
if (sortOrder == SortOrder.Ascending)
{
return GetQuery<TEntity>().OrderBy(orderBy).Skip(pageIndex).Take(pageSize).AsEnumerable();
}
return GetQuery<TEntity>().OrderByDescending(orderBy).Skip(pageIndex).Take(pageSize).AsEnumerable();
}
public IEnumerable<TEntity> Get<TEntity>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, string>> orderBy, int pageIndex, int pageSize, SortOrder sortOrder = SortOrder.Ascending) where TEntity : class
{
if (sortOrder == SortOrder.Ascending)
{
return GetQuery<TEntity>().Where(predicate).OrderBy(orderBy).Skip(pageIndex).Take(pageSize).AsEnumerable();
}
return GetQuery<TEntity>().Where(predicate).OrderByDescending(orderBy).Skip(pageIndex).Take(pageSize).AsEnumerable();
}
public IEnumerable<TEntity> Get<TEntity>(ISpecification<TEntity> specification, Expression<Func<TEntity, string>> orderBy, int pageIndex, int pageSize, SortOrder sortOrder = SortOrder.Ascending) where TEntity : class
{
if (sortOrder == SortOrder.Ascending)
{
return specification.SatisfyingEntitiesFrom(GetQuery<TEntity>()).OrderBy(orderBy).Skip(pageIndex).Take(pageSize).AsEnumerable();
}
return specification.SatisfyingEntitiesFrom(GetQuery<TEntity>()).OrderByDescending(orderBy).Skip(pageIndex).Take(pageSize).AsEnumerable();
}
public TEntity Single<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class
{
return GetQuery<TEntity>().SingleOrDefault<TEntity>(criteria);
}
public TEntity Single<TEntity>(ISpecification<TEntity> criteria) where TEntity : class
{
return criteria.SatisfyingEntityFrom(GetQuery<TEntity>());
}
public TEntity First<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
{
return GetQuery<TEntity>().FirstOrDefault(predicate);
}
public TEntity First<TEntity>(ISpecification<TEntity> criteria) where TEntity : class
{
return criteria.SatisfyingEntitiesFrom(GetQuery<TEntity>()).FirstOrDefault();
}
public void Add<TEntity>(TEntity entity) where TEntity : class
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
ObjectContext.AddObject(GetEntityName<TEntity>(), entity);
}
public void Attach<TEntity>(TEntity entity) where TEntity : class
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
ObjectContext.AttachTo(GetEntityName<TEntity>(), entity);
}
public void SaveChanges()
{
this.ObjectContext.SaveChanges();
}
public void Delete<TEntity>(TEntity entity) where TEntity : class
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
ObjectContext.DeleteObject(entity);
}
public void Delete<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class
{
IEnumerable<TEntity> records = Find<TEntity>(criteria);
foreach (TEntity record in records)
{
Delete<TEntity>(record);
}
}
public void Delete<TEntity>(ISpecification<TEntity> criteria) where TEntity : class
{
IEnumerable<TEntity> records = Find<TEntity>(criteria);
foreach (TEntity record in records)
{
Delete<TEntity>(record);
}
}
public IEnumerable<TEntity> GetAll<TEntity>() where TEntity : class
{
return GetQuery<TEntity>().AsEnumerable();
}
public void Update<TEntity>(TEntity entity) where TEntity : class
{
var fqen = GetEntityName<TEntity>();
object originalItem;
EntityKey key = ObjectContext.CreateEntityKey(fqen, entity);
if (ObjectContext.TryGetObjectByKey(key, out originalItem))
{
ObjectContext.ApplyCurrentValues(key.EntitySetName, entity);
}
}
public IEnumerable<TEntity> Find<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class
{
return GetQuery<TEntity>().Where(criteria);
}
public TEntity FindOne<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class
{
return GetQuery<TEntity>().Where(criteria).FirstOrDefault();
}
public TEntity FindOne<TEntity>(ISpecification<TEntity> criteria) where TEntity : class
{
return criteria.SatisfyingEntityFrom(GetQuery<TEntity>());
}
public IEnumerable<TEntity> Find<TEntity>(ISpecification<TEntity> criteria) where TEntity : class
{
return criteria.SatisfyingEntitiesFrom(GetQuery<TEntity>());
}
public int Count<TEntity>() where TEntity : class
{
return GetQuery<TEntity>().Count();
}
public int Count<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class
{
return GetQuery<TEntity>().Count(criteria);
}
public int Count<TEntity>(ISpecification<TEntity> criteria) where TEntity : class
{
return criteria.SatisfyingEntitiesFrom(GetQuery<TEntity>()).Count();
}
private ObjectContext ObjectContext
{
get
{
return this._objectContext;
}
}
private string GetEntityName<TEntity>() where TEntity : class
{
return string.Format("{0}.{1}", ObjectContext.DefaultContainerName, _pluralizer.Pluralize(typeof(TEntity).Name));
}
private EntityKey GetEntityKey<TEntity>(object keyValue) where TEntity : class
{
var entitySetName = GetEntityName<TEntity>();
var objectSet = ObjectContext.CreateObjectSet<TEntity>();
var keyPropertyName = objectSet.EntitySet.ElementType.KeyMembers[0].ToString();
var entityKey = new EntityKey(entitySetName, new[] { new EntityKeyMember(keyPropertyName, keyValue) });
return entityKey;
}
}
}`
namespace New\u Repository\u Design.Repositories
{
使用制度;
使用System.Collections.Generic;
使用System.Data.SqlClient;
使用System.Linq;
使用System.Linq.Expressions;
使用说明书;
公共接口假定
{
///
///按键获取实体。
///
///实体的类型。
///关键值。
///
TEntity GetByKey(object keyValue),其中TEntity:class;
///
///获取查询。
///
///实体的类型。
///
IQueryable GetQuery(),其中tenty:class;
///
///获取查询。
///
///实体的类型。
///谓词。
///
IQueryable GetQuery(表达式谓词),其中tenty:class;
///
///获取查询。
///
///实体的类型。
///标准。
///
IQueryable GetQuery(IsSpecification criteria),其中tenty:class;
///
///根据匹配条件获取一个实体
///
///实体的类型。
///标准。
///
TEntity Single(表达式标准),其中TEntity:class;
///
///使用规范获取单个实体
///
///实体的类型。
///标准。
///
TEntity Single(指定标准),其中TEntity:类别;
///
///首先是指定的谓词。
///
///实体的类型。
///谓词。
///
tenty-First(表达式谓词),其中tenty:class;
///
///获取具有规范的第一个实体。
///
///实体的类型。
///标准。
///
张力优先(I规格标准),其中张力:等级;
///
///添加指定的实体。
///
///实体的类型。
///实体。
void Add(tenty实体),其中tenty:class;
///
///附加指定的实体。
///
///实体的类型。
///实体。
无效附加(TEntity实体),其中TEntity:class;
///
///删除指定的实体。
///
///实体的类型。
///实体。
void Delete(tenty实体),其中tenty:class;
///
///删除与指定条件匹配的一个或多个实体
///
///实体的类型。
///标准。
void Delete(表达式标准),其中tenty:class;
///
///删除满足规范要求的实体
///
///实体的类型。
///标准。
无效删除(指定标准),其中tenty:类别;
///
///更新现有实体的更改。
///调用方以后必须显式调用存储库上的SaveChanges(),以将实体保存到数据库中
///
///实体的类型。
///实体。
无效更新(TEntity实体),其中TEntity:类;
///
///根据提供的条件查找实体。
///
///实体的类型。
///标准。
///
IEnumerable Find(i规格标准),其中tenty:class;
///
///根据提供的条件查找实体。
///
///实体的类型。
///标准。
///
IEnumerable Find(表达式条件),其中tenty:class;
///
///根据提供的条件查找一个实体。
///
///实体的类型。
///标准。
///
TEntity FindOne(指定标准),其中TEntity:class;
///
///根据提供的条件查找一个实体。
///
///实体的类型。
///标准。
///
TEntity FindOne(表达标准),其中TEntity:class;
///
///得到所有。
///
///实体的类型。
///
IEnumerable GetAll(),其中tenty:class;
///
///获取具有分页支持的实体集合
///
///实体的类型。
///订单被取消了。
///页面的索引。
///页面的大小。
///排序顺序。
///
IEnumerable Get(表达式orderBy,int pageIndex,int pageSize,SortOrder SortOrder=SortOrder.Ascending),其中tenty:class;
///
///获取基于具有分页支持的条件的实体集合
///
///实体的类型。
///标准。
public interface IRepository<TEntity> : IDisposable where TEntity : class
{
IQueryable<TEntity> GetAll { get; }
IEntity GetById(int id) { get; }
}
public class EntityRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
private IContext context;
private IObjectSet<TEntity> objectSet;
private IObjectSet<TEntity> ObjectSet
{
get
{
if (this.objectSet == null)
{
var entitySetProperty = this.Context.GetType().GetProperties().Single(p => p.PropertyType.IsGenericType && typeof(IQueryable<>).MakeGenericType(typeof(TEntity)).IsAssignableFrom(p.PropertyType));
this.objectSet = (IObjectSet<TEntity>)entitySetProperty.GetValue(this.Context, null);
}
return this.objectSet;
}
}
public IQueryable<TEntity> GetAll
{
get
{
return this.ObjectSet;
}
}
}
public interface IUserRepository : IRepository<User>
{
// some additional properties specific to User repository
...
}
public class UserRepository : EntityRepository<User>, IUserRepository
{
public UserRepository(IUnitOfWork uow)
: base(uow)
{
}
}