12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using DW5S.Entity;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace DW5S.Repostory
- {
- public interface IUnitOfWork : IAsyncDisposable
- {
- IRepository<TEntity, int> Of<TEntity>() where TEntity : BaseEntity<int>;
- IRepository<TEntity, long> OfLong<TEntity>() where TEntity : BaseEntity<long>;
- IRepository<TEntity,IDType> OfType<TEntity, IDType>() where TEntity : BaseEntity<IDType>;
- Task<int> SaveAsync();
- }
- public class UnitOfWork : IUnitOfWork
- {
- public readonly OracleContext ctx;
- public IRepository<TEntity, IDType> OfType<TEntity, IDType>() where TEntity : BaseEntity<IDType>
- {
- //UnitOfWork中的DbContext必须和Repository中的DbContext是同一个对象
- var reps = IocContainer.GetService<IRepository<TEntity, IDType>>(ctx);
- return reps;
- }
- public IRepository<TEntity, int> Of<TEntity>() where TEntity : BaseEntity<int>
- {
- //UnitOfWork中的DbContext必须和Repository中的DbContext是同一个对象
- var reps = IocContainer.GetService<IRepository<TEntity,int>>(ctx);
- return reps;
- }
- public IRepository<TEntity, long> OfLong<TEntity>() where TEntity : BaseEntity<long>
- {
- //UnitOfWork中的DbContext必须和Repository中的DbContext是同一个对象
- var reps = IocContainer.GetService<IRepository<TEntity, long>>(ctx);
- return reps;
- }
- public UnitOfWork(OracleContext ctx)
- {
- this.ctx = ctx;
- }
- public async Task<int> SaveAsync()
- {
- return await ctx.SaveChangesAsync();
- }
- public async ValueTask DisposeAsync()
- {
- await ctx.DisposeAsync();
- }
- }
- }
|