Thursday, September 13, 2012

.NET 4.5 Portable class libraries : Is it possible to determine the platform at runtime?

.NET 4.5 Portable class libraries : Is it possible to determine the platform at runtime?

I've got a set of libraries that target Silverlight & Windows Phone 7.5, In my libraries I have a navigation solution that has a common interface but different implementations - depending on the platform. (SL & WP7 Navigation are very similar, there are very minor differences, unfortunately they can't be avoided)

If I were to write a portable class library that would contain my INavigationModel interface + both implementations, is there any way to tell which platform is calling the portable class library? This information would help me choose the correct implementation of my solution at runtime.

Cheers

edit Any alternative solution is welcome too, I'm still trying to piece any information about the portable class libraries to see what they're capable of

Answers & Comments...

Answer: 1

Realistically, that may not work so well. Even if you could reliably determine the environment you're running under (some kind of hack with Environment.OperatingSystem perhaps), if your implementation is of any complexity, it's unlikely you could stick completely to the lowest-common-denominator of defined types/methods, etc.

The whole point of portable assemblies is to define truly shared bits (utility methods, interfaces, etc)- probably a better solution might be to define base classes/interfaces with shared functionality in the portable assembly, then extend in your platform-specific assemblies to implement the non-portable bits.

by : Matt Davishttp://stackoverflow.com/users/139652

Answer: 2

There's not a good way to do what you want to do. Portable libraries do not remove the need to have platform-specific projects, it just helps facilitate platform-specific projects to talk in a platform-agnostic way.

I would tend to recommend that the concrete implementation of the INavigationService live in the platform specific projects themselves, whereas the common abstraction live in the portable project. Then use an IoC container or ServiceLocator-like framework to hook them up (I'd recommend Autofac). I've done similar in an article (in fact, I did this for a navigation service) I wrote for MSDN: Create a Continuous Client Using Portable Class Libraries. Be sure to download the source code to see a concrete example.

by : David Keanhttp://stackoverflow.com/users/1218765




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog