From: <dav...@us...> - 2009-04-14 18:12:01
|
Revision: 4179 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4179&view=rev Author: davybrion Date: 2009-04-14 18:11:54 +0000 (Tue, 14 Apr 2009) Log Message: ----------- fix for NH-1728 Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Properties/BasicPropertyAccessor.cs trunk/nhibernate/src/NHibernate/Transform/AliasToBeanResultTransformer.cs trunk/nhibernate/src/NHibernate/Transform/Transformers.cs trunk/nhibernate/src/NHibernate.Test/TransformTests/AliasToBeanResultTransformerFixture.cs Modified: trunk/nhibernate/src/NHibernate/Properties/BasicPropertyAccessor.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Properties/BasicPropertyAccessor.cs 2009-04-13 22:21:54 UTC (rev 4178) +++ trunk/nhibernate/src/NHibernate/Properties/BasicPropertyAccessor.cs 2009-04-14 18:11:54 UTC (rev 4179) @@ -130,31 +130,32 @@ return null; } + // the BindingFlags.IgnoreCase is important here because if type is a struct, the GetProperty method does + // not ignore case by default. If type is a class, it _does_ ignore case... we're better off explicitly + // stating that casing should be ignored so we get the same behavior for both structs and classes PropertyInfo property = type.GetProperty(propertyName, - BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); + BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.IgnoreCase); if (property != null && property.CanWrite) { return new BasicSetter(type, property, propertyName); } - else + + // recursively call this method for the base Type + BasicSetter setter = GetSetterOrNull(type.BaseType, propertyName); + + // didn't find anything in the base class - check to see if there is + // an explicit interface implementation. + if (setter == null) { - // recursively call this method for the base Type - BasicSetter setter = GetSetterOrNull(type.BaseType, propertyName); - - // didn't find anything in the base class - check to see if there is - // an explicit interface implementation. - if (setter == null) + System.Type[] interfaces = type.GetInterfaces(); + for (int i = 0; setter == null && i < interfaces.Length; i++) { - System.Type[] interfaces = type.GetInterfaces(); - for (int i = 0; setter == null && i < interfaces.Length; i++) - { - setter = GetSetterOrNull(interfaces[i], propertyName); - } + setter = GetSetterOrNull(interfaces[i], propertyName); } - return setter; } + return setter; } /// <summary> Modified: trunk/nhibernate/src/NHibernate/Transform/AliasToBeanResultTransformer.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Transform/AliasToBeanResultTransformer.cs 2009-04-13 22:21:54 UTC (rev 4178) +++ trunk/nhibernate/src/NHibernate/Transform/AliasToBeanResultTransformer.cs 2009-04-14 18:11:54 UTC (rev 4179) @@ -40,8 +40,12 @@ throw new ArgumentNullException("resultClass"); } this.resultClass = resultClass; + constructor = resultClass.GetConstructor(flags, null, System.Type.EmptyTypes, null); - if (constructor == null) + + // if resultClass is a ValueType (struct), GetConstructor will return null... + // in that case, we'll use Activator.CreateInstance instead of the ConstructorInfo to create instances + if (constructor == null && resultClass.IsClass) { throw new ArgumentException("The target class of a AliasToBeanResultTransformer need a parameter-less constructor", "resultClass"); @@ -73,7 +77,9 @@ } } } - result = constructor.Invoke(null); + + // if resultClass is not a class but a value type, we need to use Activator.CreateInstance + result = resultClass.IsClass ? constructor.Invoke(null) : Activator.CreateInstance(resultClass, true); for (int i = 0; i < aliases.Length; i++) { Modified: trunk/nhibernate/src/NHibernate/Transform/Transformers.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Transform/Transformers.cs 2009-04-13 22:21:54 UTC (rev 4178) +++ trunk/nhibernate/src/NHibernate/Transform/Transformers.cs 2009-04-14 18:11:54 UTC (rev 4179) @@ -22,9 +22,9 @@ return new AliasToBeanResultTransformer(target); } - public static IResultTransformer AliasToBean<T>() where T: class + public static IResultTransformer AliasToBean<T>() { - return AliasToBean(typeof (T)); + return AliasToBean(typeof(T)); } public static readonly IResultTransformer DistinctRootEntity = new DistinctRootEntityResultTransformer(); Modified: trunk/nhibernate/src/NHibernate.Test/TransformTests/AliasToBeanResultTransformerFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/TransformTests/AliasToBeanResultTransformerFixture.cs 2009-04-13 22:21:54 UTC (rev 4178) +++ trunk/nhibernate/src/NHibernate.Test/TransformTests/AliasToBeanResultTransformerFixture.cs 2009-04-14 18:11:54 UTC (rev 4179) @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using NHibernate.Transform; @@ -36,6 +37,11 @@ } } + public struct TestStruct + { + public string Something { get; set; } + } + #region Overrides of TestCase protected override IList Mappings @@ -53,34 +59,66 @@ [Test] public void WorkWithOutPublicParameterLessCtor() { - Setup(); + try + { + Setup(); - using (ISession s = OpenSession()) + using (ISession s = OpenSession()) + { + IList<WithOutPublicParameterLessCtor> l = + s.CreateSQLQuery("select s.Name as something from Simple s").SetResultTransformer( + Transformers.AliasToBean<WithOutPublicParameterLessCtor>()).List<WithOutPublicParameterLessCtor>(); + Assert.That(l.Count, Is.EqualTo(2)); + Assert.That(l, Has.All.Not.Null); + } + } + finally { - IList<WithOutPublicParameterLessCtor> l = - s.CreateSQLQuery("select s.Name as something from Simple s").SetResultTransformer( - Transformers.AliasToBean<WithOutPublicParameterLessCtor>()).List<WithOutPublicParameterLessCtor>(); - Assert.That(l.Count, Is.EqualTo(2)); - Assert.That(l, Has.All.Not.Null); + Cleanup(); } - - Cleanup(); } [Test] public void WorkWithPublicParameterLessCtor() { - Setup(); + try + { + Setup(); - var queryString = "select s.Name as something from Simple s"; - AssertAreWorking(queryString); // working for field access - - queryString = "select s.Name as Something from Simple s"; - AssertAreWorking(queryString); // working for property access + var queryString = "select s.Name as something from Simple s"; + AssertAreWorking(queryString); // working for field access - Cleanup(); + queryString = "select s.Name as Something from Simple s"; + AssertAreWorking(queryString); // working for property access + } + finally + { + Cleanup(); + } } + [Test] + public void WorksWithStruct() + { + try + { + Setup(); + + IList<TestStruct> result; + using (ISession s = OpenSession()) + { + result = s.CreateSQLQuery("select s.Name as something from Simple s") + .SetResultTransformer(Transformers.AliasToBean<TestStruct>()) + .List<TestStruct>(); + } + Assert.AreEqual(2, result.Count); + } + finally + { + Cleanup(); + } + } + private void AssertAreWorking(string queryString) { using (ISession s = OpenSession()) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |