Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Eager-loaded data casts successfully to static type object graphs

  • Loading branch information...
commit 5c3338c553340a60bc1d85a5e7f9b8ac6046dc24 1 parent 14cd9d4
@markrendle authored
View
1  Simple.Data.SqlTest/OrderDetailTests.cs
@@ -58,5 +58,6 @@ class Order
public int OrderId { get; set; }
public DateTime OrderDate { get; set; }
public int CustomerId { get; set; }
+ public Customer Customer { get; set; }
}
}
View
21 Simple.Data.SqlTest/QueryTest.cs
@@ -341,5 +341,26 @@ public void WithClauseShouldPreselectMasterTableAsDictionary()
var customer = result["Customer"] as IDictionary<string, object>;
Assert.IsNotNull(customer);
}
+
+ [Test]
+ public void WithClauseShouldCastToStaticTypeWithComplexProperty()
+ {
+ var db = DatabaseHelper.Open();
+ Order actual = db.Orders.FindAllByOrderId(1).WithCustomer().FirstOrDefault();
+ Assert.IsNotNull(actual);
+ Assert.IsNotNull(actual.Customer);
+ Assert.AreEqual("Test", actual.Customer.Name);
+ Assert.AreEqual("100 Road", actual.Customer.Address);
+ }
+
+ [Test]
+ public void WithClauseShouldCastToStaticTypeWithCollection()
+ {
+ var db = DatabaseHelper.Open();
+ Customer actual = db.Customers.FindAllByCustomerId(1).WithOrders().FirstOrDefault();
+ Assert.IsNotNull(actual);
+ Assert.AreEqual(1, actual.Orders.Single().OrderId);
+ Assert.AreEqual(new DateTime(2010,10,10), actual.Orders.Single().OrderDate);
+ }
}
}
View
24 Simple.Data/ConcreteTypeCreator.cs
@@ -60,8 +60,28 @@ public bool TryCreate(IDictionary<string, object> data, out object result)
{
value = Convert.ChangeType(value, propertyInfo.PropertyType);
}
- propertyInfo.SetValue(obj, value, null);
- anyPropertiesSet = true;
+ if (propertyInfo.CanWrite)
+ {
+ propertyInfo.SetValue(obj, value, null);
+ anyPropertiesSet = true;
+ }
+ else
+ {
+ var propertyValue = propertyInfo.GetValue(obj, null);
+ if (propertyValue == null) continue;
+ var addMethod = propertyValue.GetType().GetMethod("Add");
+ if (addMethod != null)
+ {
+ var valueItems = value as IEnumerable;
+ if (valueItems != null)
+ {
+ foreach (var valueItem in valueItems)
+ {
+ addMethod.Invoke(propertyValue, new[] {valueItem});
+ }
+ }
+ }
+ }
}
result = anyPropertiesSet ? obj : null;
Please sign in to comment.
Something went wrong with that request. Please try again.