In my current system I decided to build some generic Extension methods to be reusable later inside the whole current system.
As I'm using Microsoft Code Contracts I decided firstly to include them to validate the source
and the range
.
1)
public static class MyExtensions {
public static void RemoveRange<T>(this ObservableCollection<T> source, IEnumerable<T> range) where T : class {
Contract.Requires(source != null);
Contract.Requires(range != null);
foreach (var element in range) {
source.Remove(element);
}
}
}
Another implementation manner is a classic way:
2)
public static class MyExtensions {
public static void RemoveRange<T>(this ObservableCollection<T> source, IEnumerable<T> range) where T : class {
if (source == null) {
throw new ArgumentNullException("source");
}
foreach (var element in range) {
source.Remove(element);
}
}
}
Until this moment I didn't had such requirements to design generic classes in a manner that can be reusable in another systems
EDIT (start)
(by another systems i mean concretly distinct systems, with different architecture. Example: the current systems if for financial sector, and it uses Code Contracts. But other system can be for military sector, which will not use Code Contracts for validation.)
EDIT (end)
I discussed with my coleagues how about reusability of that Generic class with Extension methods in another systems ?
I) An opinios is to keep Code Contracts because is is a rule of validation in whole current system. (as in first example).
The advantage of this way is that I keep one way of validation in whole system (only via Code Contracts). The disadvantage is that this Tested Generic Class with Extension methods will not be reusable in another systems (which possibly will not use Code Contracts for validation) without replacing Contracts with classic If/Throw Exception
.
II) Another opinion is to design Generic classes with Extension methods to be Reusable in another systems, even we didn't have until now such requirements. This opinion requires a classic implementation of validation, via IF and throw exception (as in second example).
The advantage of this way is that Tested Generic Classes with Extension Methods will be reusable without changes. But the disadvange is that another type of validation is approached.
Any suggestion/advice, concerning which approach to use, will be welcome.
Thank you for atention!