User: mzywitza
Date: 2010/01/06 09:55 PM

Added:
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/
  DefaultConfigurationsTestCase.cs
 /ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Config/
  DatabaseType.cs, DefaultDatabaseConfiguration.cs

Modified:
 /ActiveRecord/trunk/src/
  Changes.txt
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/
  AbstractActiveRecordTest.cs, ActiveRecordBaseGenericsTestCase.cs, 
Castle.ActiveRecord.Tests-vs2008.csproj
 /ActiveRecord/trunk/src/Castle.ActiveRecord/
  Castle.ActiveRecord-vs2008.csproj
 /ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/
  ActiveRecordStarter.cs
 /ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Config/
  InPlaceConfigurationSource.cs, XmlConfigurationSource.cs

Log:
 Applied patch by xtoff for simplifying xml configuration.

File Changes:

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord/
=======================================================

File [modified]: Castle.ActiveRecord-vs2008.csproj
Delta lines: +14 -10
===================================================================

--- ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/ActiveRecordStarter.cs 
2010-01-06 21:39:00 UTC (rev 6590)
+++ ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/ActiveRecordStarter.cs 
2010-01-07 04:55:46 UTC (rev 6591)
@@ -12,8 +12,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-using Castle.ActiveRecord.Framework.Internal.EventListener;
-
 namespace Castle.ActiveRecord
 {
        using System;
@@ -22,19 +20,24 @@
        using System.Data;
        using System.IO;
        using System.Reflection;
-       using Attributes;
+
+       using Castle.ActiveRecord.Attributes;
        using Castle.ActiveRecord.Framework;
        using Castle.ActiveRecord.Framework.Config;
        using Castle.ActiveRecord.Framework.Internal;
+       using Castle.ActiveRecord.Framework.Internal.EventListener;
        using Castle.ActiveRecord.Framework.Scopes;
        using Castle.Core.Configuration;
-       using NHibernate.Cfg;
+
        using Iesi.Collections;
        using Iesi.Collections.Generic;
+
+       using NHibernate.Cfg;
        using NHibernate.Criterion;
        using NHibernate.Tool.hbm2ddl;
-       using Environment=NHibernate.Cfg.Environment;
 
+       using Environment = NHibernate.Cfg.Environment;
+
        /// <summary>
        /// Delegate for use in <see cref="ActiveRecordStarter.ModelsCreated"/> 
and <see cref="ActiveRecordStarter.ModelsValidated"/>
        /// </summary>
@@ -155,21 +158,22 @@
                                {
                                        throw new 
ActiveRecordInitializationException("You can't invoke 
ActiveRecordStarter.Initialize more than once");
                                }
-                               
-                               foreach (Type type in types)
-                               {
-                                       registeredAssemblies.Add(type.Assembly);
-                               }
 
                                if (source == null)
                                {
                                        throw new 
ArgumentNullException("source");
                                }
+
                                if (types == null)
                                {
                                        throw new 
ArgumentNullException("types");
                                }
 
+                               foreach (Type type in types)
+                               {
+                                       registeredAssemblies.Add(type.Assembly);
+                               }
+
                                registeredTypes = new Dictionary<Type, 
string>();
 

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/
=============================================================

File [modified]: AbstractActiveRecordTest.cs
Delta lines: +0 -3
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/ActiveRecordBaseGenericsTestCase.cs
        2010-01-06 21:39:00 UTC (rev 6590)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/ActiveRecordBaseGenericsTestCase.cs
        2010-01-07 04:55:46 UTC (rev 6591)
@@ -14,9 +14,6 @@
 
 namespace Castle.ActiveRecord.Tests
 {
-       using System;
-       using System.Text;
-       using System.Reflection;
        using System.Collections.Generic;
 

File [modified]: ActiveRecordBaseGenericsTestCase.cs
Delta lines: +1 -0
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Castle.ActiveRecord.Tests-vs2008.csproj
    2010-01-06 21:39:00 UTC (rev 6590)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Castle.ActiveRecord.Tests-vs2008.csproj
    2010-01-07 04:55:46 UTC (rev 6591)
@@ -330,6 +330,7 @@
     <Compile Include="CompositeUserTypeTestCase.cs" />
     <Compile Include="Config\ConfigureTests.cs" />
     <Compile Include="Config\StorageConfigurationTests.cs" />
+    <Compile Include="DefaultConfigurationsTestCase.cs" />
     <Compile Include="Model\CompositeUserType\BadProduct.cs" />
     <Compile Include="Model\ProductWithGuid.cs" />

File [modified]: Castle.ActiveRecord.Tests-vs2008.csproj
Delta lines: +163 -0
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/DefaultConfigurationsTestCase.cs
                           (rev 0)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/DefaultConfigurationsTestCase.cs
   2010-01-07 04:55:46 UTC (rev 6591)
@@ -0,0 +1,164 @@
+// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
+// 
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// 
+//     http://www.apache.org/licenses/LICENSE-2.0
+// 
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+namespace Castle.ActiveRecord.Tests
+{
+       using System.Configuration;
+       using System.IO;
+       using System.Linq;
+
+       using Castle.ActiveRecord.Framework;
+       using Castle.ActiveRecord.Framework.Config;
+
+       using NHibernate.ByteCode.Castle;
+       using NHibernate.Connection;
+       using NHibernate.Dialect;
+       using NHibernate.Driver;
+
+       using NUnit.Framework;
+
+       using Configuration = NHibernate.Cfg.Configuration;
+
+       [TestFixture]
+       public class DefaultConfigurationsTestCase
+       {
+               string configTemplate =
+                       "<activerecord>\r\n\t<config database=\"{0}\" 
connectionStringName=\"mycs\" />\r\n</activerecord>";
+
+               string cache_use_second_level_cache = 
"cache.use_second_level_cache";
+               string connection_driver_class = "connection.driver_class";
+               string connection_provider = "connection.provider";
+               string dialect = "dialect";
+               string proxyfactory_factory_class = 
"proxyfactory.factory_class";
+               string connection_connection_string_name = 
"connection.connection_string_name";
+
+               [SetUp]
+               public void SetUp()
+               {
+                       ActiveRecordStarter.ResetInitializationFlag();
+               }
+
+               [Test]
+               public void SqlServer2005Defaults()
+               {
+                       var configuration = 
BuildConfiguration("MsSqlServer2005");
+                       AssertPropertyEquals(configuration, dialect, 
typeof(MsSql2005Dialect).AssemblyQualifiedName);
+                       AssertPropertyEquals(configuration, 
connection_provider, typeof(DriverConnectionProvider).AssemblyQualifiedName);
+                       AssertPropertyEquals(configuration, 
connection_driver_class, typeof(SqlClientDriver).AssemblyQualifiedName);
+                       AssertPropertyEquals(configuration, 
proxyfactory_factory_class, typeof(ProxyFactoryFactory).AssemblyQualifiedName);
+                       AssertPropertyEquals(configuration, 
cache_use_second_level_cache, false.ToString());
+                       AssertPropertyEquals(configuration, 
connection_connection_string_name, "mycs");
+               }
+
+               [Test]
+               public void SQLiteDefaults()
+               {
+                       var configuration = BuildConfiguration("SQLite");
+                       AssertPropertyEquals(configuration, dialect, 
typeof(SQLiteDialect).AssemblyQualifiedName);
+                       AssertPropertyEquals(configuration, 
connection_provider, typeof(DriverConnectionProvider).AssemblyQualifiedName);
+                       AssertPropertyEquals(configuration, 
connection_driver_class, typeof(SQLite20Driver).AssemblyQualifiedName);
+                       AssertPropertyEquals(configuration, 
proxyfactory_factory_class, typeof(ProxyFactoryFactory).AssemblyQualifiedName);
+                       AssertPropertyEquals(configuration, 
cache_use_second_level_cache, false.ToString());
+                       AssertPropertyEquals(configuration, 
connection_connection_string_name, "mycs");
+                       AssertPropertyEquals(configuration, 
"query.substitutions", "true=1;false=0");
+               }
+
+               [Test]
+               public void Throws_when_connectionStringName_not_specified()
+               {
+                       var value = @"<activerecord>
+       <config database=""MsSqlServer2005"" />
+</activerecord>";
+                       TestDelegate action = () => 
+
+                               BuildConfiguration(ReadConfiguration(value));
+
+                       var ex = 
Assert.Throws<ConfigurationErrorsException>(action);
+                       Assert.AreEqual(
+                               "Using short form of configuration requires 
both 'database' and 'connectionStringName' attributes to be specified.",
+                               ex.Message);
+               }
+               [Test]
+               public void Throws_when_database_not_specified()
+               {
+                       var value = @"<activerecord>
+       <config connectionStringName=""foobar"" />
+</activerecord>";
+                       TestDelegate action = () =>
+
+                               BuildConfiguration(ReadConfiguration(value));
+
+                       var ex = 
Assert.Throws<ConfigurationErrorsException>(action);
+                       Assert.AreEqual(
+                               "Using short form of configuration requires 
both 'database' and 'connectionStringName' attributes to be specified.",
+                               ex.Message);
+               }
+
+               [Test]
+               public void Can_use_shorthand_attribute_form()
+               {
+                       var value = @"<activerecord>
+       <config csn=""foobar"" db=""MsSqlServer2005"" />
+</activerecord>";
+                       var configuration = 
BuildConfiguration(ReadConfiguration(value));
+                       AssertPropertyEquals(configuration, dialect, 
typeof(MsSql2005Dialect).AssemblyQualifiedName);
+                       AssertPropertyEquals(configuration, 
connection_connection_string_name, "foobar");
+               }
+
+               [Test]
+               public void Can_override_defaults()
+               {
+                       var value = @"<activerecord>
+       <config csn=""foobar"" db=""MsSqlServer2005"">
+               <add key=""cache.use_second_level_cache"" value=""True"" />
+       </config>
+</activerecord>";
+                       var configuration = 
BuildConfiguration(ReadConfiguration(value));
+                       AssertPropertyEquals(configuration, 
cache_use_second_level_cache, "True");
+               }
+
+               private Configuration BuildConfiguration(string dbName)
+               {
+                       var source = ReadValidConfiguration(dbName);
+                       return BuildConfiguration(source);
+               }
+
+               private Configuration BuildConfiguration(IConfigurationSource 
source)
+               {
+                       ISessionFactoryHolder holder = null;
+                       ActiveRecordStarter.SessionFactoryHolderCreated += h => 
holder = h;
+                       ActiveRecordStarter.Initialize(source);
+
+                       return holder.GetAllConfigurations().Single();
+               }
+
+               private void AssertPropertyEquals(Configuration configuration, 
string name, string value)
+               {
+                       Assert.AreEqual(value, configuration.Properties[name]);
+               }
+
+               private IConfigurationSource ReadValidConfiguration(string 
dbName)
+               {
+                       return ReadConfiguration(string.Format(configTemplate, 
dbName));
+               }
+
+               private IConfigurationSource ReadConfiguration(string value)
+               {
+                       using (var reader = new StringReader(value))
+                       {
+                               return new XmlConfigurationSource(reader);
+                       }
+               }
+       }

File [added]: DefaultConfigurationsTestCase.cs
Delta lines: +5 -0
===================================================================

--- ActiveRecord/trunk/src/Changes.txt  2010-01-06 21:39:00 UTC (rev 6590)
+++ ActiveRecord/trunk/src/Changes.txt  2010-01-07 04:55:46 UTC (rev 6591)
@@ -4,6 +4,11 @@
 - Updated Castle Core to 1.2
 - Updated Castle DynamicProcy to 2.2
 - Added support for NHibernate's new access methods "readonly" and "backfield".
+- Added shorthand configuration: It is now possible to use
+    <activerecord>
+      <config db="MsSqlServer2005" csn="MyConnectionStringName"/>
+    </activerecord>
+  to get default configurations for different DBs (Sql Server, SQLite, Oracle, 
MySql and others)
 
 - Bugs fixed:

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Config/
========================================================================

File [added]: DatabaseType.cs
Delta lines: +128 -0
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Config/DefaultDatabaseConfiguration.cs
                         (rev 0)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Config/DefaultDatabaseConfiguration.cs
 2010-01-07 04:55:46 UTC (rev 6591)
@@ -0,0 +1,129 @@
+// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
+// 
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// 
+//     http://www.apache.org/licenses/LICENSE-2.0
+// 
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+namespace Castle.ActiveRecord.Framework.Config
+{
+       using System;
+       using System.Collections.Generic;
+       using System.Data;
+
+       using NHibernate.ByteCode.Castle;
+       using NHibernate.Connection;
+       using NHibernate.Dialect;
+       using NHibernate.Driver;
+
+       /// <summary>
+       /// Exposes default configuration properties for common databases 
defined in <see cref="DatabaseType"/> enum.
+       /// </summary>
+       public class DefaultDatabaseConfiguration
+       {
+               private const string cache_use_second_level_cache = 
"cache.use_second_level_cache";
+               private const string connection_driver_class = 
"connection.driver_class";
+               private const string connection_isolation = 
"connection.isolation";
+               private const string connection_provider = 
"connection.provider";
+               private const string dialect = "dialect";
+               private const string proxyfactory_factory_class = 
"proxyfactory.factory_class";
+               private const string query_substitutions = 
"query.substitutions";
+
+               private IDictionary<string, string> Configure<TDriver, 
TDialect>()
+                       where TDriver : IDriver
+                       where TDialect : Dialect
+               {
+                       return Configure<TDriver, TDialect>(new 
Dictionary<string, string>());
+               }
+
+               /// <summary>
+               /// Returns dictionary of common properties pre populated with 
default values for given <paramref name="databaseType"/>.
+               /// </summary>
+               /// <param name="databaseType">Database type for which we want 
default properties.</param>
+               /// <returns></returns>
+               public IDictionary<string, string> For(DatabaseType 
databaseType)
+               {
+                       switch (databaseType)
+                       {
+                               case DatabaseType.MsSqlServer2000:
+                                       return Configure<SqlClientDriver, 
MsSql2000Dialect>();
+                               case DatabaseType.MsSqlServer2005:
+                                       return Configure<SqlClientDriver, 
MsSql2005Dialect>();
+                               case DatabaseType.MsSqlServer2008:
+                                       return Configure<SqlClientDriver, 
MsSql2008Dialect>();
+                               case DatabaseType.SQLite:
+                                       return Configure<SQLite20Driver, 
SQLiteDialect>(SQLite());
+                               case DatabaseType.MySql:
+                                       return Configure<MySqlDataDriver, 
MySQLDialect>();
+                               case DatabaseType.MySql5:
+                                       return Configure<MySqlDataDriver, 
MySQL5Dialect>();
+                               case DatabaseType.Firebird:
+                                       return Configure<FirebirdDriver, 
FirebirdDialect>(Firebird());
+                               case DatabaseType.PostgreSQL:
+                                       return Configure<NpgsqlDriver, 
PostgreSQLDialect>();
+                               case DatabaseType.PostgreSQL81:
+                                       return Configure<NpgsqlDriver, 
PostgreSQL81Dialect>();
+                               case DatabaseType.PostgreSQL82:
+                                       return Configure<NpgsqlDriver, 
PostgreSQL82Dialect>();
+                               case DatabaseType.MsSqlCe:
+                                       return Configure<SqlServerCeDriver, 
MsSqlCeDialect>();
+                               // using oracle's own data driver since 
Microsoft
+                               // discontinued theirs, and that's what everyone
+                               // seems to be using anyway.
+                               case DatabaseType.Oracle8i:
+                                       return 
Configure<OracleDataClientDriver, Oracle8iDialect>();
+                               case DatabaseType.Oracle9i:
+                                       return 
Configure<OracleDataClientDriver, Oracle9iDialect>();
+                               case DatabaseType.Oracle10g:
+                                       return 
Configure<OracleDataClientDriver, Oracle10gDialect>();
+                       }
+
+                       throw new ArgumentOutOfRangeException("databaseType", 
databaseType, "Unsupported database type");
+               }
+
+               private IDictionary<string, string> Configure<TDriver, 
TDialect>(Dictionary<string, string> configuration)
+                       where TDriver : IDriver
+                       where TDialect : Dialect
+               {
+                       configuration[connection_provider] = 
LongName<DriverConnectionProvider>();
+                       configuration[cache_use_second_level_cache] = 
false.ToString();
+                       configuration[proxyfactory_factory_class] = 
LongName<ProxyFactoryFactory>();
+                       configuration[dialect] = LongName<TDialect>();
+                       configuration[connection_driver_class] = 
LongName<TDriver>();
+                       return configuration;
+               }
+
+               private string LongName<TType>()
+               {
+                       return typeof(TType).AssemblyQualifiedName;
+               }
+
+               private Dictionary<string, string> SQLite()
+               {
+                       // based on https://www.hibernate.org/361.html#A9
+                       return new Dictionary<string, string>
+                       {
+                               { query_substitutions, "true=1;false=0" }
+                       };
+               }
+
+               private Dictionary<string, string> Firebird()
+               {
+                       // based on https://www.hibernate.org/361.html#A5
+                       return new Dictionary<string, string>
+                       {
+                               { query_substitutions, "true 1, false 0, yes 1, 
no 0" },
+                               { connection_isolation, 
IsolationLevel.ReadCommitted.ToString() },
+                               { "command_timeout", 444.ToString() },
+                               { "use_outer_join", true.ToString() },
+                       };
+               }
+       }

File [added]: DefaultDatabaseConfiguration.cs
Delta lines: +6 -37
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Config/InPlaceConfigurationSource.cs
   2010-01-06 21:39:00 UTC (rev 6590)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Config/InPlaceConfigurationSource.cs
   2010-01-07 04:55:46 UTC (rev 6591)
@@ -18,26 +18,11 @@
        using System.Collections.Generic;
        using System.Configuration;
        using System.Text.RegularExpressions;
+
        using Castle.ActiveRecord.Framework.Scopes;
        using Castle.Core.Configuration;
 
        /// <summary>
-       /// Enum for database types support for configuration construction. 
-       /// Not to be confused by databases supported by ActiveRecord
-       /// </summary>
-       public enum DatabaseType
-       {
-               /// <summary>
-               /// Microsoft SQL Server 2005
-               /// </summary>
-               MSSQLServer2005, 
-               /// <summary>
-               /// Microsoft SQL Server 2000
-               /// </summary>
-               MSSQLServer2000
-       }
-
-       /// <summary>
        /// Usefull for test cases.
        /// </summary>
        public class InPlaceConfigurationSource : IConfigurationSource
@@ -177,7 +162,7 @@
                        if (string.IsNullOrEmpty(server)) throw new 
ArgumentNullException("server");
                        if (string.IsNullOrEmpty(initialCatalog)) throw new 
ArgumentNullException("initialCatalog");
 
-                       return Build(DatabaseType.MSSQLServer2005, "Server=" + 
server + ";initial catalog=" + initialCatalog + ";Integrated Security=SSPI");
+                       return Build(DatabaseType.MsSqlServer2005, "Server=" + 
server + ";initial catalog=" + initialCatalog + ";Integrated Security=SSPI");
                }
 
                /// <summary>
@@ -195,7 +180,7 @@
                        if (string.IsNullOrEmpty(username)) throw new 
ArgumentNullException("username");
                        if (string.IsNullOrEmpty(password)) throw new 
ArgumentNullException("password");
 
-                       return Build(DatabaseType.MSSQLServer2005, "Server=" + 
server + ";initial catalog=" + initialCatalog + ";User id=" + username + 
";password=" + password);
+                       return Build(DatabaseType.MsSqlServer2005, "Server=" + 
server + ";initial catalog=" + initialCatalog + ";User id=" + username + 
";password=" + password);
                }
 
                /// <summary>
@@ -208,26 +193,10 @@
                {
                        if (string.IsNullOrEmpty(connectionString)) throw new 
ArgumentNullException("connectionString");
 
-                       InPlaceConfigurationSource config = new 
InPlaceConfigurationSource();
+                       var config = new InPlaceConfigurationSource();
 
-                       Dictionary<string,string> parameters = new 
Dictionary<string,string>();
-                       parameters["connection.provider"] = 
"NHibernate.Connection.DriverConnectionProvider";
-                       parameters["cache.use_second_level_cache"] = "false";
-                       parameters["proxyfactory.factory_class"] = 
"NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle";
-
-                       if (database == DatabaseType.MSSQLServer2000)
-                       {
-                               parameters["connection.driver_class"] = 
"NHibernate.Driver.SqlClientDriver";
-                               parameters["dialect"] = 
"NHibernate.Dialect.MsSql2000Dialect";
-                               parameters["connection.connection_string"] = 
connectionString;
-                       }
-                       else if (database == DatabaseType.MSSQLServer2005)
-                       {
-                               parameters["connection.driver_class"] = 
"NHibernate.Driver.SqlClientDriver";
-                               parameters["dialect"] = 
"NHibernate.Dialect.MsSql2005Dialect";
-                               parameters["connection.connection_string"] = 
connectionString;
-                       }
-
+                       var parameters = new 
DefaultDatabaseConfiguration().For(database);
+                       parameters["connection.connection_string"] = 
connectionString;
                        config.Add(typeof(ActiveRecordBase), parameters);
 

File [modified]: InPlaceConfigurationSource.cs
Delta lines: +53 -14
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Config/XmlConfigurationSource.cs
       2010-01-06 21:39:00 UTC (rev 6590)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Config/XmlConfigurationSource.cs
       2010-01-07 04:55:46 UTC (rev 6591)
@@ -18,6 +18,7 @@
        using System.Collections.Generic;
        using System.Configuration;
        using System.IO;
+       using System.Text;
        using System.Xml;
 
        /// <summary>
@@ -135,42 +136,80 @@
 
                                Type targetType = typeof(ActiveRecordBase);
 
+                               IDictionary<string, string> defaults = null;
                                if (node.Attributes.Count != 0)
                                {
                                        XmlAttribute typeNameAtt = 
node.Attributes["type"];
 
-                                       if (typeNameAtt == null)
+                                       if (typeNameAtt != null)
                                        {
-                                               String message = 
String.Format("Invalid attribute at node '{0}'. " +
-                                                                              
"The only supported attribute is 'type'", Config_Node_Name);
+                                               String typeName = 
typeNameAtt.Value;
 
-                                               throw new 
ConfigurationErrorsException(message);
-                                       }
+                                               targetType = 
Type.GetType(typeName, false, false);
 
-                                       String typeName = typeNameAtt.Value;
+                                               if (targetType == null)
+                                               {
+                                                       String message = 
String.Format("Could not obtain type from name '{0}'", typeName);
 
-                                       targetType = Type.GetType(typeName, 
false, false);
+                                                       throw new 
ConfigurationErrorsException(message);
+                                               }
+                                       }
 
-                                       if (targetType == null)
+                                       var databaseName = 
node.Attributes["database"] ?? node.Attributes["db"];
+                                       var connectionStringName = 
node.Attributes["connectionStringName"] ?? node.Attributes["csn"];
+                                       if (databaseName != null && 
connectionStringName != null)
                                        {
-                                               String message = 
String.Format("Could not obtain type from name '{0}'", typeName);
-
+                                               defaults = 
SetDefaults(databaseName.Value, connectionStringName.Value);
+                                       }
+                                       else if (databaseName != null || 
connectionStringName != null)
+                                       {
+                                               var message =
+                                                       String.Format(
+                                                               "Using short 
form of configuration requires both 'database' and 'connectionStringName' 
attributes to be specified.");
                                                throw new 
ConfigurationErrorsException(message);
                                        }
                                }
 
-                               Add(targetType, BuildProperties(node));
+                               Add(targetType, BuildProperties(node, 
defaults));
                        }
                }
 
                /// <summary>
+               /// Sets the default configuration for database specifiend by 
<paramref name="name"/>.
+               /// </summary>
+               /// <param name="name">Name of the database type.</param>
+               /// <param name="connectionStringName">name of the connection 
string specified in connectionStrings configuration section</param>
+               /// <returns></returns>
+               protected IDictionary<string, string> SetDefaults(string name, 
string connectionStringName)
+               {
+                       var names = Enum.GetNames(typeof(DatabaseType));
+                       if (!Array.Exists(names, n => n.Equals(name, 
StringComparison.OrdinalIgnoreCase)))
+                       {
+                               var builder = new StringBuilder("Specified 
value ({0}) is not valid for 'database' attribute. Valid values are:");
+                               foreach (var value in 
Enum.GetValues(typeof(DatabaseType)))
+                               {
+                                       builder.AppendFormat("'{0}' ", 
value.ToString());
+                               }
+
+                               builder.Append(".");
+                               throw new 
ConfigurationErrorsException(builder.ToString());
+                       }
+
+                       var type = 
(DatabaseType)Enum.Parse(typeof(DatabaseType), name, true);
+                       var defaults = new 
DefaultDatabaseConfiguration().For(type);
+                       defaults["connection.connection_string_name"] = 
connectionStringName;
+                       return defaults;
+               }
+
+               /// <summary>
                /// Builds the configuration properties.
                /// </summary>
                /// <param name="node">The node.</param>
+               /// <param name="defaults"></param>
                /// <returns></returns>
-               protected IDictionary<string,string> BuildProperties(XmlNode 
node)
+               protected IDictionary<string, string> BuildProperties(XmlNode 
node, IDictionary<string, string> defaults)
                {
-                       Dictionary<string,string> dict = new 
Dictionary<string,string>();
+                       var dict = defaults ?? new Dictionary<string, string>();
 
                        foreach(XmlNode addNode in node.SelectNodes("add"))
                        {
@@ -185,7 +224,7 @@
                                }
                                string value = valueAtt.Value;
 
-                               dict.Add(keyAtt.Value, value);
+                               dict[keyAtt.Value] = value;
                        }
 

File [modified]: XmlConfigurationSource.cs
Delta lines: +1 -1
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/AbstractActiveRecordTest.cs    
    2010-01-06 21:39:00 UTC (rev 6590)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/AbstractActiveRecordTest.cs    
    2010-01-07 04:55:46 UTC (rev 6591)
@@ -25,7 +25,7 @@
        {
                protected static IConfigurationSource GetConfigSource()
                {
-            return 
System.Configuration.ConfigurationManager.GetSection("activerecord") as 
IConfigurationSource;
+                       return 
System.Configuration.ConfigurationManager.GetSection("activerecord") as 
IConfigurationSource;
                }
 

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/
=================================================================

File [modified]: ActiveRecordStarter.cs
Delta lines: +79 -0
===================================================================

--- ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Config/DatabaseType.cs 
                        (rev 0)
+++ ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Config/DatabaseType.cs 
2010-01-07 04:55:46 UTC (rev 6591)
@@ -0,0 +1,80 @@
+// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
+// 
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// 
+//     http://www.apache.org/licenses/LICENSE-2.0
+// 
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+namespace Castle.ActiveRecord.Framework.Config
+{
+       /// <summary>
+       /// Enum for database types support for configuration construction. 
+       /// Not to be confused by databases supported by ActiveRecord
+       /// </summary>
+       public enum DatabaseType
+       {
+               /// <summary>
+               /// Microsoft SQL Server 2000
+               /// </summary>
+               MsSqlServer2000,
+               /// <summary>
+               /// Microsoft SQL Server 2005
+               /// </summary>
+               MsSqlServer2005,
+               /// <summary>
+               /// Microsoft SQL Server 2008
+               /// </summary>
+               MsSqlServer2008,
+               /// <summary>
+               /// SQLite
+               /// </summary>
+               SQLite,
+               /// <summary>
+               /// MySQL 3 or 4
+               /// </summary>
+               MySql,
+               /// <summary>
+               /// MySQL 5
+               /// </summary>
+               MySql5,
+               /// <summary>
+               /// Firebird
+               /// </summary>
+               Firebird,
+               /// <summary>
+               /// PostgreSQL
+               /// </summary>
+               PostgreSQL,
+               /// <summary>
+               /// PostgreSQL 8.1
+               /// </summary>
+               PostgreSQL81,
+               /// <summary>
+               /// PostgreSQL 8.2
+               /// </summary>
+               PostgreSQL82,
+               /// <summary>
+               /// Microsoft SQL Server 2005 Compact Edition
+               /// </summary>
+               MsSqlCe,
+               /// <summary>
+               /// Oracle 
+               /// </summary>
+               Oracle8i,
+               /// <summary>
+               /// Oracle 9
+               /// </summary>
+               Oracle9i,
+               /// <summary>
+               /// Oracle 10g
+               /// </summary>
+               Oracle10g
+       }

Directory: /ActiveRecord/trunk/src/
===================================

File [modified]: Changes.txt
Delta lines: +0 -0
===================================================================

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-commits?hl=en.


Reply via email to