private object CreateDataAccessObject(Type type)
        {
            try
            {
                string      xmlTemplete = "<sqlMap embedded=\"{0},{1}\" xmlns=\"http://ibatis.apache.org/dataMapper\" />";
                XmlDocument config      = new XmlDocument();
                config.LoadXml(string.Format(xmlTemplete, type.FullName + ".sql.xml", type.Assembly.FullName));
                if (this._context.Reader != null)
                {
                    _mapLoader.ReadOnlyBuilder.ConfigureSqlMap(config.DocumentElement);
                }
                _mapLoader.WriteBuilder.ConfigureSqlMap(config.DocumentElement);
            }
            catch (Exception ex)
            {
                throw new DataAccessException(string.Format("创建数据访问对象失败,对象类型:{0}", type.FullName), ex);
            }
            DefaultProxyBuilder        builder       = new DefaultProxyBuilder();
            ProxyGenerator             generator     = new ProxyGenerator(builder);
            DataAccessOutSqlTextConfig outTypeConfig = null;
            var outSqlType = type.GetCustomAttribute <DataAccessOutSqlTextAttribute>();

            if (outSqlType != null)
            {
                outTypeConfig = outSqlType.Create();
            }
            return(generator.CreateInterfaceProxyWithoutTarget(type, new DataAccess(this._context, outTypeConfig)));
        }
        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;
            }
        }
 public DataAccessFactory(string dataSourceName, DataAccessOutSqlTextConfig outTypeConfig)
     : this(dataSourceName)
 {
     this._context.OutTypeConfig = outTypeConfig;
 }
示例#4
0
 public DataAccess(DataMappingContext context, DataAccessOutSqlTextConfig outTypeConfig)
 {
     this._context       = context;
     this._outTypeConfig = outTypeConfig;
 }