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

Re: How can I intercept when an item is added or removed from a List<of by arne

arne
Wed May 07 18:53:11 CDT 2008

daokfella wrote:
> 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

Re: How can I intercept when an item is added or removed from a by daokfella

daokfella
Wed May 07 21:03:15 CDT 2008

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.

Re: How can I intercept when an item is added or removed from a by parez

parez
Wed May 07 21:32:11 CDT 2008

On May 7, 10:03 pm, daokfella <jjbut...@hotmail.com> wrote:
> 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.

i think you can use the collection class..

http://dotnetcorner.weblog.com/2008/4/DoNotExposeGenericLists-error-and-Using-Collection.html

Re: How can I intercept when an item is added or removed from a List<of Type>? by Peter

Peter
Wed May 07 23:18:44 CDT 2008

On Wed, 07 May 2008 16:12:56 -0700, daokfella <jjbutera@hotmail.com> wrote:

> 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?

You can use BindingList<T> instead of List<T>. That class has a
ListChanged event that notifies you of changes to the list.

Pete

Re: How can I intercept when an item is added or removed from a by Marcin

Marcin
Thu May 08 01:22:47 CDT 2008

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!
--
Marcin Hoppe
Email: marcin.hoppe@gmail.com
Blog: http://devlicio.us/blogs/marcin_hoppe

Re: How can I intercept when an item is added or removed from a by Ignacio

Ignacio
Thu May 08 08:56:02 CDT 2008

On May 7, 7:12=A0pm, daokfella <jjbut...@hotmail.com> wrote:
> 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
> {
> =A0 =A0get
> =A0 =A0{
> =A0 =A0 =A0 if (_Permissions =3D=3D null)
> =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0_Permissions =3D new List<Permission>();
> =A0 =A0 =A0 }
> =A0 =A0 =A0 return _Permissions;
> =A0 =A0}
>
> }
>
> 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/585=
476.aspx

Re: How can I intercept when an item is added or removed from a by Ignacio

Ignacio
Thu May 08 08:58:27 CDT 2008

On May 7, 10:03=A0pm, daokfella <jjbut...@hotmail.com> wrote:
> 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

Re: How can I intercept when an item is added or removed from a List<of Type>? by Daniel

Daniel
Thu May 08 12:17:23 CDT 2008

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

"daokfella" <jjbutera@hotmail.com> wrote in message
news:e02deb31-2dc0-42f4-9198-78c22e597b65@a23g2000hsc.googlegroups.com...
> 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