麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

asp.net core 實現一個簡單的倉儲的方法

2024-07-10 13:32:45
字體:
來源:轉載
供稿:網友

一直有自己寫個框架的想法,但是一直沒有行動起來,最近比較閑,正好可以開工了.

現在已經完成了兩部分.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());  } }
 public class EfRepositoryBase<TDbContext, TEntity, TPrimaryKey> : RepositoryBase<TEntity, TPrimaryKey>  where TEntity : class  where TDbContext : DbContext {  public virtual TDbContext Context { private set; get; }  public virtual DbSet<TEntity> Table => Context.Set<TEntity>();  public EfRepositoryBase(TDbContext context)  {   Context = context;  }  public override IQueryable<TEntity> GetAll()  {   return Table;  }  public override IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors)  {   if (propertySelectors == null)   {    return GetAll();   }   var linq = GetAll();   foreach (var item in propertySelectors)   {    linq = linq.Include(item);   }   return linq;  } }

注意看EfRepositoryBase繼承了RepositoryBase,而RepositoryBase實現了IRepository.這里的RepositoryBase是所有實現的基類.GetAllList虛方法直接調用了抽象方法GetAll,這樣在EfRepositoryBase中就可以減少很多代碼了.

這里有個坑 EfRepositoryBase 是不能直接注冊到IOC中的,因為EfRepositoryBase和IRepository的泛型參數個數不一致,ioc不能找到多出的一個泛型的值.使用倉儲的時候繼承EfRepositoryBase把dbcontext傳進去就好了

public class TestRepository<TEntity, TPrimaryKey> : EfRepositoryBase<TestContext, TEntity, TPrimaryKey> where TEntity : class{ public TestRepository(TestContext context)  : base(context) { }}

IOC部分

asp.net core 微軟提供了一個簡單的IOC,但是接口比較少,替換成我們熟悉的ioc框架就方便多了. asp.net core 也有很方便的替換ioc的方法.簡單說就是修改ConfigureServices方法的返回值為IServiceProvider.我使用了autofac,下面看代碼.

public IServiceProvider ConfigureServices(IServiceCollection services){ services.AddMvc(); return services.AddLuna<AutofacModule>();}public static IServiceProvider AddLuna<TModule>([NotNull]this IServiceCollection services) where TModule : IModule, new(){ var builder = new ContainerBuilder(); builder.Populate(services); builder.RegisterModule<TModule>(); return new AutofacServiceProvider(builder.Build());}public class AutofacModule : Module{ protected override void Load(ContainerBuilder builder) {  builder.RegisterType<TestContext>();  builder.RegisterGeneric(typeof(TestRepository<,>)).As(typeof(IRepository<,>))   .InstancePerLifetimeScope(); }}

這里的Module和IModule是autofac的,功能已經實現了,但是作為框架來說直接暴露了autofac的東西顯然是不合適的,下一步要實現一個框架自身的模塊加載方式.

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩黄色免费在线观看 | 色播视频在线播放 | 黄色特级毛片 | 中文字幕激情视频 | 久啪视频 | 国产成人在线视频 | 国产精品一区免费在线观看 | 视频一区 日韩 | 一级毛片电影网 | 久久精品欧美视频 | 亚洲国产成人久久成人52 | 亚洲va久久久噜噜噜久久男同 | 91短视频在线播放 | gogo全球大胆高清人露出91 | 羞羞草视频 | 国产精品jk白丝蜜臀av软件 | 日本成人在线免费 | 亚洲一区二区 | omofun 动漫在线观看 | 免费毛片随便看 | av成人免费在线观看 | 国产精品成人一区 | 久久蜜桃香蕉精品一区二区三区 | 国产精品一区二av18款 | 澳门一级淫片免费视频 | 爽爽淫人综合网网站 | 成人做爰s片免费看网站 | 91精品成人福利在线播放 | 日日草夜夜操 | 97综合| 久久亚洲春色中文字幕久久 | 91精品国产乱码久久桃 | 国产一级不卡毛片 | 亚洲人成在线播放网站 | 2019中文字幕在线播放 | 亚洲欧美日韩一区二区三区在线观看 | 97超级碰碰人国产在线观看 | 欧美日韩视频第一页 | 国产午夜精品一区二区三区视频 | 亚洲福利在线观看视频 | 中文字幕 欧美 日韩 |