views:

177

answers:

3

If I want to define a class in a common assembly, to be utilized by both a silverlight application and an asp.net web application or other type of app, can I define the data type in a standard class library or should I use a silverlight class library?

+1  A: 

You cannot reference a standard .NET library from a Silverlight application. You can theoretically reference a Silverlight library in an ASP.NET application, althought I've has trouble with assemblies that used UI types.

I would recommend just setting up a web service that defines the data types in the service contract. When both the ASP.NET and Silverlight application add the service reference, the types will be reused/automatically generated for you. It's simpler, better supported, and it lets you change one piece of code instead of two.

Jacob Adams
+1  A: 

Take a look at RIA Services, it was written to solve this class of problem. It lets you define and annotate (e.g. add validation requirements) to server side code, and it automatically builds out a proxy class in your Silverlight class that can communicate back to your server-side code.

It can work with plain CLR objects as well as LINQ to SQL and Entity Framework models.

Jon Galloway
+1  A: 

A Silverlight application can only use Silverlight assemblies. The main reason for that is to make sure that all references can be resolved.

One option for what you are asking is to define the types within a standard class library, so that it can be used in ASP.NET and "link" those files to a Silverlight Class Library. Linking works ok, but you may find yourself having to do lots of manual work to make sure everything is in sync. The advantage of linking is that you can share logic, as long as you don't use anything that the Silverlight CLR can not deal with.

RIA Services is an option too, if you don't mind the fact that it's still CTP. :-) It is evolving quickly and quality is pretty good for a CTP. There is a learning curve and you will have to adapt to the way it works.

Klinger
The key to Klingers point is that so long as your 'shared' assembly does not contain references to non-silverlight libraries, you can simply link them and effectivly 'utilize' them by both a silverlight application and an asp.net web application.
PortageMonkey