asp. net core Implementation of a Simple Warehouse Method

  • 2021-10-16 01:23:56
  • OfStack

I have the idea of writing a framework by myself, but I have no action. I am idle recently, so I can start work.

Now that we have completed two parts. 1.1 simple repository, the implementation uses ef 2. IOC. Here, the built-in ioc is replaced by aotofac. This part still feels like there is one flaw. Let's say below

Warehousing section

Here is the main interface is to achieve, the current use of ef to achieve the warehouse interface. See the code under 1


 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 comparison occupies the layout, it will not be posted.


 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 RepositoryBase, and RepositoryBase implements IRepository. RepositoryBase here is the base class for all implementations. The GetAllList virtual method directly calls the abstract method GetAll, which reduces a lot of code in EfRepositoryBase.

There is a pit EfRepositoryBase is not directly registered to IOC, because EfRepositoryBase and IRepository generic parameter number is not 1, ioc can not find an extra generic value. When using the warehouse, it is good to inherit EfRepositoryBase and transfer dbcontext


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 the interface is less, replace with the familiar ioc framework is much more convenient. asp. net core also has a very convenient replacement ioc method. Simply say is to modify the return value of ConfigureServices method for IServiceProvider. I used autofac, see 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();
 }
}

Module and IModule here belong to autofac, and their functions have been realized. However, as a framework, it is obviously inappropriate to directly expose autofac. The next step is to realize the module loading mode of a framework itself.


Related articles: