Ejemplo n.º 1
0
        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var parameters = context.GetParameters();

            if (parameters.Any())
            {
                var id = default(int);
                if (int.TryParse(parameters[0].Value.ToString(), out id))
                {
                    var result = default(Model);
                    if (cache.TryGetValue(id, out result))
                    {
                        context.ReturnValue = result;
                        return;
                    }
                }
            }
            await next(context);

            var value = context.ReturnValue as Model;

            if (value != null)
            {
                cache[value.Id] = value;
            }
        }
        public override Task Invoke(AspectContext context, AspectDelegate next)
        {
            if (string.IsNullOrEmpty(Sql))
            {
                throw new ArgumentNullException(nameof(Sql));
            }

            var paramters    = context.GetParameters();
            var sqlParamters = ToParamterDict(paramters);

            using (var conn = ConnectionFactory.CreateConnection(ConnectionName))
            {
                //get IAgileRepository<TEntity> 's TEntity for Query T
                var result = (int)QueryHelper.RunExecute(conn, Sql, sqlParamters);
                if (context.ServiceMethod.ReturnType == typeof(bool))
                {
                    context.ReturnValue = result >= 0;
                }
                else
                {
                    context.ReturnValue = result;
                }
            }

            return(context.Break());
        }
Ejemplo n.º 3
0
        public override async Task Invoke(AspectContext context, AspectDelegate next)
        {
            var parameters = context.GetParameters();

            if (parameters.Any())
            {
                if (int.TryParse(parameters[0].Value.ToString(), out int id))
                {
                    if (_cache.TryGetValue(id, out var result))
                    {
                        var returnType = context.ServiceMethod.ReturnType;
                        if (returnType == typeof(Task <Model>))
                        {
                            context.ReturnValue = Task.FromResult(result);
                        }
                        else if (returnType == typeof(Model))
                        {
                            context.ReturnValue = result;
                        }
                        return;
                    }
                }
            }
            await next(context);

            if (context.ReturnValue is Model value)
            {
                _cache[value.Id] = value;
            }
            else if (context.ReturnValue is Task <Model> task && task.IsCompleted)
            {
                var obj = await task;
                _cache[obj.Id] = obj;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 生成缓存键
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="paramCount">参数数量</param>
        /// <returns></returns>
        private string GenerateCacheKey(AspectContext context, int paramCount)
        {
            var typeName        = context.ServiceMethod.DeclaringType?.Name;
            var methodName      = context.ServiceMethod.Name;
            var methodArguments =
                this.FormatArgumentsToPartOfCacheKey(context.GetParameters(), paramCount);

            return(this.GenerateCacheKey(typeName, methodName, methodArguments));
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 执行前
 /// </summary>
 private void ExecuteBefore(ILog log, AspectContext context, string methodName)
 {
     log.Caption($"{context.ServiceMethod.Name}方法执行前")
     .Method(methodName);
     foreach (var parameter in context.GetParameters())
     {
         parameter.AppendTo(log);
     }
     WriteLog(log);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 执行前
        /// </summary>
        private void ExecuteBefore(ILog log, AspectContext context, string methodName)
        {
            StringBuilder content = new StringBuilder();

            content.AppendFormat($"{methodName}方法执行前");
            foreach (var parameter in context.GetParameters())
            {
                parameter.AppendTo(content);
            }
            WriteLog(log, content.ToString());
        }
        public override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var sql = GenericSqlByMethodName(context, next);

            var paramters   = context.GetParameters();
            var queryParams = ToParamterDict(paramters);

            using (var conn = ConnectionFactory.CreateConnection(ConnectionName))
            {
                var result = QueryHelper.RunGenericCount(context.ServiceMethod.ReturnType, conn, sql, queryParams);
                context.ReturnValue = result;
            }

            return(context.Break());
        }
Ejemplo n.º 8
0
        public override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var sql = GenericSqlByMethodName(context, next);

            var paramters   = context.GetParameters();
            var queryParams = ToParamterDict(paramters);

            using (var conn = ConnectionFactory.CreateConnection(ConnectionName))
            {
                var gt     = AgileRepositoryGenericTypeArguments(context);
                var result = QueryHelper.RunGenericQuery(gt.First(), conn, sql, queryParams);
                context.ReturnValue = result;
            }

            return(context.Break());
        }
Ejemplo n.º 9
0
        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var selector = (IParameterInterceptorSelector)context.ServiceProvider.GetService(typeof(IParameterInterceptorSelector));

            if (selector == null)
            {
                throw new InvalidOperationException("Cannot resolve ParameterInterceptorSelector.");
            }
            var parameters = context.GetParameters();
            var count      = parameters.Count;

            if (count > 0)
            {
                var parameterAspectInvoker = new ParameterAspectInvoker();
                for (var i = 0; i < count; i++)
                {
                    var parameter    = parameters[i];
                    var interceptors = selector.Select(parameter.ParameterInfo);
                    if (interceptors.Length > 0)
                    {
                        var parameterAspectContext = new ParameterAspectContext(context, parameter);
                        foreach (var interceptor in interceptors)
                        {
                            parameterAspectInvoker.AddDelegate(interceptor.Invoke);
                        }
                        await parameterAspectInvoker.Invoke(parameterAspectContext);

                        parameterAspectInvoker.Reset();
                    }
                }
            }
            await next(context);

            var returnParameter    = context.GetReturnParameter();
            var returnInterceptors = selector.Select(returnParameter.ParameterInfo);

            if (returnInterceptors.Length > 0)
            {
                var returnParameterAspectContext = new ParameterAspectContext(context, returnParameter);
                var returnParameterAspectInvoker = new ParameterAspectInvoker();
                foreach (var interceptor in returnInterceptors)
                {
                    returnParameterAspectInvoker.AddDelegate(interceptor.Invoke);
                }
                await returnParameterAspectInvoker.Invoke(returnParameterAspectContext);
            }
        }
Ejemplo n.º 10
0
        public override async Task Invoke(AspectContext context, AspectDelegate next)
        {
            var parameters = context.GetParameters();
            var param      = parameters.FirstOrDefault(x => x.ParameterInfo.GetCustomAttribute <FromCapAttribute>() != null);

            if (param == null)
            {
                await next(context);

                return;
            }

            if (param.Value is CapHeader capHeader)
            {
                InitTraceIdContext(capHeader);
                await next(context);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 执行
        /// </summary>
        public override async Task Invoke(AspectContext context, AspectDelegate next)
        {
            var methodName = GetMethodName(context);
            var log        = Log.GetLog(methodName);

            try {
                await next(context);
            }
            catch (Exception ex) {
                log.Class(context.ServiceMethod.DeclaringType.FullName).Method(methodName).Exception(ex);
                foreach (var parameter in context.GetParameters())
                {
                    parameter.AppendTo(log);
                }
                log.Error();
                throw;
            }
        }
Ejemplo n.º 12
0
        public override Task Invoke(AspectContext context, AspectDelegate next)
        {
            if (string.IsNullOrEmpty(Sql))
            {
                throw new ArgumentNullException(nameof(Sql));
            }

            var paramters   = context.GetParameters();
            var queryParams = ToParamterDict(paramters);

            using (var conn = ConnectionFactory.CreateConnection(ConnectionName))
            {
                var result = QueryHelper.RunGenericCount(context.ServiceMethod.ReturnType, conn, Sql, queryParams);
                context.ReturnValue = result;
            }

            return(context.Break());
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 执行
        /// </summary>
        public override async Task Invoke(AspectContext context, AspectDelegate next)
        {
            var methodName = GetMethodName(context);
            var manager    = (ILogManager)context.ServiceProvider.GetService(typeof(ILogManager));
            var log        = manager.GetLog(methodName);

            try {
                await next(context);
            }
            catch (Exception ex) {
                log.Method(methodName).Exception(ex);
                foreach (var parameter in context.GetParameters())
                {
                    parameter.AppendTo(log);
                }
                log.Error();
                throw;
            }
        }
Ejemplo n.º 14
0
        public override Task Invoke(AspectContext context, AspectDelegate next)
        {
            if (string.IsNullOrEmpty(Sql))
            {
                throw new ArgumentNullException(nameof(Sql));
            }

            var paramters   = context.GetParameters();
            var queryParams = ToParamterDict(paramters);

            using (var conn = ConnectionFactory.CreateConnection(ConnectionName))
            {
                var gt = AgileRepositoryGenericTypeArguments(context);
                //get IAgileRepository<TEntity> 's TEntity for Query T
                var result = QueryHelper.RunGenericQuery(gt[0], conn, Sql, queryParams);
                context.ReturnValue = result;
            }

            return(context.Break());
        }
Ejemplo n.º 15
0
        public override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var paramters   = context.GetParameters();
            var queryParams = ToParamterDict(paramters);

            using (var conn = ConnectionFactory.CreateConnection(ConnectionName))
            {
                var sql    = GenericDeleteSql(context);
                var result = (int)QueryHelper.RunExecute(conn, sql, queryParams);
                if (context.ServiceMethod.ReturnType == typeof(bool))
                {
                    context.ReturnValue = result > 0;
                }
                else
                {
                    context.ReturnValue = result;
                }
            }

            return(context.Break());
        }
Ejemplo n.º 16
0
        public override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var paramters = context.GetParameters();

            using (var conn = ConnectionFactory.CreateConnection(ConnectionName))
            {
                var sql           = GenericInsertSql(context);
                var insertParam   = paramters.First();
                var isIEnumerable = (insertParam as IEnumerable) != null;
                var result        = 0;
                if (!isIEnumerable)
                {
                    result = (int)QueryHelper.RunExecute(conn, sql, paramters.First().Value);
                }
                else
                {
                    //for insert entities
                    using (var tran = conn.BeginTransaction())
                    {
                        foreach (var p in (IEnumerable)insertParam)
                        {
                            result += (int)QueryHelper.RunExecute(conn, tran, sql, p);
                        }

                        tran.Commit();
                    }
                }
                if (context.ServiceMethod.ReturnType == typeof(bool))
                {
                    context.ReturnValue = result > 0;
                }
                else
                {
                    context.ReturnValue = result;
                }
            }

            return(context.Break());
        }
 public DataValidationContext(AspectContext aspectContext)
 {
     AspectContext = aspectContext;
     DataMetaDatas = aspectContext.GetParameters().Select(param => new DataMetaData(param)).ToArray();
 }