/// <summary> /// Builds the the MyBatis core model (statement, alias, resultMap, parameterMap, dataSource) /// from an <see cref="IConfigurationStore"/> and store all the refrences in an <see cref="IModelStore"/> . /// </summary> /// <param name="configurationSetting">The configuration setting.</param> /// <param name="store">The configuration store.</param> /// <returns>The model store</returns> public virtual void BuildModel(ConfigurationSetting configurationSetting, IConfigurationStore store) { IObjectFactory objectFactory = null; IGetAccessorFactory getAccessorFactory = null; ISetAccessorFactory setAccessorFactory = null; ISessionFactory sessionFactory = null; ISessionStore sessionStore = null; if (configurationSetting != null) { objectFactory = configurationSetting.ObjectFactory; setAccessorFactory = configurationSetting.SetAccessorFactory; getAccessorFactory = configurationSetting.GetAccessorFactory; dataSource = configurationSetting.DataSource; sessionFactory = configurationSetting.SessionFactory; sessionStore = configurationSetting.SessionStore; dynamicSqlEngine = configurationSetting.DynamicSqlEngine; isCacheModelsEnabled = configurationSetting.IsCacheModelsEnabled; useStatementNamespaces = configurationSetting.UseStatementNamespaces; useReflectionOptimizer = configurationSetting.UseReflectionOptimizer; preserveWhitespace = configurationSetting.PreserveWhitespace; } // Xml setting override code setting LoadSetting(store); if (objectFactory == null) { objectFactory = new ObjectFactory(useReflectionOptimizer); } if (setAccessorFactory == null) { setAccessorFactory = new SetAccessorFactory(useReflectionOptimizer); } if (getAccessorFactory == null) { getAccessorFactory = new GetAccessorFactory(useReflectionOptimizer); } AccessorFactory accessorFactory = new AccessorFactory(setAccessorFactory, getAccessorFactory); TypeHandlerFactory typeHandlerFactory = new TypeHandlerFactory(); TypeAlias alias = new TypeAlias("MEMORY", typeof(PerpetualCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("Perpetual", typeof(PerpetualCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("LRU", typeof(LruCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("Lru", typeof(LruCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("FIFO", typeof(FifoCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("Fifo", typeof(FifoCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("Weak", typeof(WeakCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("WEAK", typeof(WeakCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("AnsiStringTypeHandler", typeof(AnsiStringTypeHandler)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); modelStore.DataExchangeFactory = new DataExchangeFactory(typeHandlerFactory, objectFactory, accessorFactory); if (sessionStore == null) { sessionStore = SessionStoreFactory.GetSessionStore(modelStore.Id); } modelStore.SessionStore = sessionStore; deSerializerFactory = new DeSerializerFactory(modelStore); ParameterMap emptyParameterMap = new ParameterMap( ConfigConstants.EMPTY_PARAMETER_MAP, string.Empty, string.Empty, typeof(string), modelStore.DataExchangeFactory.GetDataExchangeForClass(null), false); modelStore.AddParameterMap(emptyParameterMap); BuildProviders(store); BuildDataSource(store); if (sessionFactory == null) { sessionFactory = new DefaultSessionFactory( dataSource, modelStore.SessionStore, new DefaultTransactionManager(new AdoTransactionFactory())); } modelStore.SessionFactory = sessionFactory; BuildTypeAlias(store); BuildTypeHandlers(store); BuildCacheModels(store); BuildResultMaps(store); for (int i = 0; i < nestedProperties.Count; i++) { ResultProperty property = nestedProperties[i]; property.NestedResultMap = modelStore.GetResultMap(property.NestedResultMapName); } for (int i = 0; i < discriminators.Count; i++) { discriminators[i].Initialize(modelStore); } BuildParameterMaps(store); BuildMappedStatements(store, configurationSetting); for (int i = 0; i < store.CacheModels.Length; i++) { CacheModel cacheModel = modelStore.GetCacheModel(store.CacheModels[i].Id); for (int j = 0; j < cacheModel.StatementFlushNames.Count; j++) { string statement = cacheModel.StatementFlushNames[j]; IMappedStatement mappedStatement = modelStore.GetMappedStatement(statement); if (mappedStatement != null) { cacheModel.RegisterTriggerStatement(mappedStatement); if (logger.IsDebugEnabled) { logger.Debug("Registering trigger statement [" + statement + "] to cache model [" + cacheModel.Id + "]"); } } else { if (logger.IsWarnEnabled) { logger.Warn("Unable to register trigger statement [" + statement + "] to cache model [" + cacheModel.Id + "]. Statement does not exist."); } } } } if (logger.IsInfoEnabled) { logger.Info("Model Store"); logger.Info(modelStore.ToString()); } }
private void CheckModelStore(IModelStore store) { Console.WriteLine(store.ToString()); //Assert.That(store.Properties.Length, Is.EqualTo(14)); //Assert.That(store.GetPropertyConfiguration("useStatementNamespaces").Value, Is.EqualTo("false")); //Assert.That(store.Settings.Length, Is.EqualTo(4)); //Assert.That(store.GetSettingConfiguration("validateSqlMap").Value, Is.EqualTo("false")); //Assert.That(store.Providers.Length, Is.EqualTo(17)); //IConfiguration informixProvider = store.GetProviderConfiguration("Informix"); //Assert.That(informixProvider.Attributes["description"], Is.EqualTo("Informix NET Provider, 2.81.0.0")); //Assert.That(informixProvider.Attributes["commandBuilderClass"], Is.EqualTo("IBM.Data.Informix.IfxCommandBuilder")); //Assert.That(store.Databases.Length, Is.EqualTo(1)); //Assert.That(store.Databases[0].Children.Count, Is.EqualTo(2)); //IConfiguration provider = store.Databases[0].Children.Find(DataConstants.ELEMENT_PROVIDER)[0]; //Assert.That(provider, Is.Not.Null); //Assert.That(provider.Attributes[DataConstants.ATTRIBUTE_NAME], Is.EqualTo("sqlServer1.1")); //Assert.That(provider.Id, Is.EqualTo("sqlServer1.1")); //Assert.That(provider.Value, Is.EqualTo("sqlServer1.1")); //Assert.That(provider.Type, Is.EqualTo(DataConstants.ELEMENT_PROVIDER)); //IConfiguration datasource = store.Databases[0].Children.Find(DataConstants.ELEMENT_DATASOURCE)[0]; //Assert.That(datasource, Is.Not.Null); //Assert.That(datasource.Value.StartsWith("data source=")); //Assert.That(datasource.Type, Is.EqualTo(DataConstants.ELEMENT_DATASOURCE)); //Assert.That(store.Alias.Length, Is.EqualTo(12)); //Assert.That(store.GetAliasConfiguration("Account").Value, Is.EqualTo("MyBatis.DataMapper.SqlClient.Test.Domain.Account, MyBatis.DataMapper.SqlClient.Test")); //Assert.That(store.TypeHandlers.Length, Is.EqualTo(2)); //Assert.That(store.GetTypeHandlerConfiguration("bool").Attributes.Count, Is.EqualTo(3)); //Assert.That(store.GetTypeHandlerConfiguration("string").Attributes.Count, Is.EqualTo(2)); //// Cache model //Assert.That(store.CacheModels.Length, Is.EqualTo(2)); //IConfiguration cacheModel = store.CacheModels[0]; //Assert.That(cacheModel.Children.Count, Is.EqualTo(4)); //Assert.That(cacheModel.Attributes.ContainsKey(ConfigConstants.ATTRIBUTE_NAMESPACE), Is.True); //// Result map //Assert.That(store.ResultMaps.Length, Is.EqualTo(3 + 3 + 2)); //IConfiguration resultMap = store.GetResultMapConfiguration("Account.account-result-constructor"); //Assert.IsNotNull(resultMap); //Assert.That(resultMap.Children.Count, Is.EqualTo(4)); //IConfiguration constructor = resultMap.Children.Find(ConfigConstants.ELEMENT_CONSTRUCTOR)[0]; //Assert.IsNotNull(constructor); //Assert.That(constructor.Children.Count, Is.EqualTo(3)); //ConfigurationCollection arguments = constructor.Children.Find(ConfigConstants.ELEMENT_ARGUMENT); //Assert.IsNotEmpty(arguments); //Assert.That(arguments.Count, Is.EqualTo(3)); //Assert.That(arguments[0].Attributes[ConfigConstants.ATTRIBUTE_ARGUMENTNAME], Is.EqualTo("identifiant")); //ConfigurationCollection results = resultMap.Children.Find(ConfigConstants.ELEMENT_RESULT); //Assert.IsNotEmpty(results); //Assert.That(results.Count, Is.EqualTo(3)); //Assert.That(results[0].Attributes[ConfigConstants.ATTRIBUTE_PROPERTY], Is.EqualTo("EmailAddress")); //// Parameter map //Assert.That(store.ParameterMaps.Length, Is.EqualTo(7)); //IConfiguration parameterMap = store.GetParameterMapConfiguration("Account.insert-params"); //Assert.IsNotNull(parameterMap); //Assert.That(parameterMap.Children.Count, Is.EqualTo(6)); //ConfigurationCollection parameters = parameterMap.Children.Find(ConfigConstants.ELEMENT_PARAMETER); //Assert.IsNotEmpty(parameters); //Assert.That(parameters.Count, Is.EqualTo(6)); //Assert.That(parameters[4].Attributes.Count, Is.EqualTo(4)); //Assert.That(parameters[3].Attributes[ConfigConstants.ATTRIBUTE_PROPERTY], Is.EqualTo("EmailAddress")); //// sql statement in Mapping2.xml //Assert.That(store.Statements.Length, Is.EqualTo(97)); //IConfiguration sqlStatement = store.GetStatementConfiguration("includeComplex"); //Assert.IsNotNull(sqlStatement); //Assert.That(sqlStatement.Attributes.ContainsKey(ConfigConstants.ATTRIBUTE_NAMESPACE), Is.True); //Assert.That(sqlStatement.Children.Count, Is.EqualTo(1)); //IConfiguration child = sqlStatement.Children[0];//dynamic //Assert.That(child.Children.Count, Is.EqualTo(1)); //child = child.Children[0];//isParameterPresent //Assert.That(child.Children.Count, Is.EqualTo(3)); //child = child.Children[1];//isNotEmpty //Assert.That(child.Attributes.Count, Is.EqualTo(2)); //Assert.IsTrue(child.Attributes.ContainsKey("prepend")); //Assert.IsTrue(child.Attributes.ContainsKey("property")); //Assert.That(child.Attributes["prepend"], Is.EqualTo("and")); //Assert.That(child.Attributes["property"], Is.EqualTo("FirstName")); }
private void CheckModelStore(IModelStore store) { Console.WriteLine(store.ToString()); //Assert.That(store.Properties.Length, Is.EqualTo(14)); //Assert.That(store.GetPropertyConfiguration("useStatementNamespaces").Value, Is.EqualTo("false")); //Assert.That(store.Settings.Length, Is.EqualTo(4)); //Assert.That(store.GetSettingConfiguration("validateSqlMap").Value, Is.EqualTo("false")); //Assert.That(store.Providers.Length, Is.EqualTo(17)); //IConfiguration informixProvider = store.GetProviderConfiguration("Informix"); //Assert.That(informixProvider.Attributes["description"], Is.EqualTo("Informix NET Provider, 2.81.0.0")); //Assert.That(informixProvider.Attributes["commandBuilderClass"], Is.EqualTo("IBM.Data.Informix.IfxCommandBuilder")); //Assert.That(store.Databases.Length, Is.EqualTo(1)); //Assert.That(store.Databases[0].Children.Count, Is.EqualTo(2)); //IConfiguration provider = store.Databases[0].Children.Find(DataConstants.ELEMENT_PROVIDER)[0]; //Assert.That(provider, Is.Not.Null); //Assert.That(provider.Attributes[DataConstants.ATTRIBUTE_NAME], Is.EqualTo("sqlServer1.1")); //Assert.That(provider.Id, Is.EqualTo("sqlServer1.1")); //Assert.That(provider.Value, Is.EqualTo("sqlServer1.1")); //Assert.That(provider.Type, Is.EqualTo(DataConstants.ELEMENT_PROVIDER)); //IConfiguration datasource = store.Databases[0].Children.Find(DataConstants.ELEMENT_DATASOURCE)[0]; //Assert.That(datasource, Is.Not.Null); //Assert.That(datasource.Value.StartsWith("data source=")); //Assert.That(datasource.Type, Is.EqualTo(DataConstants.ELEMENT_DATASOURCE)); //Assert.That(store.Alias.Length, Is.EqualTo(12)); //Assert.That(store.GetAliasConfiguration("Account").Value, Is.EqualTo("MyBatis.DataMapper.Sqlite.Test.Domain.Account, MyBatis.DataMapper.Sqlite.Test")); //Assert.That(store.TypeHandlers.Length, Is.EqualTo(2)); //Assert.That(store.GetTypeHandlerConfiguration("bool").Attributes.Count, Is.EqualTo(3)); //Assert.That(store.GetTypeHandlerConfiguration("string").Attributes.Count, Is.EqualTo(2)); //// Cache model //Assert.That(store.CacheModels.Length, Is.EqualTo(2)); //IConfiguration cacheModel = store.CacheModels[0]; //Assert.That(cacheModel.Children.Count, Is.EqualTo(4)); //Assert.That(cacheModel.Attributes.ContainsKey(ConfigConstants.ATTRIBUTE_NAMESPACE), Is.True); //// Result map //Assert.That(store.ResultMaps.Length, Is.EqualTo(3 + 3 + 2)); //IConfiguration resultMap = store.GetResultMapConfiguration("Account.account-result-constructor"); //Assert.IsNotNull(resultMap); //Assert.That(resultMap.Children.Count, Is.EqualTo(4)); //IConfiguration constructor = resultMap.Children.Find(ConfigConstants.ELEMENT_CONSTRUCTOR)[0]; //Assert.IsNotNull(constructor); //Assert.That(constructor.Children.Count, Is.EqualTo(3)); //ConfigurationCollection arguments = constructor.Children.Find(ConfigConstants.ELEMENT_ARGUMENT); //Assert.IsNotEmpty(arguments); //Assert.That(arguments.Count, Is.EqualTo(3)); //Assert.That(arguments[0].Attributes[ConfigConstants.ATTRIBUTE_ARGUMENTNAME], Is.EqualTo("identifiant")); //ConfigurationCollection results = resultMap.Children.Find(ConfigConstants.ELEMENT_RESULT); //Assert.IsNotEmpty(results); //Assert.That(results.Count, Is.EqualTo(3)); //Assert.That(results[0].Attributes[ConfigConstants.ATTRIBUTE_PROPERTY], Is.EqualTo("EmailAddress")); //// Parameter map //Assert.That(store.ParameterMaps.Length, Is.EqualTo(7)); //IConfiguration parameterMap = store.GetParameterMapConfiguration("Account.insert-params"); //Assert.IsNotNull(parameterMap); //Assert.That(parameterMap.Children.Count, Is.EqualTo(6)); //ConfigurationCollection parameters = parameterMap.Children.Find(ConfigConstants.ELEMENT_PARAMETER); //Assert.IsNotEmpty(parameters); //Assert.That(parameters.Count, Is.EqualTo(6)); //Assert.That(parameters[4].Attributes.Count, Is.EqualTo(4)); //Assert.That(parameters[3].Attributes[ConfigConstants.ATTRIBUTE_PROPERTY], Is.EqualTo("EmailAddress")); //// sql statement in Mapping2.xml //Assert.That(store.Statements.Length, Is.EqualTo(97)); //IConfiguration sqlStatement = store.GetStatementConfiguration("includeComplex"); //Assert.IsNotNull(sqlStatement); //Assert.That(sqlStatement.Attributes.ContainsKey(ConfigConstants.ATTRIBUTE_NAMESPACE), Is.True); //Assert.That(sqlStatement.Children.Count, Is.EqualTo(1)); //IConfiguration child = sqlStatement.Children[0];//dynamic //Assert.That(child.Children.Count, Is.EqualTo(1)); //child = child.Children[0];//isParameterPresent //Assert.That(child.Children.Count, Is.EqualTo(3)); //child = child.Children[1];//isNotEmpty //Assert.That(child.Attributes.Count, Is.EqualTo(2)); //Assert.IsTrue(child.Attributes.ContainsKey("prepend")); //Assert.IsTrue(child.Attributes.ContainsKey("property")); //Assert.That(child.Attributes["prepend"], Is.EqualTo("and")); //Assert.That(child.Attributes["property"], Is.EqualTo("FirstName")); }
/// <summary> /// Builds the the iBATIS core model (statement, alias, resultMap, parameterMap, dataSource) /// from an <see cref="IConfigurationStore"/> and store all the refrences in an <see cref="IModelStore"/> . /// </summary> /// <param name="configurationSetting">The configuration setting.</param> /// <param name="store">The configuration store.</param> /// <returns>The model store</returns> public virtual void BuildModel(ConfigurationSetting configurationSetting, IConfigurationStore store) { IObjectFactory objectFactory = null; IGetAccessorFactory getAccessorFactory = null; ISetAccessorFactory setAccessorFactory = null; ISessionFactory sessionFactory = null; ISessionStore sessionStore = null; if (configurationSetting != null) { objectFactory = configurationSetting.ObjectFactory; setAccessorFactory = configurationSetting.SetAccessorFactory; getAccessorFactory = configurationSetting.GetAccessorFactory; dataSource = configurationSetting.DataSource; sessionFactory = configurationSetting.SessionFactory; sessionStore = configurationSetting.SessionStore; dynamicSqlEngine = configurationSetting.DynamicSqlEngine; isCacheModelsEnabled = configurationSetting.IsCacheModelsEnabled; useStatementNamespaces = configurationSetting.UseStatementNamespaces; useReflectionOptimizer = configurationSetting.UseReflectionOptimizer; preserveWhitespace = configurationSetting.PreserveWhitespace; } // Xml setting override code setting //取store中的数据初始化类成员 为下面做准备 LoadSetting(store); if (objectFactory == null) { objectFactory = new ObjectFactory(useReflectionOptimizer); } if (setAccessorFactory == null) { setAccessorFactory = new SetAccessorFactory(useReflectionOptimizer); } if (getAccessorFactory == null) { getAccessorFactory = new GetAccessorFactory(useReflectionOptimizer); } AccessorFactory accessorFactory = new AccessorFactory(setAccessorFactory, getAccessorFactory); TypeHandlerFactory typeHandlerFactory = new TypeHandlerFactory(); TypeAlias alias = new TypeAlias("MEMORY", typeof(PerpetualCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("Perpetual", typeof(PerpetualCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("LRU", typeof(LruCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("Lru", typeof(LruCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("FIFO", typeof(FifoCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("Fifo", typeof(FifoCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("Weak", typeof(WeakCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("WEAK", typeof(WeakCache)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("MemCached", typeof(MemCached)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("MEMCACHED", typeof(MemCached)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); alias = new TypeAlias("AnsiStringTypeHandler", typeof(AnsiStringTypeHandler)); typeHandlerFactory.AddTypeAlias(alias.Id, alias); //将以上类信息存入到modelStore中 实质性工作 modelStore.DataExchangeFactory = new DataExchangeFactory(typeHandlerFactory, objectFactory, accessorFactory); if (sessionStore == null) { sessionStore = SessionStoreFactory.GetSessionStore(modelStore.Id); } //将ISessionStore类对象存入modelStore中 准备数据库的连接 与 事物操作功能 实质性工作 modelStore.SessionStore = sessionStore; //初始化DefaultModelBuilder的成员变量 deSerializerFactory = new DeSerializerFactory(modelStore); //设置一个空类型的参数映射类 ParameterMap emptyParameterMap = new ParameterMap( ConfigConstants.EMPTY_PARAMETER_MAP, string.Empty, string.Empty, typeof(string), modelStore.DataExchangeFactory.GetDataExchangeForClass(null), //获得ComplexDataExchange对象 false); //向参数字典中添加一个空参数类 modelStore.AddParameterMap(emptyParameterMap); //完成了对DefaultModelBuilder成员变量dbProviderFactory的初始化,其中完成了对provider使用中节点的类初始化 BuildProviders(store); //完成了对DefaultModelBuilder成员变量IDataSource的初始化,包括数据库的名字 连接字符串 以及DbPrivder类 BuildDataSource(store); if (sessionFactory == null) { sessionFactory = new DefaultSessionFactory( dataSource, modelStore.SessionStore, new DefaultTransactionManager(new AdoTransactionFactory())); } //初始化modelStore中的ISessionFactory变量 modelStore.SessionFactory = sessionFactory; //将具体SQLXML配置文件中的Alias节点对应的别名信息写入到modelStore中的TypeHandlerFactory的字典中 BuildTypeAlias(store); //将TypeHanders下的子节点信息放入 modelStore.DataExchangeFactory.TypeHandlerFactory中的字典当中 BuildTypeHandlers(store); //将cacheModels下子节点cacheModel加入到modelStore的CacheModel字典中 BuildCacheModels(store); //将resultMap节点信息添加到modelStore的resultMaps中 BuildResultMaps(store); //为resultMapping属性所在的节点信息类设置对应的ResultProperty for (int i = 0; i < nestedProperties.Count; i++) { ResultProperty property = nestedProperties[i]; property.NestedResultMap = modelStore.GetResultMap(property.NestedResultMapName); } for (int i = 0; i < discriminators.Count; i++) { //完成对discriminator类中对字典的初始化 discriminators[i].Initialize(modelStore); } //将parameter节点信息添加到modelStore类中的parameterMaps字典中 BuildParameterMaps(store); //将statements下的节点添加到modelStore的statements的字典中 BuildMappedStatements(store, configurationSetting); for (int i = 0; i < store.CacheModels.Length; i++) { CacheModel cacheModel = modelStore.GetCacheModel(store.CacheModels[i].Id); for (int j = 0; j < cacheModel.StatementFlushNames.Count; j++) { string statement = cacheModel.StatementFlushNames[j]; IMappedStatement mappedStatement = modelStore.GetMappedStatement(statement); if (mappedStatement != null) { //为IMappedStatement类的Executed制定委托事件 目的是清空缓存 cacheModel.RegisterTriggerStatement(mappedStatement); if (logger.IsDebugEnabled) { logger.Debug("Registering trigger statement [" + statement + "] to cache model [" + cacheModel.Id + "]"); } } else { if (logger.IsWarnEnabled) { logger.Warn("Unable to register trigger statement [" + statement + "] to cache model [" + cacheModel.Id + "]. Statement does not exist."); } } } } if (logger.IsInfoEnabled) { logger.Info("Model Store"); logger.Info(modelStore.ToString()); } }