一直有自己寫個框架的想法,但是一直沒有行動起來,最近比較閑,正好可以開工了.
現在已經完成了兩部分.1.一個簡單倉儲,實現使用的是ef 2.IOC部分,這里是把內置的ioc替換成了aotofac,這部分感覺還是有一點缺陷的.下面說
倉儲部分
這里主要是接口是實現,目前使用ef實現了倉儲的接口.看一下代碼
public interface IRepository<TEntity, TPrimaryKey> where TEntity : class { #region Select/Get/Query IQueryable<TEntity> GetAll(); IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors); List<TEntity> GetAllList(); Task<List<TEntity>> GetAllListAsync(); List<TEntity> GetAllList(Expression<Func<TEntity, bool>> predicate); Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool>> predicate); T Query<T>(Func<IQueryable<TEntity>, T> queryMethod); TEntity Get(TPrimaryKey id); Task<TEntity> GetAsync(TPrimaryKey id); TEntity Single(Expression<Func<TEntity, bool>> predicate); Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> predicate); TEntity FirstOrDefault(TPrimaryKey id); Task<TEntity> FirstOrDefaultAsync(TPrimaryKey id); TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate); Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate); TEntity Load(TPrimaryKey id); #endregion #region Insert TEntity Insert(TEntity entity); Task<TEntity> InsertAsync(TEntity entity); #endregion #region Update TEntity Update(TEntity entity); Task<TEntity> UpdateAsync(TEntity entity); TEntity Update(TPrimaryKey id, Action<TEntity> updateAction); Task<TEntity> UpdateAsync(TPrimaryKey id, Func<TEntity, Task> updateAction); #endregion #region Delete void Delete(TEntity entity); Task DeleteAsync(TEntity entity); void Delete(TPrimaryKey id); Task DeleteAsync(TPrimaryKey id); void Delete(Expression<Func<TEntity, bool>> predicate); Task DeleteAsync(Expression<Func<TEntity, bool>> predicate); #endregion #region Aggregates int Count(); Task<int> CountAsync(); int Count(Expression<Func<TEntity, bool>> predicate); Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate); long LongCount(); Task<long> LongCountAsync(); long LongCount(Expression<Func<TEntity, bool>> predicate); Task<long> LongCountAsync(Expression<Func<TEntity, bool>> predicate); #endregion }下面是實現的部分代碼,代碼比較占版面,就不貼全了.
public abstract class RepositoryBase<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey> where TEntity : class { public abstract IQueryable<TEntity> GetAll(); public abstract IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors); public virtual List<TEntity> GetAllList() { return GetAll().ToList(); } public virtual async Task<List<TEntity>> GetAllListAsync() { return await Task.FromResult(GetAllList()); } }
新聞熱點
疑難解答
圖片精選