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 454097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:14:13+00:00 2026-05-12T22:14:13+00:00

Although BindingList<T> and ObservableCollection<T> provide mechanisms to detect list changes, they don’t support mechanisms

  • 0

Although BindingList<T> and ObservableCollection<T> provide mechanisms to detect list changes, they don’t support mechanisms to detect/intercept changes before they happen.

I’m writing a couple of interfaces to support this, but I want to canvas your opinion.

Option 1: Lists raise events for each type of action

Here, consumers might write code like this:

public class Order : Entity
    {
        public Order()
        {
            this.OrderItems = new List<OrderItem>();
            this.OrderItems.InsertingItem += new ListChangingEventHandler<OrderItem>(OrderItems_InsertingItem);
            this.OrderItems.SettingItem += new ListChangingEventHandler<OrderItem>(OrderItems_SettingItem);
            this.OrderItems.RemovingItem += new ListChangingEventHandler<OrderItem>(OrderItems_RemovingItem);
        }

        virtual public List<OrderItem> OrderItems { get; internal set; }

        void OrderItems_InsertingItem(object sender, IOperationEventArgs<OrderItem> e)
        {
            if (!validationPasses)
            {
                e.Cancel = true;
                return;
            }

            e.Item.Parent = this;
        }

        void OrderItems_SettingItem(object sender, IOperationEventArgs<OrderItem> e)
        {
            if (!validationPasses)
            {
                e.Cancel = true;
                return;
            }

            e.Item.Parent = this;
        }

        void OrderItems_RemovingItem(object sender, IOperationEventArgs<OrderItem> e)
        {
            if (!validationPasses)
            {
                e.Cancel = true;
                return;
            }

            e.Item.Parent = null;
        }

    }

Option 2: Lists raise a single event, and the action is determined from the event args

Here, consumers might write code like this:

public class Order : Entity
    {
        public Order()
        {
            this.OrderItems = new List<OrderItem>();
            this.OrderItems.ListChanging += new ListChangingEventHandler<OrderItem>(OrderItems_ListChanging);
        }

        virtual public List<OrderItem> OrderItems { get; internal set; }

        void OrderItems_ListChanging(object sender, IOperationEventArgs<OrderItem> e)
        {
            switch (e.Action)
            {
                case ListChangingType.Inserting:
                case ListChangingType.Setting:
                    if (validationPasses)
                    {
                        e.Item.Parent = this;
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                    break;

                case ListChangingType.Removing:
                    if (validationPasses)
                    {
                        e.Item.Parent = null;
                    }
                    else
                    {
                        e.Cancel = true;
                    } 
                    break;
            }
        }

    }

Background: I’m writing a set of general purpose interfaces/classes that represent the core components of DDD, and I’m making the source code available (hence the need to create friendly interfaces).

This question is about making the interface as cohesive as possible, so that consumers can derive and implement their own collections without losing the core semantics.

PS: Please don’t suggest using AddXYZ() and RemoveXYZ() methods for each list, because I’ve already discounted that idea.

PPS: I must include developers using .NET 2.0 🙂


Related question.

  • 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-12T22:14:14+00:00Added an answer on May 12, 2026 at 10:14 pm

    I would suggest creating something that parallels the ObservableCollection<T> where appropriate. Specifically, I would suggest following the existing techniques for notification of change of collection. Something like:

    class MyObservableCollection<T> 
        : INotifyPropertyChanging,   // Already exists
          INotifyPropertyChanged,    // Already exists
          INotifyCollectionChanging, // You'll have to create this (based on INotifyCollectionChanged)
          INotifyCollectionChanged   // Already exists
    { }
    

    This will follow established patterns so that clients are already familiar with the exposed interfaces– three of the interfaces already exist. The use of existing interfaces will also allow more proper interaction with other already existing .NET technologies, such as WPF (which binds against the INotifyPropertyChanged and INotifyCollectionChanged interfaces.)

    I would expect the INotifyCollectionChanged interface to look something like:

    public interface INotifyCollectionChanged
    {
        event CollectionChangingEventHandler CollectionChanging;
    }
    
    public delegate void CollectionChangingEventHandler(
        object source, 
        CollectionChangingEventArgs e
    );
    
    /// <remarks>  This should parallel CollectionChangedEventArgs.  the same
    /// information should be passed to that event. </remarks>
    public class CollectionChangingEventArgs : EventArgs
    {
        // appropriate .ctors here
    
        public NotifyCollectionChangedAction Action { get; private set; }
    
        public IList NewItems { get; private set; }
    
        public int NewStartingIndex { get; private set; }
    
        public IList OldItems { get; private set; }
    
        public int OldStartingIndex { get; private set; }
    }
    

    If you wish to add cancellation support, simply add a writable bool Cancel property to CollectionChangingEventArgs that the collection will read to determine whether to execute the change that’s about to occur.

    I suppose this falls under your Option 2. This is the way to go because, to interoperate properly with other .net technologies that monitor changing collections, you’re going to have to implement it anyway for INotifyCollectionChanged. This will definitely follow the policy of “Least Surprise” in your interface.

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

Sidebar

Related Questions

Although I specify a ViewFields element in my sharepoint list service's GetListItems query, all
Although some questions similar to this one have been asked before, this one is
Although I know that there are more idomatic ways of doing this, why doesn't
Although I'm programming in C++, this is more of a general question about design.
Although I couldn't find anything on it, I thought I would double check -
Although this is pretty basic, I can't find a similar question, so please link
Although this is a very general question, here's my specific example so you can
I got a question and although I could find related information, I'm if it
I have two DataGridViews that have the same column schema (although two different DataViews
Can't seem to find the following information although I'm pretty sure this should be

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.