asp.net Core implementation of a simple warehousing method

Time:2020-10-17

I always have the idea of writing a framework, but I haven’t taken action. Recently, I’m quite free and can start work

Now, two parts have been completed. 1. A simple repository is implemented using EF 2.ioc. Here, the built-in IOC is replaced by aotofac. There are still some defects in this part

Storage part

Here is the interface is the implementation, currently using EF to implement the interface of the warehouse. Take a look at the code


 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
 }

The following is the implementation of part of the code, code occupies the layout, not paste all


 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;
  }
 }

Note that efrepositorybase inherits repository base, and repository base implements irepository. Here, repositorybase is the base class of all implementations. The getalllist virtual method directly calls the abstract method getall, which can reduce a lot of code in efrepository base

There is a pit here. Efrepositorybase cannot be directly registered into the IOC. Because the number of generic parameters of efrepository base and irepository is inconsistent, IOC cannot find an extra generic value. When using the repository, inherit efrepository base and pass dbcontext in


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

IOC part

asp.net Core Microsoft provides a simple IOC, but there are few interfaces. It is more convenient to replace it with the familiar IOC framework asp.net Core also has a very convenient method to replace IOC. In short, modify the return value of configureservices method to iserviceprovider. I used Autofac, and look at the code below


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();
 }
}

The module and imodule here are Autofac, and their functions have been implemented. However, as a framework, it is obviously inappropriate to expose Autofac directly. The next step is to implement a module loading mode of the framework itself

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.