views:

72

answers:

1

I want to use the .NET 4.0 interface IObserver for a library that needs to support previous versions of the framework as well. I already have conditional compilation which lets me build for each of the framework versions.

I don't want to use the Rx Extensions' version of IObserver<T> as that would add an unnecessary dependency to an otherwise stand-alone assembly.

What I'm considering is adding this block of code to my library and my question to all of you is this:

1) I know this is a bad idea, but I'm trying to figure out exactly, "Why?"

#if !NET40
namespace System
{
    public interface IObserver<in T>
    {
        void OnCompleted();
        void OnError(Exception error);
        void OnNext(T value);
    }
}
#endif

I want to use the standard interface so that .NET 4.0 users can integrate in cool ways that I haven't even thought of yet. So I don't want to just duplicate the concept and loose the integration with other up and coming IObservable<T> usages.

The dangers that I see are if the .NET 3.5 build of my library is used in .NET 4.0 there could be type collisions. Ideally though, someone using v4.0 would use the 4.0 build of the library.

Are there any other things that I should be aware of with this approach?

2) Alternatively, I've considered doing this in my code instead (which is the direction I'm leaning) and would like to know people's thoughts on why this is a bad idea:

#if !NET40
namespace Foo
{
    public interface IObserverProxy<in T>
    {
        void OnCompleted();
        void OnError(Exception error);
        void OnNext(T value);
    }
}
#endif

And later where I want to use it:

#if NET40
using IObserverBar=System.IObserver<Bar>;
#else
using IObserverBar=Foo.IObserverProxy<Bar>;
#endif

Any thoughts?

+2  A: 

As the C# extension methods do not work well together with extern aliasing, I would advise you not to put the interface in the System namespace as it will break people who use both Rx and your library, I would put the interface in the Foo namespace. People who want to use your library and Rx as well can then always build an extension method like this:

static System.IObservable<T> ToSystemObservable<T>(this Foo.IObservable<T> source)

Also the Observable interfaces in Rx have been moved to a seperate dll System.Observable.dll for exactly this reason, I strongly encourage you to reconsider using those. This dll doesn't version from Rx build to Rx build so should not have versioning issues. This way anybody using your library can use any (recent) build of Rx that they want to do queries over your observable objects.

Jeffrey van Gogh
These are good points. I'm definitely not going to put it in the `System` namespace but was curious what breaks if I did. I still would prefer not to include external DLLs if I don't have to, as this isn't a core concept to my library, but it is nice to know they have isolated that aspect. Thanks!
McKAMEY