Search code examples
c#silverlightsilverlight-4.0wcf-ria-services

Constant updating and calling submitchanges: This entity is currently read-only ria services


I am making a Todo Type of application where a user can make a note and constantly move the note around the screen. On every change of position of "note" I need to save it into database:

The typical code for this is:

 private void SaveChanges()
    {
        if (!_context.IsSubmitting)
        {
            this.busyIndicator.IsBusy = true;
            _context.SubmitChanges(subOp =>
            {

                this.busyIndicator.IsBusy = false;
                if (subOp.Error != null)
                {
                    ErrorWindow window = new ErrorWindow(subOp.Error.Message);
                    window.Show();
                }

            }, null);
        }
    }

However as you can see I am blocking the UI by using busyindicator and stopping the user to do any activity while it is submitting. This is bad especially for this application. So as an alternative I tried to move this code onto a different thread using this code:

    private void SaveChanges()
    {
        ThreadPool.QueueUserWorkItem(waitcall =>
        {
            if (!_context.IsSubmitting)
            {
                //this.busyIndicator.IsBusy = true;
                _context.SubmitChanges(subOp =>
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        //this.busyIndicator.IsBusy = false;
                        if (subOp.Error != null)
                        {
                            ErrorWindow window = new ErrorWindow(subOp.Error.Message);
                            window.Show();
                        }
                    });

                }, null);
            }

        });
    }

This works and the application still remains responsive. However now while the context is submitting the changes and the user works with the same note by moving it around the screen etc the binding try to change the entity's properties and I get exception:

This entity is currently read-only

I understand this is obvious because the context is submitting the changes. However is there any way by which I can keep the application responsive by not showing the dumb busy indicator and still not get these exceptions?


Solution

  • During SubmitChanges, all changed properties are set to read only. In your case, it is the position properties. I am guessing that the properties are bound to the note position.

    Possiblities:

    1. Do not bind to the entity in your DomainContext. Bind to a client local object with property changed events (perhaps a clone of the entity). Use the property changed events to try to copy the change to the context entity. If it fails (read-only), you may want to add code to retry later.

    2. Use EntityGraph partial submit from RIA Services Contrib. riaservicescontrib.codeplex.com Check the discussions / blogs - code is provided to clone the EntityGraph into a temp context and submit the temp context. I do not think you will want to Synchronize on completion because that would probably overwrite the new position and cause the window to jump around. Just submit and forget. If you are adding the entity on the client and it needs auto generated key properties returned from the server (identity, etc.). Then, do not use a temp context / partial submit for the add. Block the UI and submit the add normally so your client properties are updated on completion and a partial submit is not called before your key values have been set.

    I would be interested in any other possible solutions.