Update 2010-02-08: Jonathan Pryor has merged many of my extension methods into [Cadenza][]. I'd strongly suggest checking it out.
It's no secret to my friends that I love to program... even more so as
I've been developing a [FarmVille][] client in C# and having them test
it. (As much as you might hate FarmVille, you must agree that there's a
certain awesome factor in LINQ-to-FarmVille:
service.Plow(from i in service.World.Objects.OfType<Plot>() where i.State == "fallow" || i.State == "withered" select i
)
Well, among this project and others I consistently find myself writing the same code over and over. I know of many programmers who have developed personal toolkits for the languages they use frequently, but for some reason I've been programming for about 13-15 years now and haven't ever built my own library. This application gave me an excuse to do so, and so I've started on the Cdh.Toolkit suite of libraries.
Here is a summary of the classes available:
-
**Cdh.Toolkit.Collections**: Some useful collection types, all designed
to be derived.
- **ReadOnlyCollection\
**, **ReadOnlyDictionary\ **, and **ReadOnlyList\ **: Wrappers around the corresponding interface types ICollection\ , IDictionary\ , and IList\ , throwing exceptions on all write attempts. While there is a ReadOnlyCollection\ as part of the .NET framework, it is not designed to be derived, and the other two classes do not have a counterpart at all. - **SynchronizedCollection\ **, **SynchronizedDictionary\ **, and **SynchronizedList\ **: Wrappers around the corresponding interfaces. All accesses are synchronized against a ReaderWriterLockSlim, allowing for multiple concurrent read operations. The enumeration behavior can be specified as either lock, which holds a read lock for the duration of the enumeration, or copy, which creates a copy of the collection and enumerates it instead. - **ObservableCollection\ **: A collection that fires events when modified. ObservableDictionary and ObservableList are currently not provided, due to some implementation complexities. However, the interfaces **IObservableCollection\ **, **IObservableDictionary\ **, and **IObservableList\ ** and some EventArgs classes are provided to allow developers to implement their own observable collections easily. - **ObservableHashSet\ **: An observable and API-compatible wrapper around HashSet\ . - **ReadOnlyObservableCollection\ **, **ReadOnlyObservableDictionary\ **, and **ReadOnlyObservableList\ **: Wrappers around the IObservable\* interfaces mentioned above. Events from the wrapped collections are forwarded. This allows one to have a read only observable collection without sacrificing the IObservable\* interface, which would happen if such a collection were wrapped in one of the normal read only classes listed above. -
**Cdh.Toolkit.Extensions**: Extension libraries designed to ease the use
of many classes in the .NET framework.
- **Collections**: Extensions specific to collection classes.
- **TValue IDictionary\
.GetOrDefault(TKey key)**: Returns default(TValue) if the key is not present in the dictionary. - **TValue IDictionary\ .GetOrValue(TKey key, TValue fallback)**: Returns fallback if the key is not present in the dictionary. - **ComponentModel**: Extensions specific to the System.ComponentModel namespace. - **void ISynchronizeInvoke.AutoInvoke(Action action)**: Executes the action delegate on the ISynchronizeInvoke object if required, otherwise does so on the current thread. - **object ISynchronizeInvoke.AutoInvoke(Delegate method, params object[] args)**: Executes the delegate on the ISynchronizeInvoke object if required, otherwise does so on the current thread, and returns the return value of that method in either case. - **AsyncCallback AsyncCallback.Invoked(ISynchronizeInvoke obj)**: Returns a wrapper around the AsyncCallback delegate that will invoke it using the AutoInvoke extension above. Useful for async callbacks that need to operate on a Winforms GUI. - **Enumerable**: Extensions to enumerable objects. - **IEnumerable\ IEnumerable\ .NotNull() where T : struct**: Returns all values from the non-null nullable objects in the sequence. - **void IEnumerable\ .Walk()**: Enumerates the sequence, discarding all values obtained. Useful as an alternative to .ToList() when you need to make sure that a query executes, but do not need to use the result. - **void IEnumerable\ .CopyInto(IList\ list)**: Copies a sequence into a list. - **Events**: Extensions that make writing event logic easier. All of these extensions return if the event handler in question is null, making event-firing code simpler and easier to read. - **void EventHandler.Fire(object sender)**: Uses EventArgs.Empty as the event arguments. - **void EventHandler.Fire(object sender, EventArgs args)** - **void EventHandler.Fire(object sender, Func\ argsFactory)**: Calls the factory function only if the event handler is not null. Useful when construction of the event arguments can take a long time. - **void EventHandler\ .Fire(object sender, T args)** - **void EventHandler\ .Fire(object sender, Func\ argsFactory)**: Calls the factory function only if the event handler is not null. Useful when construction of the event arguments can take a long time. - **ReaderWriterLockSlim**: Allows these kind of locks to be used in a using() block, which makes code easier to read and maintain. They will also return a no-op IDisposable if a compatible lock is already held by the current thread, which makes non-recursive lock objects simpler to code with. (The return type is actually a value type that implements IDisposable, which means that usage of these methods does not incur any object allocation overhead.) - **IDisposable ReaderWriterLockSlim.Read()**: Returns an IDisposable that will release the read lock when disposed. This method returns a no-op IDisposable instead if the current thread already holds a read, upgradeable read, or write lock. - **IDisposable ReaderWriterLockSlim.UpgradeableRead()**: Returns an IDisposable that will release the upgradeable read lock when disposed. This method returns a no-op IDisposable instead if the current thread already holds an upgradeable read or write lock. - **IDisposable ReaderWriterLockSlim.Write()**: Returns an IDisposable that will release the write lock when disposed. This method returns a no-op IDisposable instead if the current thread already holds a write lock. - **Reflection** - **T ICustomAttributeProvider.GetCustomAttribute\ (bool inherit) where T : Attribute**: Returns a typed attribute, or null if there is no attribute of type T. - **IEnumerable\ ICustomAttributeProvider.GetCustomAttributes\ (bool inherit) where T : Attribute**: Returns a sequence of attributes of type T present on the attribute provider. - **Reflection.Emit** - **void ILGenerator.EmitTypeOf(Type type)**: Emits the IL sequence that will leave the same Type object on the execution stack. The amount of code is slim, but I've found at least one of the classes or extensions invaluable in every project I've worked on since starting the toolkit. It's an interesting case where coding for a game actually winds up improving my productivity working on other software too. Eventually these libraries will be released under the MIT license, so stay tuned for another blog post with a link to the Git repository. (And yes, the above list will be converted into real documentation. Someday.) [Cadenza]: http://gitorious.org/cadenza [FarmVille]: http://www.farmville.com
Comments