How can I intercept when an item is added or removed from a List<ofType>?

D

daokfella

I have a business object that exposes a collection of other objects
via a List<of Type>. How can I intercept when an item is either added
or removed from this list. Is it possible?

private List<Permission> _Permissions;

public List<Permission2> Permissions
{
get
{
if (_Permissions == null)
{
_Permissions = new List<Permission>();
}
return _Permissions;
}
}

The consumer of this can now say
myobject.Permissions.Add(newPermission). I'd like to be able to run
some code when this happens. What's the best way?

Thanks,

Jason
 
A

Arne Vajhøj

daokfella said:
I have a business object that exposes a collection of other objects
via a List<of Type>. How can I intercept when an item is either added
or removed from this list. Is it possible?

private List<Permission> _Permissions;

public List<Permission2> Permissions
{
get
{
if (_Permissions == null)
{
_Permissions = new List<Permission>();
}
return _Permissions;
}
}

The consumer of this can now say
myobject.Permissions.Add(newPermission). I'd like to be able to run
some code when this happens. What's the best way?

The obvious solution would be not to expose the entire list
as a property but instead expose add and remove methods that
modified the list and did whatever you want them do.

Arne
 
D

daokfella

Yes, I thought of that originally, but consumers of the object need to
have access to the list. I'm thinking of just creating a custom class
that inherits from IList that will raise events during add and remove.
 
M

Marcin Hoppe

daokfella pisze:
Yes, I thought of that originally, but consumers of the object need to
have access to the list.

Why exactly do they need the list for? If it is sequential access, you
can expose IEnumerable member. If it is some kind of index lookup, you
can expose proper method that takes an index and returns proper item.

Best regards!
 
I

Ignacio Machin ( .NET/ C# MVP )

I have a business object that exposes a collection of other objects
via a List<of Type>. How can I intercept when an item is either added
or removed from this list. Is it possible?

private List<Permission> _Permissions;

public List<Permission2> Permissions
{
   get
   {
      if (_Permissions == null)
      {
         _Permissions = new List<Permission>();
      }
      return _Permissions;
   }

}

The consumer of this can now say
myobject.Permissions.Add(newPermission). I'd like to be able to run
some code when this happens. What's the best way?

Thanks,

Jason

Hi,

List<T> does not expose this feature, as a matter of fact in code
analysis you have a role because of this.

Take a look at this post: http://blogs.msdn.com/fxcop/archive/2006/04/27/585476.aspx
 
I

Ignacio Machin ( .NET/ C# MVP )

Yes, I thought of that originally, but consumers of the object need to
have access to the list. I'm thinking of just creating a custom class
that inherits from IList that will raise events during add and remove.

You expose the List<T> decorated with a ReadOnlyList

This pattern is also used when you have a type that only exist as part
of another type (like a Order / OrderLine relationship) you have a
public Order.Details property that return a readonly collection of
Details
 
D

Daniel Cigic

Take look at System.Collections.ObjectModel.ObservableCollection<T>, it
might be what you need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top