Hi everyone,

I'm using NHibernate.Linq to expose data in a REST-ful manner using
ADO.NET Data Services (ADS). I discovered that when updating an entity
using a HTTP PUT request, the current IUpdatable implementation will
reset all of the entity's properties to their default (usually null)
values. While this is the desired behavior for primitive properties,
it shouldn't be done for entity properties. The default IUpdatable
implementation that ADS contains for ADO.NET Entity Framework checks
if the property is of an entity type and if so, skips resetting its
value.

I have done the same for NHibernateContext that implements IUpdatable.
I get the property type from the IClassMetadata object and reset the
property only if the property type IsEntityType property is false. I
also added two tests that cover the scenarios. So, I'd like to submit
a patch. Is this the correct place to do so?

Find the patch below (also available at <http://dl.getdropbox.com/u/
1838958/ResetResourceNotAffectingEntityProps.patch>).

Best,
Hristo Deshev
~~~~~~~~~~~~~~~~~~~~~

Index: src/NHibernate.Linq.Tests/IUpdatableTests.cs
===================================================================
--- src/NHibernate.Linq.Tests/IUpdatableTests.cs        (revision 1041)
+++ src/NHibernate.Linq.Tests/IUpdatableTests.cs        (working copy)
@@ -216,5 +216,25 @@
                        Assert.AreSame(usr, usr2);
                }

+               [Test]
+               public void TestResetResourceForPrimitiveProperties()
+               {
+                       var simpleEntity = (AnotherEntity) update.CreateResource
("AnotherEntities", typeof(AnotherEntity).FullName);
+                       update.ResetResource(simpleEntity);
+                       Assert.IsNull(simpleEntity.Output);
+               }
+
+               [Test]
+               public void TestResetResourceForEntityProperties()
+               {
+                       var complexEntity = (Role) 
update.CreateResource("Roles", typeof
(Role).FullName);
+                       var simpleEntity = (AnotherEntity) update.CreateResource
("AnotherEntities", typeof(AnotherEntity).FullName);
+                       complexEntity.Entity = simpleEntity;
+
+                       update.ResetResource(complexEntity);
+                       Assert.IsNull(complexEntity.Name, "Simple property gets 
reset.");
+                       Assert.IsNotNull(complexEntity.Entity, "Entity property 
gets
preserved.");
+               }
+
        }
 }
Index: src/NHibernate.Linq/NHibernateContext.cs
===================================================================
--- src/NHibernate.Linq/NHibernateContext.cs    (revision 1041)
+++ src/NHibernate.Linq/NHibernateContext.cs    (working copy)
@@ -302,11 +302,16 @@
                        IClassMetadata metadata = 
session.SessionFactory.GetClassMetadata
(resource.GetType().ToString());
                        object tempCopy = metadata.Instantiate(null, 
EntityMode.Poco);

-                       // Copy the default non-keys
-                       foreach (string propName in metadata.PropertyNames)
+                       for (int i = 0; i < metadata.PropertyNames.Length; i++)
                        {
-                               object value = 
metadata.GetPropertyValue(tempCopy, propName,
EntityMode.Poco);
-                               update.SetValue(resource, propName, value);
+                               var propertyType = metadata.PropertyTypes[i];
+                               var propName = metadata.PropertyNames[i];
+
+                               if (!propertyType.IsEntityType)
+                               {
+                                       object value = 
metadata.GetPropertyValue(tempCopy, propName,
EntityMode.Poco);
+                                       update.SetValue(resource, propName, 
value);
+                               }
                        }

                        //Return the new resource

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"NHibernate Contrib - Development Group" 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.ar/group/nhcdevs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to