Wrox Programmer Forums
|
BOOK: Professional ASP.NET Design Patterns
This is the forum to discuss the Wrox book Professional ASP.NET Design Patterns by Scott Millett; ISBN: 978-0-470-29278-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET Design Patterns section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2012, 10:04 AM
Registered User
 
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Delete Child From Aggregate Root

I am trying to delete a child object from an aggregate root but I keep receiving an error saying the foreign key is null.

I am using EF and the repository pattern and saving my aggregate root like below.

Code:
Dim product As New ProductInfo
product = _productRepository.FindBy(1).FirstOrDefault
If (product IsNot Nothing) Then
    stockTransfer.Remove(product.StockKeys.Where(Function(x) x.Id = 1).FirstOrDefault)
End If

_stockTransferRepository.Save(stockTransfer)
I have read the following two links.

http://blogs.msdn.com/b/dsimmons/arc...ps-in-ef4.aspx

http://mocella.blogspot.com/2010/01/...ect-graph.html

The links suggest to use DeleteObject to manually delete the child from the parent. Because when I remove the child from the parents collection it just removes the relationship but still leaves the record in the database.

My Save method in the repository is expecting an aggregate root. Will I need to create a new Save method inside my repository that passes in the aggregate root and the child object and then delete the child object manually while saving the aggregate root like below.

Code:
Public Sub SaveParentAndRemoveChild(entity As StockTransfer, item As StockTransferItem) Implements IStockTransferRepository.SaveParentAndRemoveChild
            GetObjectContext().DeleteObject(item)
            MyBase.Save(entity)
End Sub
Any suggestions would be great.

Mike
 
Old February 25th, 2012, 06:29 AM
elbandit's Avatar
Wrox Author
 
Join Date: May 2007
Posts: 107
Thanks: 10
Thanked 17 Times in 15 Posts
Default

Hi mike,

Thanks for reading the book!

This is the issue you have http://stackoverflow.com/questions/2...-delete-it-why

Have a read of the answer in this post as I think it could solve your issue http://social.msdn.microsoft.com/For...3-bac661331788

I am not much of an EF expert I'm afraid. This is really easy to do in NHiberate though!

Cheers
Scott
 
Old February 27th, 2012, 05:49 AM
Registered User
 
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your response.

I am able to delete the child object using the DeleteObject() method but I was wondering which is the best way to follow DDD with deleting child objects with EF.

Should I create another method in my repository like DeleteChild() and call this before I use the save method?
 
Old September 6th, 2012, 01:03 AM
Registered User
 
Join Date: Jun 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Where did you get to with this? I've also run into the same issue, and even adding a DeleteChild method on the repository is tricky because all the methods thereafter expect an AggregateRoot which its not.

I would be curious to know how this is done in NHibernate because it must be translatable.

I'm coming to the conclusion that a generic delete method needs to be added to the UOW which can delete any object. Practical, but defeats the architecture somewhat.

I had to do something similar for creating entities, since entities need to be created by the calling CreateObject for the data context to register them I added T CreateObject<t>() to my UOW to handle non-aggreate root entities.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find child node when there is mentioned (child::*) mlohokare XSLT 3 May 12th, 2009 12:40 PM
aggregate function polofson BOOK: Professional SQL Server 2005 Reporting Services ISBN: 0-7645-8497-9 0 August 14th, 2007 01:29 PM
How to use the SUM Aggregate gregalb SQL Server 2000 1 May 10th, 2007 01:19 AM
DataShaping with Aggregate Jon4Paz Classic ASP Databases 0 September 15th, 2006 01:29 PM
delete XML node from JavaScript - not child crmpicco Javascript How-To 1 April 27th, 2006 11:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.