C# – Feasibility of shared code between .NET and Silverlight

cnetsharingsilverlight

Having just gone through a small experimenting session to try to see how much work it would take to bring our .NET class library, or at least portions of it, into Silverlight so that we can reuse business logic between the two worlds, I'm wondering if others have experience with this sort of thing.

The things I noticed, off the top of my head:

  • Lots of attributes missing (Browsable(false) for instance)
  • Lots of interfaces missing, or present, but empty (ICloneable is hidden, ITypedList missing)
  • Reflection differences (everything reachable needs to be public)
  • Some base class differences (no Component?)

So I'm wondering, is it really feasible for me to even look at this as a possibility?

I got the initial code running, but I had to just comment out a whole lot of the base functionality, mostly around handling lists since they are based on ITypedList and some base classes. Apparently I need to change to ObservableCollection in Silverlight, so a whole of of base-code needs to be changed in order to cope.

The actual business test class I created is 99.5% identical to the one I would've made for .NET, only some minor changes that would easily be usable in .NET as well, just not as I would've made it before looking at Silverlight. In other words, it looks feasible to share business logic, provided I can make the base classes compatible.

Just so I'm clear, what I'm talking about is that I would basically have two project files, one for .NET, and one for Silverlight, but the actual C# source code would be the same, shared between the two.

So does anyone have any experience with this? Any tips or guidelines?

Will it be worth it? It certainly warrants more looking into.

Best Answer

It is definitely feasible.

It's done on a project here; the Silverlight project includes the C# ones, and there are some #IF statements handling some things (like log4net declarations), and other times things are just re-implemented. But in general, it's a huge win, and you should definitely attempt it (and certainly, we have, successfully).

-- Edit:

One point though, is that our OR/M (LLBLGen) didn't have inbuilt support for 'simple' objects to send down through Silverlight; but someone had written a plugin that handled it, which helped. So it may be worth considering what sort of DAL you're using, and how well it supports Silverlight.