/// <summary> /// /// </summary> /// <param name="context"></param> /// <returns></returns> public override async Task Befor(IAOPContext context) { var container = context.ServiceProvider.GetService <TransactionContainer>(); if (container == null) { throw new Exception("请调用AddAOPTransaction注入事务"); } container.Depth++; if (!container.TransactionOpened) { container.TransactionOpened = true; container.DbContexts = _dbImplementTypes .Where(x => typeof(DbContext).IsAssignableFrom(x)) .Select(x => (DbContext)context.ServiceProvider.GetService(x)) .ToList(); container.DbAccessors = _dbImplementTypes .Where(x => typeof(IDbAccessor).IsAssignableFrom(x)) .Select(x => (IDbAccessor)context.ServiceProvider.GetService(x)) .ToList(); foreach (var aDbContext in container.DbContexts) { await aDbContext.Database.BeginTransactionAsync(_isolationLevel); } foreach (var aDbAccessor in container.DbAccessors) { await aDbAccessor.BeginTransactionAsync(_isolationLevel); } } }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <returns></returns> public override async Task After(IAOPContext context) { var container = context.ServiceProvider.GetService<TransactionContainer>(); container.Depth--; try { if (container.TransactionOpened && container.Depth == 0) { foreach (var aDbContext in container.DbContexts) { await aDbContext.Database.CurrentTransaction.CommitAsync(); } foreach (var aDbAccessor in container.DbAccessors) { aDbAccessor.CommitTransaction(); } container.TransactionOpened = false; } } catch { foreach (var aDbContext in container.DbContexts) { await aDbContext.Database.CurrentTransaction.RollbackAsync(); } foreach (var aDbAccessor in container.DbAccessors) { aDbAccessor.RollbackTransaction(); } throw; } }
public override async Task After(IAOPContext context) { var op = context.ServiceProvider.GetService <IOperator>(); op.WriteUserLog(_logType, $"删除{_dataName}:{_names}"); await Task.CompletedTask; }
public override async Task After(IAOPContext context) { var op = context.ServiceProvider.GetService <IOperator>(); var obj = context.Arguments[0]; op.WriteUserLog(_logType, $"修改{_dataName}:{obj.GetPropertyValue(_nameField)?.ToString()}"); await Task.CompletedTask; }
private void Init(IInvocation invocation) { _aopContext = new CastleAOPContext(invocation, _serviceProvider); _aops = invocation.MethodInvocationTarget.GetCustomAttributes(typeof(BaseAOP), true) .Concat(invocation.InvocationTarget.GetType().GetCustomAttributes(typeof(BaseAOP), true)) .Select(x => (BaseAOP)x) .ToList(); }
public override async Task Befor(IAOPContext context) { _container = context.ServiceProvider.GetService <TransactionContainer>(); if (!_container.TransactionOpened) { _container.TransactionOpened = true; await _container.BeginTransactionAsync(_isolationLevel); } }
public override async Task Befor(IAOPContext context) { List <string> ids = context.Arguments[0] as List <string>; var q = context.InvocationTarget.GetType().GetMethod("GetIQueryable").Invoke(context.InvocationTarget, new object[] { }) as IQueryable; var deleteList = q.Where("@0.Contains(Id)", ids).CastToList <object>(); _names = string.Join(",", deleteList.Select(x => x.GetPropertyValue(_nameField)?.ToString())); await Task.CompletedTask; }
public override async Task Befor(IAOPContext context) { Type entityType = context.Arguments[0].GetType(); var data = context.Arguments[0]; List <string> whereList = new List <string>(); var properties = _validateFields .Where(x => !data.GetPropertyValue(x.Key).IsNullOrEmpty()) .ToList(); properties.ForEach((aProperty, index) => { whereList.Add($" {aProperty.Key} = @{index} "); }); IQueryable q = null; if (_allData) { var repository = context.Proxy.GetPropertyValue("Service") as IDbAccessor; var method = repository.GetMethod("GetIQueryable"); q = method.MakeGenericMethod(entityType).Invoke(repository, new object[] { }) as IQueryable; } else { q = context.InvocationTarget.GetType().GetMethod("GetIQueryable").Invoke(context.InvocationTarget, new object[] { }) as IQueryable; } q = q.Where("Id != @0", data.GetPropertyValue("Id")); q = q.Where( string.Join(_matchOr ? " || " : " && ", whereList), properties.Select(x => data.GetPropertyValue(x.Key)).ToArray()); var list = q.CastToList <object>(); if (list.Count > 0) { var repeatList = properties .Where(x => list.Any(y => !y.GetPropertyValue(x.Key).IsNullOrEmpty())) .Select(x => x.Value) .ToList(); throw new BusException($"{string.Join(_matchOr ? "或" : "与", repeatList)}已存在!"); } await Task.CompletedTask; }
public async Task <T> OnExecuting <T>(IAOPContext context) { if (!(context.Arguments[0] is ICacheKey)) { throw new NotSupportedException("该方法不支持缓存."); } var cacheManagerConfigurations = context.ServiceProvider.GetServices <ICacheManagerConfiguration>(); //if (IsRedisNeverExpires) // this._cacheManager = CacheFactory.FromConfiguration<object>("", cacheManagerConfigurations.First(x => x.Name == "")); //else // this._cacheManager = CacheFactory.FromConfiguration<object>("", cacheManagerConfigurations.First(x => x.Name == "")); this._cacheKey = $"{PREFIX}{context.Method.DeclaringType.Namespace}_{context.Method.Name}_{(context.Arguments[0] as ICacheKey).GetCacheKey()}"; //var cacheData = await Task.FromResult(this._cacheManager.Get<T>(this._cacheKey)); await Task.CompletedTask; return(CacheManager.Default.Get <T>(_cacheKey)); //返回值 //return cacheData; }
public override async Task After(IAOPContext context) { _container = context.ServiceProvider.GetService <TransactionContainer>(); try { if (_container.TransactionOpened) { _container.CommitTransaction(); } } catch (Exception ex) { _container.RollbackTransaction(); throw new Exception("系统异常", ex); } if (_container.TransactionOpened) { _container.TransactionOpened = false; } await Task.CompletedTask; }
public virtual async Task After(IAOPContext context) { await Task.CompletedTask; }
public async Task OnExecuted <T>(IAOPContext context, T result) { //this._cacheManager.AddOrUpdate(_cacheKey, result, (v) => result); CacheManager.Default.Set_AbsoluteExpire <T>(_cacheKey, result, TimeSpan.FromMinutes(10)); //缓存十分钟 await Task.CompletedTask; }