J
jehugaleahsa
Hello:
The "gurus" out there suggests being very careful about how your
expose collections in your interface. If you do not intend the users
of your class to alter a collection, you must make sure that they
can't.
Here are some common implementations for returning a collection and
some of their pros and cons. Take a look and tell me what you
typically do and which one you think is the best practice.
1) Return the collection directly - Directly expose the collection in
the interface. If the collection is created in the property or method,
altering the collection won't likely affect the class. If the
collection is a member of the class, changes to it could invalidate
the state of the class. Future versions of the interface will be
required to expose the functionality provided by the collection (to be
backward compatible).
2) Return a copy of the collection - Expose a copy of the collection.
The copy would provide all of the same functionality as the
collection, but changes to it couldn't affect the state of the class.
This could lead to a lot of overhead if the collections are large or
complex. Your interface will be forced to expose the collection in
future versions.
3) Return a read-only wrapper around the collection - Expose a read-
only collection. This prevents modification. It has minimal overhead.
However, modifications to the collection may cause runtime errors.
You're still exposing it in your interface.
4) Return the collection through an interface - Expose the collection,
but through one of its interfaces. This would be like exposing List<T>
as IEnumerable<T>. This approach is vulnerable to users casting. Do
you expose List<T> as IList<T>, IEnumerable, etc?
5) Implement a custom collection - Expose the collection, but through
a custom collection with a constrained interface. This can eliminate
generics. The collection could inherit Collection<T>, List<T>, etc.
This is under the assumption that I actually want to expose a
collection. I'm not too concerned with the logistics behind
determining when to expose a collection, I just want to know how.
My approach, that has been evolving for a while now, has been to
return the collection through a restricted interface. I just have a
policy that says I can't cast from IEnumerable<T> to List<T>. If I
want to work with a List<T>, I have create a new one and add the
members via the ctor or the AddRange method.
I realize that this has costs associated with it. Generally, it has
not been an issue.
There are times where I won't even follow this policy. Some times I
don't think exposing a List<T> is such a big deal. Some times when the
collection is part of the class's state, I will create a copy to
mitigate a cast being performed. It kind of bothers me that I switch
from time to time.
I'm more curious what strategies others have come to practice on a
day-to-day basis. I'd like to hear as a community what seems to be the
trend.
The "gurus" out there suggests being very careful about how your
expose collections in your interface. If you do not intend the users
of your class to alter a collection, you must make sure that they
can't.
Here are some common implementations for returning a collection and
some of their pros and cons. Take a look and tell me what you
typically do and which one you think is the best practice.
1) Return the collection directly - Directly expose the collection in
the interface. If the collection is created in the property or method,
altering the collection won't likely affect the class. If the
collection is a member of the class, changes to it could invalidate
the state of the class. Future versions of the interface will be
required to expose the functionality provided by the collection (to be
backward compatible).
2) Return a copy of the collection - Expose a copy of the collection.
The copy would provide all of the same functionality as the
collection, but changes to it couldn't affect the state of the class.
This could lead to a lot of overhead if the collections are large or
complex. Your interface will be forced to expose the collection in
future versions.
3) Return a read-only wrapper around the collection - Expose a read-
only collection. This prevents modification. It has minimal overhead.
However, modifications to the collection may cause runtime errors.
You're still exposing it in your interface.
4) Return the collection through an interface - Expose the collection,
but through one of its interfaces. This would be like exposing List<T>
as IEnumerable<T>. This approach is vulnerable to users casting. Do
you expose List<T> as IList<T>, IEnumerable, etc?
5) Implement a custom collection - Expose the collection, but through
a custom collection with a constrained interface. This can eliminate
generics. The collection could inherit Collection<T>, List<T>, etc.
This is under the assumption that I actually want to expose a
collection. I'm not too concerned with the logistics behind
determining when to expose a collection, I just want to know how.
My approach, that has been evolving for a while now, has been to
return the collection through a restricted interface. I just have a
policy that says I can't cast from IEnumerable<T> to List<T>. If I
want to work with a List<T>, I have create a new one and add the
members via the ctor or the AddRange method.
I realize that this has costs associated with it. Generally, it has
not been an issue.
There are times where I won't even follow this policy. Some times I
don't think exposing a List<T> is such a big deal. Some times when the
collection is part of the class's state, I will create a copy to
mitigate a cast being performed. It kind of bothers me that I switch
from time to time.
I'm more curious what strategies others have come to practice on a
day-to-day basis. I'd like to hear as a community what seems to be the
trend.