Exemple #1
0
 public DataAccessContextScope(DataMappingContext context)
 {
     this._context = context;
     if (ContextType == DataAccessContextType.Normal)
     {
         this._context.StartReadOnlyContext();
     }
     else if (ContextType == DataAccessContextType.ReadWrite)
     {
         this._context.StartReadWriteContext();
     }
     else
     {
         throw new DataAccessException("不支持的上下文参数");
     }
 }
        public DataAccessContext(DataMappingContext context, MethodInfo method, DataAccessOutSqlTextConfig outTypeConfig)
        {
            this._context       = context;
            this._method        = method;
            this._statementName = method.DeclaringType.FullName + "." + method.Name;
            var outSqlType = method.GetCustomAttribute <DataAccessOutSqlTextAttribute>();

            if (outSqlType != null)
            {
                this._outTypeConfig = outSqlType.Create();
            }
            else if (this._context.OutTypeConfig != null)
            {
                this._outTypeConfig = this._context.OutTypeConfig;
            }
            else if (outTypeConfig != null)
            {
                this._outTypeConfig = outTypeConfig;
            }
            var type       = method.GetCustomAttribute <DataAccessTypeAttribute>();
            var methodName = method.Name.ToLower();

            if (type != null)
            {
                this._dataAccessType = type.AccessType;
            }
            else if (methodName.StartsWith("get"))
            {
                this._dataAccessType = DataAccessType.SelectReadOnly;
            }
            else if (methodName.StartsWith("update"))
            {
                this._dataAccessType = DataAccessType.Update;
            }
            else if (methodName.StartsWith("delete"))
            {
                this._dataAccessType = DataAccessType.Delete;
            }
            else if (methodName.StartsWith("insert"))
            {
                this._dataAccessType = DataAccessType.Insert;
            }
            if (this._dataAccessType == DataAccessType.SelectReadOnly && context.Reader == null)
            {
                this._dataAccessType = DataAccessType.SelectReadWrite;
                _logger.Warn("数据源配置未指定只读库连接,该方法自动转为读写库连接,方法名:{0}", method.Name);
            }
            var interfaces = this._method.ReturnType.GetInterfaces();

            foreach (var item in interfaces)
            {
                if (item == typeof(IPagedList))
                {
                    this._isIPagedList = true;
                    break;
                }
            }
            if (!this._isIPagedList)
            {
                foreach (var item in interfaces)
                {
                    if (item == typeof(IEnumerable))
                    {
                        this._isSelectList = true;
                        break;
                    }
                }
            }
            if (this._isIPagedList && this._method.ReturnType.IsGenericType)
            {
                var genericArgs = this._method.ReturnType.GetGenericArguments();
                if (genericArgs.Length > 1)
                {
                    throw new DataAccessException("分页查询方法返回值必须为PagedList<T>接口的对象");
                }
                var returnType = genericArgs[0];
                this._pagedListType = typeof(PagedList <>).MakeGenericType(returnType);
            }

            if (!this._isIPagedList && !this._isSelectList && !IsNullableType(this._method.ReturnType) && this._method.ReturnType.IsValueType)
            {
                this._isNotCanNullReturnValue = true;
            }

            var parameters = this._method.GetParameters();

            if (parameters.Length > 1)
            {
                if (this._isIPagedList)
                {
                    throw new DataAccessException("分页查询方法参数必须且只能是一个IPageParameter接口的对象");
                }
                foreach (var item in parameters)
                {
                    _parameterList.Add(item.Name);
                }
                _isHashtableParameter = true;
            }
            else if (parameters.Length == 0 && this._isIPagedList)
            {
                throw new DataAccessException("分页查询方法参数必须且只能是一个IPageParameter接口的对象");
            }
            else if (parameters.Length == 1 && this._isIPagedList)
            {
                var  parameterInterfaces = parameters[0].ParameterType.GetInterfaces();
                bool flag = false;
                foreach (var item in parameterInterfaces)
                {
                    if (item == typeof(IPageParameter))
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    throw new DataAccessException("分页查询方法参数必须且只能是一个IPageParameter接口的对象");
                }
            }
            var interception = method.GetCustomAttribute <DataMappingInterceptionAttribute>();

            if (interception != null)
            {
                _intercept = Activator.CreateInstance(interception.Intercept) as IDataMappingInterception;
            }
        }
Exemple #3
0
 public ReadOnlyContextScope(DataMappingContext context) : base(context)
 {
 }
Exemple #4
0
 public ReadWriteContextScope(DataMappingContext context) : base(context)
 {
 }
Exemple #5
0
 public DataAccess(DataMappingContext context, DataAccessOutSqlTextConfig outTypeConfig)
 {
     this._context       = context;
     this._outTypeConfig = outTypeConfig;
 }