asp.net core怎么实现一个简单的仓储的方法
创始人
2024-11-27 21:00:59
0次

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

现在已经完成了两部分.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的东西显然是不合适的,下一步要实现一个框架自身的模块加载方式.

ASP.NET 是什么

ASP.NET 是开源,跨平台,高性能,轻量级的 Web 应用构建框架,常用于通过 HTML、CSS、JavaScript 以及服务器脚本来构建网页和网站。

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...