From: <dav...@us...> - 2008-12-25 19:57:58
|
Revision: 3965 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3965&view=rev Author: davybrion Date: 2008-12-25 19:57:53 +0000 (Thu, 25 Dec 2008) Log Message: ----------- applying patch from Germ?\195?\161n Schuager for NH-1621 Modified Paths: -------------- trunk/nhibernate/src/NHibernate/NHibernate.csproj trunk/nhibernate/src/NHibernate/Properties/PropertyAccessorFactory.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate/Properties/ReadonlyAccessor.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Fixture.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Mappings.hbm.xml trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Model.cs trunk/nhibernate/src/NHibernate.Test/PropertyTest/ReadonlyAccessorFixture.cs Modified: trunk/nhibernate/src/NHibernate/NHibernate.csproj =================================================================== --- trunk/nhibernate/src/NHibernate/NHibernate.csproj 2008-12-17 06:50:00 UTC (rev 3964) +++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2008-12-25 19:57:53 UTC (rev 3965) @@ -840,6 +840,7 @@ <Compile Include="Proxy\Map\MapProxyFactory.cs" /> <Compile Include="Proxy\Poco\BasicLazyInitializer.cs" /> <Compile Include="QueryParameterException.cs" /> + <Compile Include="Properties\ReadonlyAccessor.cs" /> <Compile Include="SessionException.cs" /> <Compile Include="SqlCommand\InformixJoinFragment.cs" /> <Compile Include="SqlCommand\SubselectClauseExtractor.cs" /> @@ -1150,4 +1151,4 @@ <Target Name="AfterBuild"> </Target> --> -</Project> \ No newline at end of file +</Project> Modified: trunk/nhibernate/src/NHibernate/Properties/PropertyAccessorFactory.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Properties/PropertyAccessorFactory.cs 2008-12-17 06:50:00 UTC (rev 3964) +++ trunk/nhibernate/src/NHibernate/Properties/PropertyAccessorFactory.cs 2008-12-25 19:57:53 UTC (rev 3965) @@ -23,6 +23,7 @@ accessors["property"] = new BasicPropertyAccessor(); accessors["field"] = new FieldAccessor(); accessors["backfield"] = new FieldAccessor(new BackFieldStrategy()); + accessors["readonly"] = new ReadOnlyAccessor(); accessors["field.camelcase"] = new FieldAccessor(new CamelCaseStrategy()); accessors["field.camelcase-underscore"] = new FieldAccessor(new CamelCaseUnderscoreStrategy()); accessors["field.lowercase"] = new FieldAccessor(new LowerCaseStrategy()); @@ -84,6 +85,14 @@ /// allows users of the Class to get the value of the Id but not set the value. /// </description> /// </item> + /// <item> + /// <term>readonly</term> + /// <description> + /// The <c>name</c> attribute is the name of the Property. NHibernate will use the + /// Property's get method to retrieve the value but will never set the value back in the domain. + /// This is used for read-only calculated properties with only a get method. + /// </description> + /// </item> /// <item> /// <term>Assembly Qualified Name</term> /// <description> Added: trunk/nhibernate/src/NHibernate/Properties/ReadonlyAccessor.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Properties/ReadonlyAccessor.cs (rev 0) +++ trunk/nhibernate/src/NHibernate/Properties/ReadonlyAccessor.cs 2008-12-25 19:57:53 UTC (rev 3965) @@ -0,0 +1,76 @@ +using System.Reflection; + +namespace NHibernate.Properties +{ + /// <summary> + /// Access the mapped property through a Property <c>get</c> to get the value + /// and do nothing to set the value. + /// </summary> + /// <remarks> + /// This is useful to allow calculated properties in the domain that will never + /// be recovered from the DB but can be used for querying. + /// </remarks> + public class ReadOnlyAccessor : IPropertyAccessor + { + /// <summary> + /// Initializes a new instance of <see cref="ReadOnlyAccessor"/>. + /// </summary> + public ReadOnlyAccessor() + { + } + + #region IPropertyAccessor Members + + /// <summary> + /// Creates an <see cref="BasicPropertyAccessor.BasicGetter"/> to <c>get</c> the value from the Property. + /// </summary> + /// <param name="type">The <see cref="System.Type"/> to find the Property in.</param> + /// <param name="propertyName">The name of the mapped Property to get.</param> + /// <returns> + /// The <see cref="BasicPropertyAccessor.BasicGetter"/> to use to get the value of the Property from an + /// instance of the <see cref="System.Type"/>.</returns> + /// <exception cref="PropertyNotFoundException" > + /// Thrown when a Property specified by the <c>propertyName</c> could not + /// be found in the <see cref="System.Type"/>. + /// </exception> + public IGetter GetGetter(System.Type type, string propertyName) + { + BasicPropertyAccessor.BasicGetter result = BasicPropertyAccessor.GetGetterOrNull(type, propertyName); + if (result == null) + { + throw new PropertyNotFoundException(type, propertyName, "getter"); + } + return result; + } + + /// <summary> + /// Create a <see cref="NoopAccessor.NoopSetter"/> to do nothing when trying to + /// se the value of the mapped Property + /// </summary> + /// <param name="type">The <see cref="System.Type"/> to find the mapped Property in.</param> + /// <param name="propertyName">The name of the mapped Property to set.</param> + /// <returns> + /// An instance of <see cref="NoopAccessor.NoopSetter"/>. + /// </returns> + public ISetter GetSetter(System.Type type, string propertyName) + { + return new NoopSetter(); + } + + public bool CanAccessThroughReflectionOptimizer + { + get { return true; } + } + + #endregion + + private class NoopSetter : ISetter + { + public void Set(object target, object value) {} + + public string PropertyName { get { return null; } } + + public MethodInfo Method { get { return null; } } + } + } +} Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Fixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Fixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Fixture.cs 2008-12-25 19:57:53 UTC (rev 3965) @@ -0,0 +1,44 @@ +using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; + +namespace NHibernate.Test.NHSpecificTest.NH1621 +{ + [TestFixture] + public class Fixture : BugTestCase + { + ISession session; + + public override string BugNumber + { + get { return "NH1621"; } + } + + [Test] + public void QueryUsingReadonlyProperty() + { + using (session = OpenSession()) + { + Nums nums1 = new Nums {ID = 1, NumA = 1, NumB = 2}; + session.Save(nums1); + + Nums nums2 = new Nums {ID = 2, NumA = 2, NumB = 2 }; + session.Save(nums2); + + Nums nums3 = new Nums {ID = 3, NumA = 5, NumB = 2 }; + session.Save(nums3); + + session.Flush(); + session.Clear(); + + var nums = session.CreateQuery("from Nums b where b.Sum > 4").List<Nums>(); + + Assert.That(nums, Has.Count(1)); + Assert.That(nums[0].Sum, Is.EqualTo(7)); + + session.Delete("from Nums"); + session.Flush(); + session.Close(); + } + } + } +} Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Mappings.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Mappings.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Mappings.hbm.xml 2008-12-25 19:57:53 UTC (rev 3965) @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" + namespace="NHibernate.Test.NHSpecificTest.NH1621" + assembly="NHibernate.Test" +> + <class name="Nums" table="nums"> + <id name="ID"> + <generator class="assigned"/> + </id> + <property name="NumA"/> + <property name="NumB"/> + <property name="Sum" access="readonly"/> + </class> +</hibernate-mapping> Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Model.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Model.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1621/Model.cs 2008-12-25 19:57:53 UTC (rev 3965) @@ -0,0 +1,17 @@ +namespace NHibernate.Test.NHSpecificTest.NH1621 +{ + public class Nums + { + public virtual int ID { get; set; } + + public virtual int NumA { get; set; } + public virtual int NumB { get; set; } + public virtual int Sum + { + get + { + return NumA + NumB; + } + } + } +} Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2008-12-17 06:50:00 UTC (rev 3964) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2008-12-25 19:57:53 UTC (rev 3965) @@ -553,6 +553,8 @@ <Compile Include="NHSpecificTest\NH1594\A.cs" /> <Compile Include="NHSpecificTest\NH1594\Fixture.cs" /> <Compile Include="NHSpecificTest\NH1608\Fixture.cs" /> + <Compile Include="NHSpecificTest\NH1621\Fixture.cs" /> + <Compile Include="NHSpecificTest\NH1621\Model.cs" /> <Compile Include="NHSpecificTest\NH280\Fixture.cs" /> <Compile Include="NHSpecificTest\NH280\Foo.cs" /> <Compile Include="NHSpecificTest\NH1018\Employee.cs" /> @@ -825,6 +827,7 @@ <Compile Include="PropertyRef\KeyPropertyRefFixture.cs" /> <Compile Include="PropertyTest\BackFieldAccessorFixture.cs" /> <Compile Include="PropertyTest\BasicSetterExceptionFixture.cs" /> + <Compile Include="PropertyTest\ReadonlyAccessorFixture.cs" /> <Compile Include="PropertyTest\FieldAccessorFixture.cs" /> <Compile Include="PropertyTest\FieldCamelCaseFixture.cs" /> <Compile Include="PropertyTest\FieldCamelCaseUnderscoreFixture.cs" /> @@ -1573,6 +1576,7 @@ <EmbeddedResource Include="Cascade\JobBatch.hbm.xml" /> <EmbeddedResource Include="Deletetransient\Person.hbm.xml" /> <Content Include="DynamicEntity\package.html" /> + <EmbeddedResource Include="NHSpecificTest\NH1621\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1609\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1605\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1443\AclassWithSpecific.hbm.xml" /> @@ -1678,4 +1682,4 @@ if exist "$(ProjectDir)hibernate.cfg.xml" (copy "$(ProjectDir)hibernate.cfg.xml" "hibernate.cfg.xml") copy /y "..\..\..\NHibernate.DomainModel\ABC.hbm.xml" "ABC.hbm.xml"</PostBuildEvent> </PropertyGroup> -</Project> \ No newline at end of file +</Project> Added: trunk/nhibernate/src/NHibernate.Test/PropertyTest/ReadonlyAccessorFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/PropertyTest/ReadonlyAccessorFixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/PropertyTest/ReadonlyAccessorFixture.cs 2008-12-25 19:57:53 UTC (rev 3965) @@ -0,0 +1,39 @@ +using NHibernate.Properties; +using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; + +namespace NHibernate.Test.PropertyTest +{ + [TestFixture] + public class ReadonlyAccessorFixture + { + [Test] + public void GetValue() + { + var accessor = PropertyAccessorFactory.GetPropertyAccessor("readonly"); + var getter = accessor.GetGetter(typeof(Calculation), "Sum"); + + Assert.That(getter.Get(new Calculation()), Is.EqualTo(2)); + } + + [Test] + public void SetValue() + { + var accessor = PropertyAccessorFactory.GetPropertyAccessor("readonly"); + var getter = accessor.GetGetter(typeof(Calculation), "Sum"); + var setter = accessor.GetSetter(typeof(Calculation), "Sum"); + + var i = new Calculation(); + Assert.That(getter.Get(i), Is.EqualTo(2)); + setter.Set(i, 1); + Assert.That(getter.Get(i), Is.EqualTo(2)); + } + } + + public class Calculation + { + public Calculation() { } + + public int Sum { get { return 1 + 1; } } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |