Fetching a child object after the parent has been fetched.

Fetching a child object after the parent has been fetched.

Old forum URL: forums.lhotka.net/forums/t/6444.aspx


mikemir8 posted on Tuesday, February 17, 2009

I have the following scenario:
Project
-- ProjectResources
---- Resource

Kind of similar to the ProjectTracker example, with the difference that each resource can ONLY be assigned to one project at a time.

Now, I'd like to know what others are doing for this: After I load a project and all of its resources, suppose I want to assign a Resource from a different project to my current project. The way I see it, I should somehow fetch that Resource and assign it to the ProjectResources list of my current project. But since Resource is a child object, I cannot ask Resource to fetch itself unless I make it a switchable object. Who should be responsible for loading this Resource? Or should I take a completely different route?

- Miguel

ajj3085 replied on Tuesday, February 17, 2009

You could still create a resource, because the Resource is a LINK to an actual resource in this case.. not the resource itself.  You could have a NewResource factory method, which hits the database (to check if the resource is used, for example) and then return that.. but at the very bottom of your DataPortal_Create, call MarkAsChild, or have the call in your constructor.  It's still not a switchable object, but it can be "created" independently.

You could hide all of this as well; on your ProjectResources, have and AddResource method that takes paremeters to indicate which resource to link to your project.. then your CreateResource factory method would be internal in scope.  That might be a more clear option for the UI developer.

mikemir8 replied on Tuesday, February 17, 2009

The thing with that approach is that in the create method I would need to delete the previous assignment from the database, and later I would need to Insert a new one. I guess that's pretty much what's needed for many scenarios, but in mine I need to update the existing record assignment. Or did I miss something from you advice?

ajj3085 replied on Tuesday, February 17, 2009

You could handle the deletion in your Child_Insert code, and then create a new link as well.  I don't think you'd want to break the link to the existing project until the root Project has been saved anyway, correct?  Otherwise you could break the link, then choose not to save the current project and you'll have to restore the link somehow.

Copyright (c) Marimer LLC