栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

asp.net core 实现一个简单的仓储的方法

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

asp.net core 实现一个简单的仓储的方法

一直有自己写个框架的想法,但是一直没有行动起来,最近比较闲,正好可以开工了.

现在已经完成了两部分.1.一个简单仓储,实现使用的是ef 2.IOC部分,这里是把内置的ioc替换成了aotofac,这部分感觉还是有一点缺陷的.下面说

仓储部分

这里主要是接口是实现,目前使用ef实现了仓储的接口.看一下代码

 public interface IRepository
  where TEntity : class
 {
  #region Select/Get/Query

  IQueryable GetAll();

  IQueryable GetAllIncluding(params expression>[] propertySelectors);

  List GetAllList();

  Task> GetAllListAsync();

  List GetAllList(expression> predicate);

  Task> GetAllListAsync(expression> predicate);

  T Query(Func, T> queryMethod);

  TEntity Get(TPrimaryKey id);

  Task GetAsync(TPrimaryKey id);

  TEntity Single(expression> predicate);

  Task SingleAsync(expression> predicate);

  TEntity FirstOrDefault(TPrimaryKey id);

  Task FirstOrDefaultAsync(TPrimaryKey id);

  TEntity FirstOrDefault(expression> predicate);

  Task FirstOrDefaultAsync(expression> predicate);

  TEntity Load(TPrimaryKey id);

  #endregion

  #region Insert

  TEntity Insert(TEntity entity);

  Task InsertAsync(TEntity entity);

  #endregion

  #region Update

  TEntity Update(TEntity entity);

  Task UpdateAsync(TEntity entity);

  TEntity Update(TPrimaryKey id, Action updateAction);

  Task UpdateAsync(TPrimaryKey id, Func updateAction);

  #endregion

  #region Delete

  void Delete(TEntity entity);

  Task DeleteAsync(TEntity entity);

  void Delete(TPrimaryKey id);

  Task DeleteAsync(TPrimaryKey id);

  void Delete(expression> predicate);

  Task DeleteAsync(expression> predicate);

  #endregion

  #region Aggregates

  int Count();

  Task CountAsync();

  int Count(expression> predicate);

  Task CountAsync(expression> predicate);

  long LongCount();

  Task LongCountAsync();

  long LongCount(expression> predicate);

  Task LongCountAsync(expression> predicate);

  #endregion
 }

下面是实现的部分代码,代码比较占版面,就不贴全了.

 public abstract class Repositorybase : IRepository
  where TEntity : class
 {
  public abstract IQueryable GetAll();

  public abstract IQueryable GetAllIncluding(params expression>[] propertySelectors);

  public virtual List GetAllList()
  {
   return GetAll().ToList();
  }

  public virtual async Task> GetAllListAsync()
  {
   return await Task.FromResult(GetAllList());
  }
 }
 public class EfRepositorybase : Repositorybase
  where TEntity : class
  where TDbContext : DbContext
 {
  public virtual TDbContext Context { private set; get; }

  public virtual DbSet Table => Context.Set();

  public EfRepositorybase(TDbContext context)
  {
   Context = context;
  }

  public override IQueryable GetAll()
  {
   return Table;
  }

  public override IQueryable GetAllIncluding(params expression>[] 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 : EfRepositorybase 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();
}


public static IServiceProvider AddLuna([NotNull]this IServiceCollection services)
 where TModule : IModule, new()
{
 var builder = new ContainerBuilder();
 builder.Populate(services);
 builder.RegisterModule();

 return new AutofacServiceProvider(builder.Build());
}

public class AutofacModule : Module
{
 protected override void Load(ContainerBuilder builder)
 {
  builder.RegisterType();

  builder.RegisterGeneric(typeof(TestRepository<,>)).As(typeof(IRepository<,>))
   .InstancePerLifetimeScope();
 }
}

这里的Module和IModule是autofac的,功能已经实现了,但是作为框架来说直接暴露了autofac的东西显然是不合适的,下一步要实现一个框架自身的模块加载方式.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/55403.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号