123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 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>;
- /// <summary>
- /// 保存
- /// </summary>
- /// <param name="changedEntityTypes">变化的模型,用于清空模型对应的缓存</param>
- /// <returns></returns>
- Task<int> SaveAsync(params Type[] changedEntityTypes);
- }
- 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(params Type[] changedEntityTypes)
- {
- if (changedEntityTypes != null)
- {
- if (changedEntityTypes.Contains(typeof(XlInfo)))
- {
- IocContainer.Cache.Remove(CacheKeys.AllXlSat);
- }
- else if (changedEntityTypes.Contains(typeof(SatInfo)))
- {
- IocContainer.Cache.Remove(CacheKeys.AllSat);
- }
- }
- return await ctx.SaveChangesAsync();
- }
- public async ValueTask DisposeAsync()
- {
- await ctx.DisposeAsync();
- }
- }
- }
|