public SqlMapper(IObjectFactory objectFactory, IBatisNet.Common.Utilities.Objects.Members.AccessorFactory accessorFactory) { this._objectFactory = objectFactory; this._accessorFactory = accessorFactory; this._dataExchangeFactory = new IBatisNet.DataMapper.DataExchange.DataExchangeFactory(this._typeHandlerFactory, this._objectFactory, accessorFactory); this._id = HashCodeProvider.GetIdentityHashCode(this).ToString(); this._sessionStore = SessionStoreFactory.GetSessionStore(this._id); }
public DataExchangeFactory(IBatisNet.DataMapper.TypeHandlers.TypeHandlerFactory typeHandlerFactory, IObjectFactory objectFactory, IBatisNet.Common.Utilities.Objects.Members.AccessorFactory accessorFactory) { this._objectFactory = objectFactory; this._typeHandlerFactory = typeHandlerFactory; this._accessorFactory = accessorFactory; this._primitiveDataExchange = new PrimitiveDataExchange(this); this._complexDataExchange = new ComplexDataExchange(this); this._listDataExchange = new ListDataExchange(this); this._dictionaryDataExchange = new DictionaryDataExchange(this); }
/// <summary> /// Initializes a new instance of the <see cref="DataExchangeFactory"/> class. /// </summary> /// <param name="typeHandlerFactory">The type handler factory.</param> /// <param name="objectFactory">The object factory.</param> /// <param name="accessorFactory">The accessor factory.</param> public DataExchangeFactory(TypeHandlerFactory typeHandlerFactory, IObjectFactory objectFactory, AccessorFactory accessorFactory) { _objectFactory = objectFactory; _typeHandlerFactory = typeHandlerFactory; _accessorFactory = accessorFactory; _primitiveDataExchange = new PrimitiveDataExchange(this); _complexDataExchange = new ComplexDataExchange(this); _listDataExchange = new ListDataExchange(this); _dictionaryDataExchange = new DictionaryDataExchange(this); }
/// <summary> /// Sets the member. /// </summary> /// <param name="obj">The obj.</param> /// <param name="memberName">Name of the member.</param> /// <param name="memberValue">The member value.</param> /// <param name="accessorFactory">The accessor factory.</param> public static void SetMember(object obj, string memberName, object memberValue, AccessorFactory accessorFactory) { try { if (memberName.IndexOf("[") > -1) { SetArrayMember(obj, memberName, memberValue, accessorFactory); } else { if (obj is IDictionary) { ((IDictionary) obj)[memberName] = memberValue; } else { Type targetType = obj.GetType(); ISetAccessorFactory setAccessorFactory = accessorFactory.SetAccessorFactory; ISetAccessor setAccessor = setAccessorFactory.CreateSetAccessor(targetType, memberName); if (setAccessor == null) { throw new ProbeException("No Set method for member " + memberName + " on instance of " + obj.GetType().Name); } try { setAccessorFactory.CreateSetAccessor(targetType, memberName).Set(obj, memberValue); } catch (Exception ex) { throw new ProbeException(ex); } } } } catch (ProbeException pe) { throw pe; } catch (Exception e) { throw new ProbeException("Could not Get property '" + memberName + "' for " + obj.GetType().Name + ". Cause: " + e.Message, e); } }
/// <summary> /// Return the specified member on an object. /// </summary> /// <param name="obj">The Object on which to invoke the specified property.</param> /// <param name="memberName">Name of the member.</param> /// <param name="accessorFactory">The accessor factory.</param> /// <returns>An Object representing the return value of the invoked property.</returns> public static object GetMemberValue(object obj, string memberName, AccessorFactory accessorFactory) { if (memberName.IndexOf('.') > -1) { StringTokenizer parser = new StringTokenizer(memberName, "."); IEnumerator enumerator = parser.GetEnumerator(); object value = obj; string token = null; while (enumerator.MoveNext()) { token = (string)enumerator.Current; value = GetMember(value, token, accessorFactory); if (value == null) { break; } } return value; } else { return GetMember(obj, memberName, accessorFactory); } }
/// <summary> /// Initializes a new instance of the <see cref="IsLessThanTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsLessThanTagHandler(AccessorFactory accessorFactory) : base(accessorFactory) { }
/// <summary> /// Initializes a new instance of the <see cref="T:Dynamic"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public Dynamic(AccessorFactory accessorFactory) { this.Handler = new DynamicTagHandler(accessorFactory); }
/// <summary> /// Initializes a new instance of the <see cref="IsGreaterEqualTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsGreaterEqualTagHandler(AccessorFactory accessorFactory) : base(accessorFactory) { }
/// <summary> /// Gets the member's value on the specified object. /// </summary> /// <param name="obj">The obj.</param> /// <param name="memberName">Name of the member.</param> /// <param name="accessorFactory">The accessor factory.</param> /// <returns>The member's value</returns> public static object GetMember(object obj, string memberName, AccessorFactory accessorFactory) { try { object value = null; if (memberName.IndexOf("[") > -1) { value = GetArrayMember(obj, memberName, accessorFactory); } else { if (obj is IDictionary) { value = ((IDictionary) obj)[memberName]; } else { Type targetType = obj.GetType(); IGetAccessorFactory getAccessorFactory = accessorFactory.GetAccessorFactory; IGetAccessor getAccessor = getAccessorFactory.CreateGetAccessor(targetType, memberName); if (getAccessor == null) { throw new ProbeException("No Get method for member " + memberName + " on instance of " + obj.GetType().Name); } try { value = getAccessor.Get(obj); } catch (Exception ae) { throw new ProbeException(ae); } } } return value; } catch (ProbeException pe) { throw pe; } catch(Exception e) { throw new ProbeException("Could not Set property '" + memberName + "' for " + obj.GetType().Name + ". Cause: " + e.Message, e); } }
/// <summary> /// /// </summary> public IsNull(AccessorFactory accessorFactory) { this.Handler = new IsNullTagHandler(accessorFactory); }
/// <summary> /// Initializes a new instance of the <see cref="IsLessThan"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsLessThan(AccessorFactory accessorFactory) { this.Handler = new IsLessThanTagHandler(accessorFactory); }
/// <summary> /// Initializes a new instance of the <see cref="IsNotEmpty"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsNotEmpty(AccessorFactory accessorFactory) { this.Handler = new IsNotEmptyTagHandler(accessorFactory); }
/// <summary> /// Initializes a new instance of the <see cref="BaseTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public BaseTagHandler(AccessorFactory accessorFactory) { _accessorFactory = accessorFactory; }
/// <summary> /// Initializes a new instance of the <see cref="Iterate"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public Iterate(AccessorFactory accessorFactory) { this.Handler = new IterateTagHandler(accessorFactory); }
/// <summary> /// Intialize the internal ISqlMapper instance. /// </summary> private void Initialize() { Reset(); #region Load Global Properties if (_configScope.IsCallFromDao == false) { _configScope.NodeContext = _configScope.SqlMapConfigDocument.SelectSingleNode( ApplyDataMapperNamespacePrefix(XML_DATAMAPPER_CONFIG_ROOT), _configScope.XmlNamespaceManager); ParseGlobalProperties(); } #endregion #region Load settings _configScope.ErrorContext.Activity = "loading global settings"; XmlNodeList settings = _configScope.SqlMapConfigDocument.SelectNodes( ApplyDataMapperNamespacePrefix(XML_CONFIG_SETTINGS), _configScope.XmlNamespaceManager); if (settings!=null) { foreach (XmlNode setting in settings) { if (setting.Attributes[ATR_USE_STATEMENT_NAMESPACES] != null ) { string value = NodeUtils.ParsePropertyTokens(setting.Attributes[ATR_USE_STATEMENT_NAMESPACES].Value, _configScope.Properties); _configScope.UseStatementNamespaces = Convert.ToBoolean( value ); } if (setting.Attributes[ATR_CACHE_MODELS_ENABLED] != null ) { string value = NodeUtils.ParsePropertyTokens(setting.Attributes[ATR_CACHE_MODELS_ENABLED].Value, _configScope.Properties); _configScope.IsCacheModelsEnabled = Convert.ToBoolean( value ); } if (setting.Attributes[ATR_USE_REFLECTION_OPTIMIZER] != null ) { string value = NodeUtils.ParsePropertyTokens(setting.Attributes[ATR_USE_REFLECTION_OPTIMIZER].Value, _configScope.Properties); _configScope.UseReflectionOptimizer = Convert.ToBoolean( value ); } if (setting.Attributes[ATR_VALIDATE_SQLMAP] != null ) { string value = NodeUtils.ParsePropertyTokens(setting.Attributes[ATR_VALIDATE_SQLMAP].Value, _configScope.Properties); _configScope.ValidateSqlMap = Convert.ToBoolean( value ); } } } #endregion if (_objectFactory == null) { _objectFactory = new ObjectFactory(_configScope.UseReflectionOptimizer); } if (_setAccessorFactory == null) { _setAccessorFactory = new SetAccessorFactory(_configScope.UseReflectionOptimizer); } if (_getAccessorFactory == null) { _getAccessorFactory = new GetAccessorFactory(_configScope.UseReflectionOptimizer); } if (_sqlMapper == null) { AccessorFactory accessorFactory = new AccessorFactory(_setAccessorFactory, _getAccessorFactory); _configScope.SqlMapper = new SqlMapper(_objectFactory, accessorFactory); } else { _configScope.SqlMapper = _sqlMapper; } ParameterMap emptyParameterMap = new ParameterMap(_configScope.DataExchangeFactory); emptyParameterMap.Id = ConfigurationScope.EMPTY_PARAMETER_MAP; _configScope.SqlMapper.AddParameterMap( emptyParameterMap ); _configScope.SqlMapper.IsCacheModelsEnabled = _configScope.IsCacheModelsEnabled; #region Cache Alias TypeAlias cacheAlias = new TypeAlias(typeof(MemoryCacheControler)); cacheAlias.Name = "MEMORY"; _configScope.SqlMapper.TypeHandlerFactory.AddTypeAlias(cacheAlias.Name, cacheAlias); cacheAlias = new TypeAlias(typeof(LruCacheController)); cacheAlias.Name = "LRU"; _configScope.SqlMapper.TypeHandlerFactory.AddTypeAlias(cacheAlias.Name, cacheAlias); cacheAlias = new TypeAlias(typeof(FifoCacheController)); cacheAlias.Name = "FIFO"; _configScope.SqlMapper.TypeHandlerFactory.AddTypeAlias(cacheAlias.Name, cacheAlias); cacheAlias = new TypeAlias(typeof(AnsiStringTypeHandler)); cacheAlias.Name = "AnsiStringTypeHandler"; _configScope.SqlMapper.TypeHandlerFactory.AddTypeAlias(cacheAlias.Name, cacheAlias); #endregion #region Load providers if (_configScope.IsCallFromDao == false) { GetProviders(); } #endregion #region Load DataBase #region Choose the provider IDbProvider provider = null; if ( _configScope.IsCallFromDao==false ) { provider = ParseProvider(); _configScope.ErrorContext.Reset(); } #endregion #region Load the DataSources _configScope.ErrorContext.Activity = "loading Database DataSource"; XmlNode nodeDataSource = _configScope.SqlMapConfigDocument.SelectSingleNode( ApplyDataMapperNamespacePrefix(XML_DATABASE_DATASOURCE), _configScope.XmlNamespaceManager ); if (nodeDataSource == null) { if (_configScope.IsCallFromDao == false) { throw new ConfigurationException("There's no dataSource tag in SqlMap.config."); } else // patch from Luke Yang { _configScope.SqlMapper.DataSource = _configScope.DataSource; } } else { if (_configScope.IsCallFromDao == false) { _configScope.ErrorContext.Resource = nodeDataSource.OuterXml.ToString(); _configScope.ErrorContext.MoreInfo = "parse DataSource"; DataSource dataSource = DataSourceDeSerializer.Deserialize( nodeDataSource ); dataSource.DbProvider = provider; dataSource.ConnectionString = NodeUtils.ParsePropertyTokens(dataSource.ConnectionString, _configScope.Properties); _configScope.DataSource = dataSource; _configScope.SqlMapper.DataSource = _configScope.DataSource; } else { _configScope.SqlMapper.DataSource = _configScope.DataSource; } _configScope.ErrorContext.Reset(); } #endregion #endregion #region Load Global TypeAlias foreach (XmlNode xmlNode in _configScope.SqlMapConfigDocument.SelectNodes( ApplyDataMapperNamespacePrefix(XML_GLOBAL_TYPEALIAS), _configScope.XmlNamespaceManager)) { _configScope.ErrorContext.Activity = "loading global Type alias"; TypeAliasDeSerializer.Deserialize(xmlNode, _configScope); } _configScope.ErrorContext.Reset(); #endregion #region Load TypeHandlers foreach (XmlNode xmlNode in _configScope.SqlMapConfigDocument.SelectNodes( ApplyDataMapperNamespacePrefix(XML_GLOBAL_TYPEHANDLER), _configScope.XmlNamespaceManager)) { try { _configScope.ErrorContext.Activity = "loading typeHandler"; TypeHandlerDeSerializer.Deserialize( xmlNode, _configScope ); } catch (Exception e) { NameValueCollection prop = NodeUtils.ParseAttributes(xmlNode, _configScope.Properties); throw new ConfigurationException( String.Format("Error registering TypeHandler class \"{0}\" for handling .Net type \"{1}\" and dbType \"{2}\". Cause: {3}", NodeUtils.GetStringAttribute(prop, "callback"), NodeUtils.GetStringAttribute(prop, "type"), NodeUtils.GetStringAttribute(prop, "dbType"), e.Message), e); } } _configScope.ErrorContext.Reset(); #endregion #region Load sqlMap mapping files foreach (XmlNode xmlNode in _configScope.SqlMapConfigDocument.SelectNodes( ApplyDataMapperNamespacePrefix(XML_SQLMAP), _configScope.XmlNamespaceManager)) { _configScope.NodeContext = xmlNode; ConfigureSqlMap(); } #endregion #region Attach CacheModel to statement if (_configScope.IsCacheModelsEnabled) { foreach(DictionaryEntry entry in _configScope.SqlMapper.MappedStatements) { _configScope.ErrorContext.Activity = "Set CacheModel to statement"; IMappedStatement mappedStatement = (IMappedStatement)entry.Value; if (mappedStatement.Statement.CacheModelName.Length >0) { _configScope.ErrorContext.MoreInfo = "statement : "+mappedStatement.Statement.Id; _configScope.ErrorContext.Resource = "cacheModel : " +mappedStatement.Statement.CacheModelName; mappedStatement.Statement.CacheModel = _configScope.SqlMapper.GetCache(mappedStatement.Statement.CacheModelName); } } } _configScope.ErrorContext.Reset(); #endregion #region Register Trigger Statements for Cache Models foreach (DictionaryEntry entry in _configScope.CacheModelFlushOnExecuteStatements) { string cacheModelId = (string)entry.Key; IList statementsToRegister = (IList)entry.Value; if (statementsToRegister != null && statementsToRegister.Count > 0) { foreach (string statementName in statementsToRegister) { IMappedStatement mappedStatement = _configScope.SqlMapper.MappedStatements[statementName] as IMappedStatement; if (mappedStatement != null) { CacheModel cacheModel = _configScope.SqlMapper.GetCache(cacheModelId); if (_logger.IsDebugEnabled) { _logger.Debug("Registering trigger statement [" + mappedStatement.Id + "] to cache model [" + cacheModel.Id + "]"); } cacheModel.RegisterTriggerStatement(mappedStatement); } else { if (_logger.IsWarnEnabled) { _logger.Warn("Unable to register trigger statement [" + statementName + "] to cache model [" + cacheModelId + "]. Statement does not exist."); } } } } } #endregion #region Resolve resultMap / Discriminator / PropertyStategy attributes on Result/Argument Property foreach(DictionaryEntry entry in _configScope.SqlMapper.ResultMaps) { _configScope.ErrorContext.Activity = "Resolve 'resultMap' attribute on Result Property"; ResultMap resultMap = (ResultMap)entry.Value; for(int index=0; index< resultMap.Properties.Count; index++) { ResultProperty result = resultMap.Properties[index]; if(result.NestedResultMapName.Length >0) { result.NestedResultMap = _configScope.SqlMapper.GetResultMap(result.NestedResultMapName); } result.PropertyStrategy = PropertyStrategyFactory.Get(result); } for(int index=0; index< resultMap.Parameters.Count; index++) { ResultProperty result = resultMap.Parameters[index]; if(result.NestedResultMapName.Length >0) { result.NestedResultMap = _configScope.SqlMapper.GetResultMap(result.NestedResultMapName); } result.ArgumentStrategy = ArgumentStrategyFactory.Get( (ArgumentProperty)result ); } if (resultMap.Discriminator != null) { resultMap.Discriminator.Initialize(_configScope); } } _configScope.ErrorContext.Reset(); #endregion }
/// <summary> /// Initializes a new instance of the <see cref="IsNotParameterPresent"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsNotParameterPresent(AccessorFactory accessorFactory) { this.Handler = new IsNotParameterPresentTagHandler(accessorFactory); }
/// <summary> /// Sets the member value. /// </summary> /// <param name="obj">he Object on which to invoke the specified mmber.</param> /// <param name="memberName">Name of the member.</param> /// <param name="memberValue">The member value.</param> /// <param name="objectFactory">The object factory.</param> /// <param name="accessorFactory">The accessor factory.</param> public static void SetMemberValue(object obj, string memberName, object memberValue, IObjectFactory objectFactory, AccessorFactory accessorFactory) { if (memberName.IndexOf('.') > -1) { StringTokenizer parser = new StringTokenizer(memberName, "."); IEnumerator enumerator = parser.GetEnumerator(); enumerator.MoveNext(); string currentPropertyName = (string)enumerator.Current; object child = obj; while (enumerator.MoveNext()) { Type type = GetMemberTypeForSetter(child, currentPropertyName); object parent = child; child = GetMember(parent, currentPropertyName, accessorFactory); if (child == null) { try { IFactory factory = objectFactory.CreateFactory(type, Type.EmptyTypes); child = factory.CreateInstance(Type.EmptyTypes); SetMemberValue(parent, currentPropertyName, child, objectFactory, accessorFactory); } catch (Exception e) { throw new ProbeException("Cannot set value of property '" + memberName + "' because '" + currentPropertyName + "' is null and cannot be instantiated on instance of " + type.Name + ". Cause:" + e.Message, e); } } currentPropertyName = (string)enumerator.Current; } SetMember(child, currentPropertyName, memberValue, accessorFactory); } else { SetMember(obj, memberName, memberValue, accessorFactory); } }
/// <summary> /// Initializes a new instance of the <see cref="IterateTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IterateTagHandler(AccessorFactory accessorFactory) : base(accessorFactory) { }
/// <summary> /// Sets the array member. /// </summary> /// <param name="obj">The obj.</param> /// <param name="indexedName">Name of the indexed.</param> /// <param name="value">The value.</param> /// <param name="accessorFactory">The accessor factory.</param> private static void SetArrayMember(object obj, string indexedName, object value, AccessorFactory accessorFactory) { try { int startIndex = indexedName.IndexOf("["); int length = indexedName.IndexOf("]"); string name = indexedName.Substring(0, startIndex); string index = indexedName.Substring( startIndex+1, length-(startIndex+1)); int i = System.Convert.ToInt32(index); object list = null; if (name.Length > 0) { list = GetMember(obj, name, accessorFactory); } else { list = obj; } if (list is IList) { ((IList) list)[i] = value; } else { throw new ProbeException("The '" + name + "' member of the " + obj.GetType().Name + " class is not a List or Array."); } } catch (ProbeException pe) { throw pe; } catch (Exception e) { throw new ProbeException("Error getting ordinal value from .net object. Cause" + e.Message, e); } }
/// <summary> /// Initializes a new instance of the <see cref="IsLessEqualTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsLessEqualTagHandler(AccessorFactory accessorFactory) : base(accessorFactory) { }
/// <summary> /// Initializes a new instance of the <see cref="ConditionalTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public ConditionalTagHandler(AccessorFactory accessorFactory) : base(accessorFactory) { }
/// <summary> /// Initializes a new instance of the <see cref="IsNotPropertyAvailable"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsNotPropertyAvailable(AccessorFactory accessorFactory) { this.Handler = new IsNotPropertyAvailableTagHandler(accessorFactory); }
/// <summary> /// Initializes a new instance of the <see cref="IsNotParameterPresentTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsNotParameterPresentTagHandler(AccessorFactory accessorFactory) : base(accessorFactory) { }
public BaseTagHandler(IBatisNet.Common.Utilities.Objects.Members.AccessorFactory accessorFactory) { this._accessorFactory = accessorFactory; }
/// <summary> /// Initializes a new instance of the <see cref="IsPropertyAvailableTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsPropertyAvailableTagHandler(AccessorFactory accessorFactory) : base(accessorFactory) { }
/// <summary> /// Initializes a new instance of the <see cref="IsEmptyTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsEmptyTagHandler(AccessorFactory accessorFactory) : base(accessorFactory) { }
/// <summary> /// Initializes a new instance of the <see cref="IsLessEqual"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsLessEqual(AccessorFactory accessorFactory) { this.Handler = new IsLessEqualTagHandler(accessorFactory); }
/// <summary> /// Initializes a new instance of the <see cref="IsGreaterThanTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsGreaterThanTagHandler(AccessorFactory accessorFactory) : base(accessorFactory) { }
/// <summary> /// Initializes a new instance of the <see cref="IsNullTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public IsNullTagHandler(AccessorFactory accessorFactory) : base(accessorFactory) { }
/// <summary> /// Initializes a new instance of the <see cref="DynamicTagHandler"/> class. /// </summary> /// <param name="accessorFactory">The accessor factory.</param> public DynamicTagHandler(AccessorFactory accessorFactory) : base(accessorFactory) { }