Author: gbayon
Date: Sun Mar 12 08:43:32 2006
New Revision: 385313

URL: http://svn.apache.org/viewcvs?rev=385313&view=rev
Log:
- Improved object reflection

Added:
    ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/Simple.cs
    
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/ObjectFactoryTest.cs
    
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ActivatorFactory.cs
    
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/DefaultObjectFactory.cs
    ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/EmitFactory.cs
    
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/EmitObjectFactory.cs
    ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IFactory.cs
    ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IObjectFactory.cs
    ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ObjectFactory.cs
Modified:
    ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/Account.cs
    ibatis/trunk/cs/mapper/IBatisNet.Common.Test/IBatisNet.Common.Test.csproj
    
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/PropertyAccessorTest.cs
    ibatis/trunk/cs/mapper/IBatisNet.Common/IBatisNet.Common.csproj
    
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ILPropertyAccessor.cs
    
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IPropertyAccessor.cs

Modified: ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/Account.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/Account.cs?rev=385313&r1=385312&r2=385313&view=diff
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/Account.cs (original)
+++ ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/Account.cs Sun Mar 12 
08:43:32 2006
@@ -5,6 +5,7 @@
        /// <summary>
        /// Description résumée de Account.
        /// </summary>
+       [Serializable]
        public class Account
        {
                private int _id = 0;

Added: ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/Simple.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/Simple.cs?rev=385313&view=auto
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/Simple.cs (added)
+++ ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/Simple.cs Sun Mar 12 
08:43:32 2006
@@ -0,0 +1,62 @@
+using System;
+
+namespace IBatisNet.Common.Test.Domain
+{
+       [Serializable]
+       public class Simple
+       {
+               private string _name = string.Empty;
+               private string _address = string.Empty;
+               private int _count = int.MinValue;
+               private DateTime _date = new DateTime(2006, 03, 06, 12, 00, 00, 
00);
+               private decimal _pay = 78.78M;
+               private int _id = 0;
+
+
+               public Simple() { }
+
+               public void Init()
+               {
+                       _name = "Someone with along name";
+                       _address = "1234 some street, some city, victoria, 
3000, austaya";
+                       _count = 69;
+                       _date = DateTime.Now;
+               }
+
+               public int Id
+               {
+                       get { return _id; }
+                       set { _id = value; }
+               }
+
+               public string Name
+               {
+                       get { return _name; }
+                       set { _name = value; }
+               }
+
+               public string Address
+               {
+                       get { return _address; }
+                       set { _address = value; }
+               }
+
+               public int Count
+               {
+                       get { return _count; }
+                       set { _count = value; }
+               }
+
+               public DateTime Date
+               {
+                       get { return _date; }
+                       set { _date = value; }
+               }
+
+               public decimal Pay
+               {
+                       get { return _pay; }
+                       set { _pay = value; }
+               }
+       }
+}

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/IBatisNet.Common.Test.csproj
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common.Test/IBatisNet.Common.Test.csproj?rev=385313&r1=385312&r2=385313&view=diff
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.Common.Test/IBatisNet.Common.Test.csproj 
(original)
+++ ibatis/trunk/cs/mapper/IBatisNet.Common.Test/IBatisNet.Common.Test.csproj 
Sun Mar 12 08:43:32 2006
@@ -212,6 +212,11 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = "Domain\Simple.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = "Maps\Access\OleDb\Account.xml"
                     BuildAction = "Content"
                 />
@@ -281,6 +286,11 @@
                 />
                 <File
                     RelPath = 
"NUnit\CommonTests\Transaction\TransactionTest.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
+                    RelPath = 
"NUnit\CommonTests\Utilities\ObjectFactoryTest.cs"
                     SubType = "Code"
                     BuildAction = "Compile"
                 />

Added: 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/ObjectFactoryTest.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/ObjectFactoryTest.cs?rev=385313&view=auto
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/ObjectFactoryTest.cs
 (added)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/ObjectFactoryTest.cs
 Sun Mar 12 08:43:32 2006
@@ -0,0 +1,154 @@
+using System.ComponentModel;
+using System.Runtime.InteropServices;
+using System.Threading;
+using IBatisNet.Common.Test.Domain;
+using IBatisNet.Common.Utilities.Objects;
+using NUnit.Framework;
+using System;
+
+namespace IBatisNet.Common.Test.NUnit.CommonTests.Utilities
+{
+
+    [TestFixture] 
+       public class ObjectFactoryTest
+       {
+
+               [Test]
+               public void DynamicFactoryCreatesTypes()
+               {
+                       IObjectFactory objectFactory = new ObjectFactory(true);
+
+                       IFactory factory = objectFactory.CreateFactory(typeof 
(Account));
+                       object obj = factory.CreateInstance();
+                       Assert.IsTrue(obj is Account);
+
+                       factory = objectFactory.CreateFactory(typeof (Account));
+                       obj = factory.CreateInstance();
+                       Assert.IsTrue(obj is Account);
+
+                       factory = objectFactory.CreateFactory(typeof (Simple));
+                       obj = factory.CreateInstance();
+                       Assert.IsTrue(obj is Simple);
+               }
+
+               [Test]
+               public void CreateInstanceWithDifferentFactories()
+               {
+                       const int TEST_ITERATIONS = 1000000;
+                       IFactory factory = null;
+
+                       #region new
+                       factory = new NewAccountFactory();
+
+                       // create an instance so that Activators can
+                       // cache the type/constructor/whatever
+                       factory.CreateInstance();
+
+                       GC.Collect();
+                       GC.WaitForPendingFinalizers();
+
+                       HiPerformanceTimer timer = new HiPerformanceTimer();
+                       timer.Start();
+                       for (int i = 0; i < TEST_ITERATIONS; i++)
+                       {
+                               factory.CreateInstance();
+                       }
+                       timer.Stop();
+                       double newFactoryResult = 1000000 * (timer.Duration / 
(double)TEST_ITERATIONS);
+                       #endregion
+
+                       #region activator
+                       factory = new 
ActivatorObjectFactory().CreateFactory(typeof(Account));
+
+                       // create an instance so that Activators can
+                       // cache the type/constructor/whatever
+                       factory.CreateInstance();
+
+                       GC.Collect();
+                       GC.WaitForPendingFinalizers();
+
+                       timer.Start();
+                       for (int i = 0; i < TEST_ITERATIONS; i++)
+                       {
+                               factory.CreateInstance();
+                       }
+                       timer.Stop();
+                       double activatorFactoryResult = 1000000 * 
(timer.Duration / (double)TEST_ITERATIONS);
+                       #endregion
+
+                       #region Emit
+                       factory = new 
EmitObjectFactory().CreateFactory(typeof(Account));
+
+                       // create an instance so that Activators can
+                       // cache the type/constructor/whatever
+                       factory.CreateInstance();
+
+                       GC.Collect();
+                       GC.WaitForPendingFinalizers();
+
+                       timer.Start();
+                       for (int i = 0; i < TEST_ITERATIONS; i++)
+                       {
+                               factory.CreateInstance();
+                       }
+                       timer.Stop();
+                       double emitFactoryResult = 1000000 * (timer.Duration / 
(double)TEST_ITERATIONS);
+                       #endregion
+
+                       // Print results
+                       Console.WriteLine(
+                               "Create " + TEST_ITERATIONS.ToString() + " 
objects via factory :"
+                               + "\nNew : \t\t\t" + 
newFactoryResult.ToString("F3")
+                               + "\nActivator : \t\t" + 
activatorFactoryResult.ToString("F3")+ " Ratio : " + ((activatorFactoryResult / 
newFactoryResult)).ToString("F3")
+                               + "\nEmit IL : \t\t\t" + 
emitFactoryResult.ToString("F3") + " Ratio : " + ((emitFactoryResult / 
newFactoryResult)).ToString("F3"));
+
+               }
+
+               internal class NewAccountFactory : IFactory
+               {
+                       public object CreateInstance()
+                       {
+                               return new Account();
+                       }
+               }
+
+               internal class HiPerformanceTimer
+               {
+                       [DllImport("Kernel32.dll")]
+                       private static extern bool QueryPerformanceCounter(out 
long lpPerformanceCount);
+
+                       [DllImport("Kernel32.dll")]
+                       private static extern bool 
QueryPerformanceFrequency(out long lpFrequency);
+
+                       private long startTime, stopTime;
+                       private long freq;
+
+                       public HiPerformanceTimer()
+                       {
+                               startTime = 0;
+                               stopTime = 0;
+
+                               if (QueryPerformanceFrequency(out freq) == 
false)
+                               {
+                                       throw new Win32Exception();
+                               }
+                       }
+
+                       public void Start()
+                       {
+                               Thread.Sleep(0);
+                               QueryPerformanceCounter(out startTime);
+                       }
+
+                       public void Stop()
+                       {
+                               QueryPerformanceCounter(out stopTime);
+                       }
+
+                       public double Duration
+                       {
+                               get { return (double) (stopTime - 
startTime)/(double) freq; }
+                       }
+               }
+       }
+}

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/PropertyAccessorTest.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/PropertyAccessorTest.cs?rev=385313&r1=385312&r2=385313&view=diff
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/PropertyAccessorTest.cs
 (original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/PropertyAccessorTest.cs
 Sun Mar 12 08:43:32 2006
@@ -1,10 +1,7 @@
 using System;
-using System.Text;
 using System.Reflection;
-
-using IBatisNet.Common.Utilities.Objects;
 using IBatisNet.Common.Test.Domain;
-
+using IBatisNet.Common.Utilities.Objects;
 using NUnit.Framework;
 
 namespace IBatisNet.Common.Test.NUnit.CommonTests.Utilities
@@ -40,12 +37,13 @@
         public void TestGetIntegerPerformance()
         {
             const int TEST_ITERATIONS = 1000000;
-            Account account = new Account();
+               Account account = new Account();
             int test = -1;
 
             // IL Property accessor
-            IPropertyAccessor propertyAccessor = 
ILPropertyAccessor.CreatePropertyAccessor(typeof(Account), "Id");
-            long time = DateTime.Now.Ticks;
+               IPropertyAccessor propertyAccessor = 
ILPropertyAccessor.CreatePropertyAccessor(typeof(Account), "Id");
+
+                       long time = DateTime.Now.Ticks;
             for (int i = 0; i < TEST_ITERATIONS; i++)
             {
                 test = -1;
@@ -71,7 +69,7 @@
             {
                 test = -1;
                 test = (int)type.InvokeMember("Id",
-                    BindingFlags.Public | BindingFlags.GetProperty | 
BindingFlags.Instance,
+                                              BindingFlags.Public | 
BindingFlags.GetProperty | BindingFlags.Instance,
                     null, account, null);
                 Assert.AreEqual(0, test);
             }
@@ -85,7 +83,8 @@
                 + "\nReflection : \t\t\t" + reflectionMs.ToString() + " ms 
Ratio: " + (((float)reflectionMs / directAccessMs)).ToString());
         }
 
-        /// <summary>
+        
+               /// <summary>
         /// Test the performance of getting an integer property.
         /// </summary>
         [Test]
@@ -131,7 +130,8 @@
                 + "\nReflection : \t\t\t" +  reflectionMs +" ms Ratio: " + 
(((float)reflectionMs / directAccessMs)).ToString());
         }
 
-        /// <summary>
+       
+               /// <summary>
         /// Test the performance of getting an integer property.
         /// </summary>
         [Test]

Modified: ibatis/trunk/cs/mapper/IBatisNet.Common/IBatisNet.Common.csproj
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/IBatisNet.Common.csproj?rev=385313&r1=385312&r2=385313&view=diff
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.Common/IBatisNet.Common.csproj (original)
+++ ibatis/trunk/cs/mapper/IBatisNet.Common/IBatisNet.Common.csproj Sun Mar 12 
08:43:32 2006
@@ -300,12 +300,47 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = "Utilities\Objects\ActivatorFactory.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
+                    RelPath = "Utilities\Objects\DefaultObjectFactory.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
+                    RelPath = "Utilities\Objects\EmitFactory.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
+                    RelPath = "Utilities\Objects\EmitObjectFactory.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
+                    RelPath = "Utilities\Objects\IFactory.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = "Utilities\Objects\ILPropertyAccessor.cs"
                     SubType = "Code"
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = "Utilities\Objects\IObjectFactory.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = "Utilities\Objects\IPropertyAccessor.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
+                    RelPath = "Utilities\Objects\ObjectFactory.cs"
                     SubType = "Code"
                     BuildAction = "Compile"
                 />

Added: 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ActivatorFactory.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ActivatorFactory.cs?rev=385313&view=auto
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ActivatorFactory.cs 
(added)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ActivatorFactory.cs 
Sun Mar 12 08:43:32 2006
@@ -0,0 +1,34 @@
+using System;
+
+namespace IBatisNet.Common.Utilities.Objects
+{
+       /// <summary>
+       /// Create objects via Activator.CreateInstance
+       /// </summary>
+       public class ActivatorFactory : IFactory
+       {
+               private Type _typeToCreate = null;
+
+               /// <summary>
+               /// 
+               /// </summary>
+               /// <param name="typeToCreate"></param>
+               public ActivatorFactory(Type typeToCreate)
+               {
+                       _typeToCreate = typeToCreate;
+               }
+
+               #region IFactory members
+
+               /// <summary>
+               /// Create a new object instance via via 
Activator.CreateInstance
+               /// </summary>
+               /// <returns></returns>
+               public object CreateInstance()
+               {
+                       return Activator.CreateInstance( _typeToCreate );
+               }
+
+               #endregion
+       }
+}

Added: 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/DefaultObjectFactory.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/DefaultObjectFactory.cs?rev=385313&view=auto
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/DefaultObjectFactory.cs
 (added)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/DefaultObjectFactory.cs
 Sun Mar 12 08:43:32 2006
@@ -0,0 +1,26 @@
+using System;
+
+namespace IBatisNet.Common.Utilities.Objects
+{
+       /// <summary>
+       /// A factory that can create objects 
+       /// via Activator.CreateInstance
+       /// </summary>
+       public class ActivatorObjectFactory : IObjectFactory
+       {
+
+               #region IObjectFactory members
+
+               /// <summary>
+               /// Create a new factory instance for a given type
+               /// </summary>
+               /// <param name="typeToCreate"></param>
+               /// <returns></returns>
+               public IFactory CreateFactory(Type typeToCreate)
+               {
+                       return new ActivatorFactory( typeToCreate );
+               }
+
+               #endregion
+       }
+}

Added: ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/EmitFactory.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/EmitFactory.cs?rev=385313&view=auto
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/EmitFactory.cs 
(added)
+++ ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/EmitFactory.cs 
Sun Mar 12 08:43:32 2006
@@ -0,0 +1,98 @@
+#region Apache Notice
+/*****************************************************************************
+ * $Revision: 374175 $
+ * $LastChangedDate: 2006-02-19 12:37:22 +0100 (Sun, 19 Feb 2006) $
+ * $LastChangedBy: gbayon $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2006/2005 - The Apache Software Foundation
+ *  
+ * 
+ * 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.
+ * 
+ 
********************************************************************************/
+#endregion
+
+using System;
+using System.Reflection;
+using System.Reflection.Emit;
+using IBatisNet.Common.Exceptions;
+
+namespace IBatisNet.Common.Utilities.Objects
+{
+       /// <summary>
+       /// Build IFactory object via IL 
+       /// </summary>
+       public class FactoryBuilder
+       {
+               private ModuleBuilder _moduleBuilder = null;
+
+               /// <summary>
+               /// constructor
+               /// </summary>
+               public FactoryBuilder()
+               {
+                       AssemblyName assemblyName = new AssemblyName();
+                       assemblyName.Name = "iBATIS.EmitFactory";
+
+                       // Create a new assembly with one module
+                       AssemblyBuilder _assemblyBuilder = 
AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, 
AssemblyBuilderAccess.Run);
+                       _moduleBuilder = 
_assemblyBuilder.DefineDynamicModule(assemblyName.Name + ".dll");
+               }
+
+
+               /// <summary>
+               /// 
+               /// </summary>
+               /// <param name="typeToCreate"></param>
+               /// <returns></returns>
+               public IFactory CreateFactory(Type typeToCreate)
+               {
+                       Type innerType = CreateFactoryType(typeToCreate);
+                       ConstructorInfo ctor = innerType.GetConstructor(new 
Type[] {});
+                       return (IFactory) ctor.Invoke(new object[] {});
+               }
+
+
+               /// <summary>
+               /// 
+               /// </summary>
+               /// <param name="typeToCreate"></param>
+               /// <returns></returns>
+               private Type CreateFactoryType(Type typeToCreate)
+               {
+                       TypeBuilder typeBuilder = 
_moduleBuilder.DefineType("EmitFactoryFor" + typeToCreate.Name, 
TypeAttributes.Public);
+                       typeBuilder.AddInterfaceImplementation(typeof 
(IFactory));
+                       ImplementCreateInstance(typeBuilder, typeToCreate);
+                       return typeBuilder.CreateType();
+               }
+
+               
+               private MethodAttributes createMethodAttributes = 
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot 
| MethodAttributes.Virtual | MethodAttributes.Final;
+
+               private void ImplementCreateInstance(TypeBuilder typeBuilder, 
Type typeToCreate)
+               {
+                       MethodBuilder meth = 
typeBuilder.DefineMethod("CreateInstance", createMethodAttributes, typeof 
(object), Type.EmptyTypes);
+                       ILGenerator il = meth.GetILGenerator();
+
+                       // Add test if contructeur not public
+                       ConstructorInfo ctor = 
typeToCreate.GetConstructor(Type.EmptyTypes);
+                       if (!ctor.IsPublic)
+                       {
+                               throw new ProbeException(string.Format("Unable 
to optimize create instance for type \"{0}\". Cause no public constructor on 
type. ", typeToCreate.Name));
+                       }
+                       il.Emit(OpCodes.Newobj, ctor);
+                       il.Emit(OpCodes.Ret);
+               }
+       }
+}

Added: 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/EmitObjectFactory.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/EmitObjectFactory.cs?rev=385313&view=auto
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/EmitObjectFactory.cs 
(added)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/EmitObjectFactory.cs 
Sun Mar 12 08:43:32 2006
@@ -0,0 +1,68 @@
+#region Apache Notice
+/*****************************************************************************
+ * $Revision: 374175 $
+ * $LastChangedDate: 2006-02-19 12:37:22 +0100 (Sun, 19 Feb 2006) $
+ * $LastChangedBy: gbayon $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2006/2005 - The Apache Software Foundation
+ *  
+ * 
+ * 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.
+ * 
+ 
********************************************************************************/
+#endregion
+
+using System;
+using System.Collections;
+using System.Collections.Specialized;
+
+namespace IBatisNet.Common.Utilities.Objects
+{
+       /// <summary>
+       /// A factory that can create objects via IL code
+       /// </summary>
+       public class EmitObjectFactory : IObjectFactory
+       {
+               private IDictionary _cachedfactories = new HybridDictionary();
+               private FactoryBuilder _factoryBuilder = null;
+
+               /// <summary>
+               /// 
+               /// </summary>
+               public EmitObjectFactory()
+               {
+                       _factoryBuilder = new FactoryBuilder();
+               }
+
+               #region IObjectFactory members
+
+               /// <summary>
+               /// Create a new factory instance for a given type
+               /// </summary>
+               /// <param name="typeToCreate"></param>
+               /// <returns></returns>
+               public IFactory CreateFactory(Type typeToCreate)
+               {
+                       IFactory factory = (IFactory) 
_cachedfactories[typeToCreate];
+                       if (null == factory)
+                       {
+                               factory = 
_factoryBuilder.CreateFactory(typeToCreate);
+                               _cachedfactories[typeToCreate] = factory;
+                       }
+                       return factory;
+               }
+
+               #endregion
+       }
+}

Added: ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IFactory.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IFactory.cs?rev=385313&view=auto
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IFactory.cs 
(added)
+++ ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IFactory.cs Sun 
Mar 12 08:43:32 2006
@@ -0,0 +1,39 @@
+
+#region Apache Notice
+/*****************************************************************************
+ * $Revision: 374175 $
+ * $LastChangedDate: 2006-02-19 12:37:22 +0100 (Sun, 19 Feb 2006) $
+ * $LastChangedBy: gbayon $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2006/2005 - The Apache Software Foundation
+ *  
+ * 
+ * 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.
+ * 
+ 
********************************************************************************/
+#endregion
+
+namespace IBatisNet.Common.Utilities.Objects
+{
+       /// <summary>
+       /// Factory to create object for a given type
+       /// </summary>
+       public interface IFactory
+       {
+               /// <summary>
+               /// Create a new instance
+               /// </summary>
+               object CreateInstance();
+       }
+}

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ILPropertyAccessor.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ILPropertyAccessor.cs?rev=385313&r1=385312&r2=385313&view=diff
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ILPropertyAccessor.cs 
(original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ILPropertyAccessor.cs 
Sun Mar 12 08:43:32 2006
@@ -1,11 +1,11 @@
 #region Apache Notice
 /*****************************************************************************
- * $Header: $
- * $Revision: 383310 $
- * $Date$
+ * $Revision: 374175 $
+ * $LastChangedDate$
+ * $LastChangedBy$
  * 
  * iBATIS.NET Data Mapper
- * Copyright (C) 2004 - Gilles Bayon
+ * Copyright (C) 2006/2005 - The Apache Software Foundation
  *  
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -24,11 +24,10 @@
 #endregion
 
 using System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
 using System.Collections;
 using System.Collections.Specialized;
+using System.Reflection;
+using System.Reflection.Emit;
 using IBatisNet.Common.Exceptions;
 
 namespace IBatisNet.Common.Utilities.Objects
@@ -83,7 +82,7 @@
             _typeToOpcode[typeof(double)] = OpCodes.Ldind_R8;
             _typeToOpcode[typeof(float)] = OpCodes.Ldind_R4;
 
-            AssemblyName assemblyName = new AssemblyName();
+               AssemblyName assemblyName = new AssemblyName();
             assemblyName.Name = "iBATIS.FastPropertyAccessor";
 
             // Create a new assembly with one module
@@ -91,7 +90,8 @@
             _moduleBuilder = 
_assemblyBuilder.DefineDynamicModule(assemblyName.Name + ".dll");
         }
 
-        /// <summary>
+        
+               /// <summary>
         /// Generate the IPropertyAccessor object
         /// </summary>
         /// <param name="targetType">Target object type.</param>
@@ -115,6 +115,7 @@
             }
         }
 
+               
                /// <summary>
                /// Creates a new IL property accessor.
                /// </summary>
@@ -127,9 +128,7 @@
 
             PropertyInfo propertyInfo = targetType.GetProperty(propertyName);
 
-                       //
                        // Make sure the property exists
-                       //
                        if(propertyInfo == null)
                        {
                                throw new ProbeException(
@@ -220,7 +219,7 @@
                        
typeBuilder.AddInterfaceImplementation(typeof(IPropertyAccessor));
 
                        // Add a constructor
-                       ConstructorBuilder constructorBuilder = 
typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
+                       
typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
 
                        // Define a method for the get operation. 
                        Type[] getParamTypes = new Type[] {typeof(object)};
@@ -239,14 +238,10 @@
 
                        if(targetGetMethod != null)
                        {
-                               
                                getIL.DeclareLocal(typeof(object));
-                               getIL.Emit(OpCodes.Ldarg_1);                    
                                        //Load the first argument 
-                                                                               
                                                                        
//(target object)
-
-                               getIL.Emit(OpCodes.Castclass, 
this._targetType);                        //Cast to the source type
-
-                               getIL.EmitCall(OpCodes.Call, targetGetMethod, 
null);            //Get the property value
+                               getIL.Emit(OpCodes.Ldarg_1);    //Load the 
first argument,(target object)
+                               getIL.Emit(OpCodes.Castclass, 
this._targetType);        //Cast to the source type
+                               getIL.EmitCall(OpCodes.Call, targetGetMethod, 
null);    //Get the property value
 
                                if(targetGetMethod.ReturnType.IsValueType)
                                {

Added: 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IObjectFactory.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IObjectFactory.cs?rev=385313&view=auto
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IObjectFactory.cs 
(added)
+++ ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IObjectFactory.cs 
Sun Mar 12 08:43:32 2006
@@ -0,0 +1,43 @@
+
+#region Apache Notice
+/*****************************************************************************
+ * $Revision: 374175 $
+ * $LastChangedDate: 2006-02-19 12:37:22 +0100 (Sun, 19 Feb 2006) $
+ * $LastChangedBy: gbayon $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2006/2005 - The Apache Software Foundation
+ *  
+ * 
+ * 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.
+ * 
+ 
********************************************************************************/
+#endregion
+
+using System;
+
+namespace IBatisNet.Common.Utilities.Objects
+{
+       /// <summary>
+       /// A factory that can create objects 
+       /// </summary>
+       public interface IObjectFactory
+       {
+               /// <summary>
+               /// Create a new factory instance for a given type
+               /// </summary>
+               /// <param name="typeToCreate"></param>
+               /// <returns></returns>
+               IFactory CreateFactory(Type typeToCreate);
+       }
+}

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IPropertyAccessor.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IPropertyAccessor.cs?rev=385313&r1=385312&r2=385313&view=diff
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IPropertyAccessor.cs 
(original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/IPropertyAccessor.cs 
Sun Mar 12 08:43:32 2006
@@ -1,11 +1,11 @@
 #region Apache Notice
 /*****************************************************************************
- * $Header: $
- * $Revision: 378715 $
- * $Date$
+ * $Revision: 374175 $
+ * $LastChangedDate$
+ * $LastChangedBy$
  * 
  * iBATIS.NET Data Mapper
- * Copyright (C) 2004 - Gilles Bayon
+ * Copyright (C) 2006/2005 - The Apache Software Foundation
  *  
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");

Added: 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ObjectFactory.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ObjectFactory.cs?rev=385313&view=auto
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ObjectFactory.cs 
(added)
+++ ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Objects/ObjectFactory.cs 
Sun Mar 12 08:43:32 2006
@@ -0,0 +1,78 @@
+#region Apache Notice
+/*****************************************************************************
+ * $Revision: 374175 $
+ * $LastChangedDate: 2006-02-19 12:37:22 +0100 (Sun, 19 Feb 2006) $
+ * $LastChangedBy: gbayon $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2006/2005 - The Apache Software Foundation
+ *  
+ * 
+ * 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.
+ * 
+ 
********************************************************************************/
+#endregion
+
+using System;
+
+namespace IBatisNet.Common.Utilities.Objects
+{
+       /// <summary>
+       /// Description résumée de ObjectFactory.
+       /// </summary>
+       public class ObjectFactory : IObjectFactory
+       {
+               private IObjectFactory _objectFactory = null;
+
+               /// <summary>
+               /// Constructor
+               /// </summary>
+               /// <param name="allowCodeGeneration"></param>
+               public ObjectFactory(bool allowCodeGeneration)
+               {
+                       if (allowCodeGeneration)
+                       {
+                               // Detect runtime environment and create the 
appropriate factory
+                               if (Environment.Version.Major >= 2)
+                               {
+                                       // To Do : a custom factory for .NET V2
+                                       // optimize with DynamicMethod or 
Delegate.CreateDelegate
+                                       _objectFactory = new 
EmitObjectFactory();
+                                       
+                               }
+                               else
+                               {
+                                       _objectFactory = new 
EmitObjectFactory();
+                               }
+                       }
+                       else
+                       {
+                               _objectFactory = new ActivatorObjectFactory();
+                       }
+               }
+
+               #region IObjectFactory members
+
+               /// <summary>
+               /// Create a new factory instance for a given type
+               /// </summary>
+               /// <param name="typeToCreate"></param>
+               /// <returns></returns>
+               public IFactory CreateFactory(Type typeToCreate)
+               {
+                       return _objectFactory.CreateFactory(typeToCreate);
+               }
+
+               #endregion
+       }
+}


Reply via email to