Components
File: Read-Only Collections & Dictionary
- Uploaded:
- October.04.09
- Modified:
- October.04.09
- File Size:
- 8 KB
- Downloads:
- 597
- Version
- 1.0
Read-Only wrappers for IList<>,
ICollection<>
and ICollection<>.
Analogous to the ReadOnlyDictionary<>
class. All wrappers fully implement all relevant generic and non-generic collection interfaces.
Full unit test coverage for every single method.
Details
The .NET Framework provides several collection classes in the System.Collections.Generic
namespace as well as some base classes for you to implement your own collection classes in the
System.Collections.ObjectModel namespace. Between these also is a read-only wrapper
for the ICollection<> interface. This read-only wrapper allows you to publish a
collection of items from an object without allowing its users to modify that collection (even though
the object itself can modify the collection).
Quite useful, for example, if a list just represents an internal state that must not be modified.
But then you find out that the ReadOnlyCollection<> class actually wraps an
IList<>, not an ICollection<>, requiring you to support
index-based access any time you want expose a collection. And there's no class at all for wrapping a
Dictionary<>. So now you could do a quick hack job, writing your own read-only
wrapper for IDictionary<> or ICollection<> with just the handful
of methods you use in your project.
Or use mine! I've created three wrappers, ReadOnlyList<> for indexable collections
(which is what .NET's ReadOnlyCollection<> should have been called in my humble
opinion), ReadOnlyCollection<> for non-indexable collections and
ReadOnlyDictionary<> for associative collections. All three fully implement the
relevant .NET collection interfaces in their generic and older non-generic form. And they've got
100% unit test coverage, so you can be sure there are no nasty surprises in any of the methods.
Example
Features
-
Individual read-only wrappers for
IList<>,ICollection<>andIDictionary<> - Wrappers fully implement all relevant generic and non-generic collection interfaces
- 100% unit test coverage for all code
- No dependencies, all three wrappers can be plugged into any project
- Default interface also omits all methods that modify the collection
These classes are part of the Nuclex.Support library from the Nuclex Framework. You can
find the most recent release of the code on the framework's CodePlex site:
http://nuclexframework.codeplex.com/