I have looked very closely at the screen shots in the last few storefront episodes (since Rob contracted a mild case of DDD fever). I have modeled my own application repositry very closely on the one I see in the screenshots. I have written a test which
fails and I don't know how to go about fixing the problem. Any suggestions would be welcomed.
Using the SqlOrderRepository as an example I could write a test something like this
[TestMethod]
public void Order_shouldHave_Constant_Identity()
{
OrderProcessing.Order order1 = orderRepository
.GetOrders()
.Where(o=>o.OrderID == 1)
.SingleOrDefault();
OrderProcessing.Order order2 = orderRepository
.GetOrders()
.Where(o=>0.OrderID == 1)
.SingleOrDefault();
Assert.AreSame(order1, order2, "Getting the same order twice should return the same object");
}
Note that I attempt to retrieve the same OrderId both times. The Assert.AreSame fails as the two objects are not the same. This obviously opens the door to all sorts of nasty things happening.
I'm guessing that I need to introduce some kind of identity map for the domain objects but I don't know how to go about it. Any suggestions? (Hey Rob, an answer of 'Wait until after MIX, all will be revealed" would do for me)
I guess overriding Equals() and Hashcode() would make that particular test pass but I wouldn't really have the same object, I would just have two objects which happen to have the same properites. Maybe, I have the wrong test - let me try again.
order2.BillingAddress = new Address() { Country = "USA"} ;
Assert.AreEqual(order1.BillingAddress.Country, order2.BillingAdress.Country, "Getting the same order twice should return the same object");
}
This object identity problem is looked after by LinqToSql for the objects that are created as a result of retrieving database rows.
http://msdn.microsoft.com/en-us/library/bb399376.aspx but I don't know how to enforce the same logic for my domain objects.
Paul Linton
Star
13421 Points
2535 Posts
Avoiding duplicate domain objects
Feb 18, 2009 09:53 PM|LINK
I have looked very closely at the screen shots in the last few storefront episodes (since Rob contracted a mild case of DDD fever). I have modeled my own application repositry very closely on the one I see in the screenshots. I have written a test which fails and I don't know how to go about fixing the problem. Any suggestions would be welcomed.
Using the SqlOrderRepository as an example I could write a test something like this
[TestMethod] public void Order_shouldHave_Constant_Identity() { OrderProcessing.Order order1 = orderRepository .GetOrders() .Where(o=>o.OrderID == 1) .SingleOrDefault(); OrderProcessing.Order order2 = orderRepository .GetOrders() .Where(o=>0.OrderID == 1) .SingleOrDefault(); Assert.AreSame(order1, order2, "Getting the same order twice should return the same object"); }Note that I attempt to retrieve the same OrderId both times. The Assert.AreSame fails as the two objects are not the same. This obviously opens the door to all sorts of nasty things happening.
I'm guessing that I need to introduce some kind of identity map for the domain objects but I don't know how to go about it. Any suggestions? (Hey Rob, an answer of 'Wait until after MIX, all will be revealed" would do for me)
thanks for any help
robconery
Participant
852 Points
195 Posts
Re: Avoiding duplicate domain objects
Feb 18, 2009 10:52 PM|LINK
Have you tried to override Equals() and Hashcode?
Paul Linton
Star
13421 Points
2535 Posts
Re: Avoiding duplicate domain objects
Feb 18, 2009 11:06 PM|LINK
I guess overriding Equals() and Hashcode() would make that particular test pass but I wouldn't really have the same object, I would just have two objects which happen to have the same properites. Maybe, I have the wrong test - let me try again.
[TestMethod] public void Order_shouldHave_Constant_Identity() { OrderProcessing.Order order1 = orderRepository .GetOrders() .Where(o=>o.OrderID == 1) .SingleOrDefault(); order1.BillingAddress = new Address() {Country="Australia"}; OrderProcessing.Order order2 = orderRepository .GetOrders() .Where(o=>0.OrderID == 1) .SingleOrDefault();order2.BillingAddress = new Address() { Country = "USA"} ; Assert.AreEqual(order1.BillingAddress.Country, order2.BillingAdress.Country, "Getting the same order twice should return the same object"); }This object identity problem is looked after by LinqToSql for the objects that are created as a result of retrieving database rows. http://msdn.microsoft.com/en-us/library/bb399376.aspx but I don't know how to enforce the same logic for my domain objects.