Question

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!

Was it helpful?

Solution

You can add Code Contracts to a class library without introducing a dependency on Code Contracts for any other assembly that uses that class library.

The Code Contracts are implemented by a re-writer that changes the code that specifies the code contracts. The rest of the Code Contracts support is already baked in to .Net 4 or later, so other code can happily reference the library that uses Code Contracts without having to take any special steps.

Therefore you can use Code Contracts in some assemblies without worrying about anything else that references those assemblies.

Specifically, this statement is not true:

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.

because you can reuse it in "other systems" (if by "other systems" you mean "assemblies that do not use Code Contracts").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top