UnitOfWork.cs 940 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. private readonly OracleContext ctx;
  17. public IRepository<TEntity> Of<TEntity>() where TEntity : BaseEntity
  18. {
  19. var reps = IocContainer.GetService<IRepository<TEntity>>();
  20. return reps;
  21. }
  22. public UnitOfWork(OracleContext ctx)
  23. {
  24. this.ctx = ctx;
  25. }
  26. public async Task<int> SaveAsync()
  27. {
  28. return await ctx.SaveChangesAsync();
  29. }
  30. public async ValueTask DisposeAsync()
  31. {
  32. await ctx.DisposeAsync();
  33. }
  34. }
  35. }