How can I manage domain object collections from another domain object's view

Nov 27, 2012 at 10:55 AM

Hi,

I have a Customer domain object, which holds collections of other domain objects (Folders, Policies).

What is the NO approach for managing (create, modify, delete) folders and policies from the customer's view?

Coordinator
Nov 27, 2012 at 11:28 AM

There's no 'NO approach', as such, for this.  Generally, I might add actions to the Customer itself to add or delete the associated objects.  In fact, if you add a collection using the 'coll' snippet, you'll get these AddToXxx and RemoveFromXxx actions added automatically.

Nov 27, 2012 at 11:47 AM

Thanks Richard.

I have these actions attached to the type, but I'm looking for create rather than add.  That is, I don't want to add an existing Folder,  I want to open the "New Folder" view directly from the customer (and while at it, associate the new folder with the customer).

Coordinator
Nov 27, 2012 at 12:19 PM

Try this (note that the Folder class needs to have a Customer property  -  but it should have anyway (based on there being an FK back to Customer in the database).

public class Customer {

  public ICollection<Folder> Folders {...}

  public Folder CreateNewFolder() {
    var f = Container.NewTransientInstance<Folder>();
    f.Customer = this;
    return f;
  }
}


Note that there is no need to add the new folder to the Folders collection - EF will handle this automatically when you save the new folder (based on having already populated its Customer property). (You'll need an injected Container  -  use the 'injc' snippet to add one).

Nov 27, 2012 at 12:28 PM

Beautiful :)

Thanks again!