Avoiding duplicate domain objects

Last post 02-18-2009 7:06 PM by Paul Linton. 2 replies.

Sort Posts:

  • Avoiding duplicate domain objects

    02-18-2009, 5:53 PM
    • Contributor
      4,980 point Contributor
    • Paul Linton
    • Member since 04-30-2008, 3:16 AM
    • Posts 870

    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

    Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
  • Re: Avoiding duplicate domain objects

    02-18-2009, 6:52 PM
    • Participant
      846 point Participant
    • robconery
    • Member since 02-23-2005, 10:16 PM
    • Posts 192
    • AspNetTeam

    Have you tried to override Equals() and Hashcode?

  • Re: Avoiding duplicate domain objects

    02-18-2009, 7:06 PM
    • Contributor
      4,980 point Contributor
    • Paul Linton
    • Member since 04-30-2008, 3:16 AM
    • Posts 870

    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.

    Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
Page 1 of 1 (3 items)