UnitOfWork.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using DW5S.Entity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace DW5S.Repostory
  8. {
  9. public interface IUnitOfWork : IAsyncDisposable
  10. {
  11. IRepository<TEntity> Of<TEntity>() where TEntity : BaseEntity;
  12. Task<int> SaveAsync();
  13. }
  14. public class UnitOfWork : IUnitOfWork
  15. {
  16. public readonly OracleContext ctx;
  17. public IRepository<TEntity> Of<TEntity>() where TEntity : BaseEntity
  18. {
  19. //UnitOfWork中的DbContext必须和Repository中的DbContext是同一个对象
  20. var reps = IocContainer.GetService<IRepository<TEntity>>(ctx);
  21. return reps;
  22. }
  23. public UnitOfWork(OracleContext ctx)
  24. {
  25. this.ctx = ctx;
  26. }
  27. public async Task<int> SaveAsync()
  28. {
  29. return await ctx.SaveChangesAsync();
  30. }
  31. public async ValueTask DisposeAsync()
  32. {
  33. await ctx.DisposeAsync();
  34. }
  35. }
  36. }