コード例 #1
0
        public void Intercept(IInvocation invocation)
        {
            string methodName = invocation.Method.Name;

            Type typeofIDao = typeof(INpiDao <>);
            Type entityType = invocation.Method.DeclaringType.GetInterface(typeofIDao.FullName).GetGenericArguments()[0];


            // todo :对其单例化
            ISqlCommandGenerator  g = NpiServicesCollection.GetService <ISqlServerCommandGenerator>();
            SqlCommandDescription d = g.Generate(invocation.Method, invocation.Arguments);

            DebugLogger.Debug(d.ToString());

            DapperParameters dp = new DapperParameters();

            foreach (var p in d.Parameters)
            {
                dp[p.Key] = p.Value.Value;
            }

            object dbReturnedValue = null;
            object handledValue    = null;

            DebugLogger.Debug("准备执行 Sql");

            switch (d.Type)
            {
            case SqlCommandTypes.Insert:
            case SqlCommandTypes.Update:
            case SqlCommandTypes.Delete:
            {
                var executor = ServicesCollection.GetService <ISqlCommandExecutor>();
                dbReturnedValue = executor.Execute(d.SqlCommand, dp, dbConnectionContext);
            }
            break;

            case SqlCommandTypes.Select:
            {
                var querier = ServicesCollection.GetService <ISqlCommandQuerier>();
                dbReturnedValue = querier.Select(entityType, d.SqlCommand, dp, dbConnectionContext);
            }
            break;

            default:
                break;
            }
            DebugLogger.Debug("Sql 执行完毕,开始处理结果集");
            foreach (var handler in dbReturnValueHandlers)
            {
                if (!handler.CanHandle(invocation.Method, entityType))
                {
                    continue;
                }
                handledValue = handler.Handle(invocation.Method, entityType, dbReturnedValue);
            }
            invocation.ReturnValue = handledValue;
            DebugLogger.Debug("结果集处理完毕");
        }
コード例 #2
0
        public override void Intercept(InterfaceInvocationInfo info)
        {
            this.AssertProviderIsValid();
            Type typeOfIDao = typeof(INpiDao <>);

            ISqlCommandGenerator  g = NpiServicesCollection.GetService <ISqlServerCommandGenerator>();
            SqlCommandDescription d = g.Generate(info.Method, info.Arguments);

            this.EventBus.Publish(new SqlCommandDescriptionGeneratedEvent(this, d));

            if (info.Method.ReturnType == typeof(void))
            {
                d.Mode = SqlCommandExecuteModes.Execute;
            }

            switch (d.Mode)
            {
            case SqlCommandExecuteModes.Execute:
                int i = this.Executor.Execute(this.Provider.Provide(), d);
                foreach (var handler in this.ExecuteResultHandlers)
                {
                    if (!handler.CanHandle(info.Method))
                    {
                        continue;
                    }
                    info.ReturnValue = handler.Handle(info.Method, i);
                }
                break;

            case SqlCommandExecuteModes.Query:
            {
                Type returnType = info.Method.ReturnType;
                Type itemType;
                if (TryGetIEnumerableItemType(returnType, out itemType))
                {
                    returnType = itemType;
                }

                IEnumerable <object> list = this.Executor.Select(this.Provider.Provide(), d, returnType);
                foreach (var handler in this.SelectResultHandlers)
                {
                    if (!handler.CanHandle(info.Method, returnType))
                    {
                        continue;
                    }
                    info.ReturnValue = handler.Handle(info.Method, returnType, list);
                }
            }
            break;

            default:
                break;
            }
        }