Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 984591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:02:29+00:00 2026-05-16T05:02:29+00:00

I have been building an application, which uses the LoadOperation’s Entities to return an

  • 0

I have been building an application, which uses the LoadOperation’s Entities to return an IEnumerable which becomes the source of a CollectionViewSource in my View Model. I am now discovering the potential pitfall to this approach, when adding Entities in my Silverlight client, I cannot see these entities, unless I either submit the New Entity, then reload, or Maintain a separate collection of items, which I am binding to.

What I really see as my options are:

  1. Add an ObservableCollection to use as the Source of the CollectionViewSource property in my ViewModel – this way I can add to both the DomainContext and the ObservableCollection at the same time to keep the collections in sync.
  2. Change the Binding to the EntitySet directly, and add a filtering event handler to provide the filtering on the CollectionViewSource.

If anyone has tips or thoughts about pros/cons of each, I would greatly appreciate it. In particular, I am wondering, if there are performance and/or programming benefits in favor of one or the other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:02:29+00:00Added an answer on May 16, 2026 at 5:02 am

    I am taking this one approach at a time. First, I am going to show a point of reference to dicuss this with, then I will highlight the different changes necessary to support each methodology.

    The basis for my demo is a single, authenticated domain service which returns a single entity of Resource. I will expose 4 commands (save, undo, add, and delete), plus a Collection, and a Property to hold the SelectedResource.

    2 Different classes implement this interface (1 for blending, 1 for production). The production is the only one I will discuss here. Notice the action(lo.Entities) in the GetMyResources function:

    public class WorkProvider
    {
        static WorkContext workContext;
        public WorkProvider()
        {
            if (workContext == null)
                workContext = new WorkContext();
        }
        public void AddResource(Resource resource)
        {
            workContext.Resources.Add(resource);
        }
        public void DelResource(Resource resource)
        {
            workContext.Resources.Remove(resource);
        }
        public void UndoChanges()
        {
            workContext.RejectChanges();
        }
        public void SaveChanges(Action action)
        {
            workContext.SubmitChanges(so =>
                {
                    if (so.HasError)
                        // Handle Error
                        throw so.Error;
                    else
                        action();
                }, null);
        }
        public void GetMyResources(Action<IEnumerable<Resource>> action)
        {
            var query = workContext.GetResourcesQuery()
                .Where(r => r.UserName == WebContext.Current.User.Name);
            workContext.Load(query, LoadBehavior.MergeIntoCurrent, lo =>
                {
                    if (lo.HasError)
                        // Handle Error
                        throw lo.Error;
                    else
                        action(lo.Entities);
                }, null);
        }
    }
    

    In the ViewModel, I have the following Implementation:

    public class HomeViewModel : ViewModelBase
    {
        WorkProvider workProvider;
        public HomeViewModel()
        {
            workProvider = new WorkProvider();
        }
    
        // _Source is required when returning IEnumerable<T>
        ObservableCollection<Resource> _Source; 
        public CollectionViewSource Resources { get; private set; }
        void setupCollections()
        {
            Resources = new CollectionViewSource();
            using (Resources.DeferRefresh())
            {
                _Source = new ObservableCollection<Resource>();
                Resources.Source = _Source;
                Resources.GroupDescriptions.Add(new PropertyGroupDescription("Title"));
                Resources.SortDescriptions.Add(new SortDescription("Title", ListSortDirection.Ascending));
                Resources.SortDescriptions.Add(new SortDescription("Rate", ListSortDirection.Ascending));
            }
        }
        void loadMyResources()
        {
            workProvider.GetMyResources(results =>
                {
                    using (Resources.DeferRefresh())
                    {
                        // This is required when returning IEnumerable<T>
                        _Source.Clear();
                        foreach (var result in results)
                        {
                            if (!_Source.Contains(result))
                                _Source.Add(result);
                        }
                    }
                });
        }
        Resource _SelectedResource;
        public Resource SelectedResource
        {
            get { return _SelectedResource; }
            set
            {
                if (_SelectedResource != value)
                {
                    _SelectedResource = value;
                    RaisePropertyChanged("SelectedResource");
                }
            }
        }
    
        public RelayCommand CmdSave { get; private set; }
        public RelayCommand CmdUndo { get; private set; }
        public RelayCommand CmdAdd { get; private set; }
        public RelayCommand CmdDelete { get; private set; }
        void setupCommands()
        {
            CmdSave = new RelayCommand(() =>
                {
                    workProvider.SaveChanges(() =>
                        {
                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                {
                                    System.Windows.MessageBox.Show("Saved");
                                });
                        });
                });
            CmdUndo = new RelayCommand(() =>
                {
                    workProvider.UndoChanges();
                    // This is required when returning IEnumerable<T>
                    loadMyResources();
                });
            CmdAdd = new RelayCommand(() =>
                {
                    Resource newResource = new Resource()
                    {
                        ResourceID = Guid.NewGuid(),
                        Rate = 125,
                        Title = "Staff",
                        UserName = "jsmith"
                    };
                    // This is required when returning IEnumerable<T>
                    _Source.Add(newResource);
                    workProvider.AddResource(newResource);
                });
            CmdDelete = new RelayCommand(() =>
            {
                // This is required when returning IEnumerable<T>
                _Source.Remove(_SelectedResource);
                workProvider.DelResource(_SelectedResource);
            });
        }
    }
    

    The alternate method would involve changing the WorkProvider class as follows (notice the action(workContext.Resources) that is returned:

        public void GetMyResources(Action<IEnumerable<Resource>> action)
        {
            var query = workContext.GetResourcesQuery()
                .Where(r => r.UserName == WebContext.Current.User.Name);
            workContext.Load(query, LoadBehavior.MergeIntoCurrent, lo =>
                {
                    if (lo.HasError)
                        // Handle Error
                        throw lo.Error;
                    else
                        // Notice Changed Enumeration
                        action(workContext.Resources);
                }, null);
        }
    

    And the changes to the viewmodel are as follows (notice the removal of the _Source ObservableCollection):

    public class HomeViewModel : ViewModelBase
    {
        WorkProvider workProvider;
        public HomeViewModel()
        {
            workProvider = new WorkProvider();
        }
    
        public CollectionViewSource Resources { get; private set; }
        void setupCollections()
        {
            Resources = new CollectionViewSource();
            using (Resources.DeferRefresh())
            {
                Resources.Filter += (s,a) =>
                    {
                        a.Accepted = false;
                        if (s is Resource)
                        {
                            Resource res = s as Resource;
                            if (res.UserName == WebContext.Current.User.Name)
                                a.Accepted = true;
                        }
                    };
                Resources.GroupDescriptions.Add(new PropertyGroupDescription("Title"));
                Resources.SortDescriptions.Add(new SortDescription("Title", ListSortDirection.Ascending));
                Resources.SortDescriptions.Add(new SortDescription("Rate", ListSortDirection.Ascending));
            }
        }
        void loadMyResources()
        {
            workProvider.GetMyResources(results =>
                {
                    using (Resources.DeferRefresh())
                    {
                        Resources.Source = results;
                    }
                });
        }
        Resource _SelectedResource;
        public Resource SelectedResource
        {
            get { return _SelectedResource; }
            set
            {
                if (_SelectedResource != value)
                {
                    _SelectedResource = value;
                    RaisePropertyChanged("SelectedResource");
                }
            }
        }
    
        public RelayCommand CmdSave { get; private set; }
        public RelayCommand CmdUndo { get; private set; }
        public RelayCommand CmdAdd { get; private set; }
        public RelayCommand CmdDelete { get; private set; }
        void setupCommands()
        {
            CmdSave = new RelayCommand(() =>
                {
                    workProvider.SaveChanges(() =>
                        {
                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                {
                                    System.Windows.MessageBox.Show("Saved");
                                });
                        });
                });
            CmdUndo = new RelayCommand(() =>
                {
                    workProvider.UndoChanges();
                    Resources.View.Refresh();
                });
            CmdAdd = new RelayCommand(() =>
                {
                    Resource newResource = new Resource()
                    {
                        ResourceID = Guid.NewGuid(),
                        Rate = 125,
                        Title = "Staff",
                        UserName = "jsmith"
                    };
                    workProvider.AddResource(newResource);
                });
            CmdDelete = new RelayCommand(() =>
            {
                workProvider.DelResource(_SelectedResource);
            });
        }
    }
    

    While the second approach definately requires adding the filter event handler in the configuration of the CollectionViewSource, and could be seen as filtering data 2 times (1 time at the server, and the second time by the CollectionViewSource), it does off the following benefits: There is a single collection – which makes management of collection notifications simpler and easier. The collection is the actual collection which will be submitted to the server, which makes managing adds/deletes simpler, since there are not opportunities for forgetting to add/remove entities from the correct collection to initiate the add/delete function when submitting back.

    The one last thing I need to confirm is the following: On a collectionviewsource, it is my understanding that you should use DeferRefresh() when making multiple changes that affect the view. This just prevents unnecessary refreshes from occuring when internal changes may cause refreshes such as configuring sorting, grouping, etc. It is also important to call .View.Refresh() when we expect the UI to process some update changes. The .View.Refresh() is probably more important to note than the DeferRefresh(), since it actually causes a UI update, as opposed to a prevent unexpected UI updates.

    I don’t know if this will help others, but I hope so. I definately spent some time working through these and trying to understand this. If you have clarifications or other things to add, please feel free to do so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been given an application which uses a build.xml file for building purposes.
Context: I have been working on and off on an application which uses a
I'm building an application which uses AutoFac 2 for DI. I've been reading that
I am absolutely new to the Android platform and have been building an application
I have been building a Rails application that performs accounting functionality. As part of
I have a JSF 2 application which uses a Richfaces 4 tree control. The
I have been building an application on android and wanted to do some user
I have been building a JSF application using JPA to access the DB. There
I have been building enterprise software for the last 10 years. In this time
I have been building IMO a really cool RIA. But its now close to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.