views:

270

answers:

0

Hi guys

I'm faced with my first real practical usage of a many-to-many relationship and am having a little bit of trouble with the approach...

I'm using ASP.Net MVC and have the following 2 entities: Activity <---> Program.

Now I was wondering what people think of the approach and if there is a better way of doing this:

  • When pull out an Activity, pull out a list of related Program ID's. What is the best way of doing this with minimum connections?
  • I then map the data from the EF data objects to my business obejcts (in our system EF data objects don't move beyond the data access layer)
  • The list that exists on the business object isn't just an IList, but probably something like IList.
    • Where ManyRelation has both an Id and Status property (where status is either: Unchanged, Added or Removed - when the data is coming out it would be marked as unchanged by default)
  • When rendering the page, my thoughts are to put these into a comma separated format in a hidden textbox on the page
  • Then when the post occurs I have the old list of values and a new list of currently selected values
  • I then create a new IList and merge the two lists together
    • At this point, as a result of the merge I would know what Unchanged, Added or Removed
  • Once I am back at the EF data level, I can add a stub Program (where only the ID is set) of any Unchanged and Removed items to a new stub Activity I create
  • Attach the Activity and all the its currently related Programs to the context
  • Remove any Programs that are marked as removed
  • Add in the additional Programs that are marked as Added
  • Finally save the changes

Does that all kinda make sense? Is there a better way of doing this or is this the standard approach (considering that I use the EF entities at the data access layer only, I have detected entities and I want to use stub entities for the relation)?

Cheers Anthony