开发者

Allowing selective entity saves in a paging WPF MVVM application using LINQ to SQL

I am developing an application that allows the editing of a SQL Server database. The problem that I am having is with allowing the user to save some changes but discard some others. I am hoping for some advice regarding my design.

The application is MVVM. As Model I use the LINQ to SQL classes. In general, I have ViewModel classes for each entity class. I then create a ViewModel instance for each entity instance

The interface consists of a single window, where I switch Views to implement a pagin开发者_如何学Gog system. For the most part, there is a 1:1 relationship between Entities, ViewModels, Views and Pages.

The problem I am having is best described by the following example:

Consider the following tables:

  • Services
  • StaffMembers
  • StaffMemberAssignments

I have an entity, Service. It is represented in the database by a row in the Services table, and has parameters such as "Name", "ContractValue" and "Type".

Each Service also contains a list of StaffMemberAssignments. StaffMemberAssignments are stored in their own table and contain some parameters regarding the assignments, as well as two foreign keys - one to the Service where they have been assigned, and one to the StaffMember table, to the StaffMember that is being assigned.

In summary:

Service - StaffMemberAssignment - StaffMember

After editing a Service (on the ServiceView, using a ServiceViewModel based on a single Service instance), the user can "Save Changes" or "Cancel". The "Save Changes" command submits all changes to the LINQ to SQL datacontext to the Database. Cancel restores the Service object to its original state.

The user can add StaffMembers to the Service - this creates StaffMemberAssignments to link the StaffMember to the Service.

Here comes the tricky part

If the user wants to add a StaffMember to the Service, the StaffMembersListView is opened up (it's a table with the option of selection). This view replaces the ServiceView as the current page. After selecting a StaffMember, paging return to the ServiceView and all is well, with the new StaffMemberAssignment created to link the StaffMember to the Service.

However, while viewing the StaffMembers list, the user can choose to "Create new Staffmember". This opens the StaffMemberView with a new StaffMember instance and viewmodel. Here the user can enter details for the new StaffMember and can then "Save Changes" or "Cancel". "Cancel" simply returns to the previous page and discards the newly created StaffMember entity, while "Save" commits the new entity to the database. The StaffMembersListView is then returned, the user can select the new StaffMember from the list, and return to the ServiceView where the new StaffMemberAssignment has been made.

The problem is

When the new Staffmember was created, saving it pushed all datacontext changes to the DB. This means that any edits to the Service that the user was busy with, have also been pushed to the DB. If the user chooses to "Cancel" the service after creating the new StaffMember, the Service will not be returned to its original state.

The desired behaviour is that when the StaffMember is created and "Save"-d, only the new StaffMember is pushed to the DB. The user can select it from the list, adding it to the Service. Then, if the user Cancels the Service changes, the Service returns to its original state, but the newly created StaffMember will remain in the StaffMember table. It has, after all, been Saved.

Googling for ways to selectively save entities gave me the impression that this is not supposed to be done. So how can I modify either my data layer design or my UI design to get the functionality that I want? Or is what I want unrealistic?

Final Summation of steps leading to problem:

  1. User opens Service entity for editing
  2. User wishes to select StaffMember to link to Service
  3. User creates new StaffMember
  4. New StaffMember is saved to DB.

    Changes to Service are also saved, but should not be saved.

  5. User selects a StaffMember to add to the Service (the new one or any other)
  6. User Cancels changes to Service. Service must return to original state but cannot, due to collateral save in step 4.


Wouldn't it be an option to use a different model context instance for creating the StaffMember?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜