Shared Code

[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date Please see Release Notes or Changelog for a list of changes since WCF RIA Services ]
Open Ria Services enables you to write application logic that is shared between the middle tier and the presentation tier and so provides identical functionality on both the server and the client. Code can be shared either with source files or assemblies.
Unlike the automatic code generation process described in the Client Code Generation topic, shared code is not altered during compilation. Instead, the code is either copied or shared verbatim between the tiers. Shared code allow you to define pieces of logic or partial class extensions for your entities that are defined once on the server, but that get code generated on the client as well, so that the same logic is available for use in both places.

Shared Source Files

You can add source files to the middle tier and then explicitly designate those files for sharing with the presentation tier. There are two approaches to sharing source files between the tiers. The first approach is to name the source files according to a shared naming convention: *.shared.cs (for C#) or *.shared.vb (for Visual Basic). The second approach is to use the linked files feature in Visual Studio.

Shared Naming Convention

When you use the shared naming convention (*.shared.cs or *.shared.vb) to share files, you implement a "push" model for sharing source code files. The shared files are actively copied from the middle-tier project to the client project during compilation. The shared naming convention works for sharing files only when a Open Ria Services link exists between the client and server projects.
Shared File
The shared naming convention has the following advantages:
Shared Naming Convention Advantages
Built-in support
No further action by the developer is required to keep the shared files synchronized.
The name clearly indicates the file is intended for sharing.
As new shared files are added, all client projects linked to the middle tier are automatically updated when the solution is compiled.
Easy-to-understand debugging experience
The developer can set breakpoints in the server or client versions of the file.
The shared naming convention has the following disadvantages:
Shared Naming Convention Disadvantages
New concept
The developer must be aware of the shared naming convention.
Files are copied
The shared files are physically copied to client projects, which means that a developer may accidentally edit the copied version and lose changes during the next compilation.
For more information, see How to: Share Code through Source Files.

Linked Files

Linked files is an existing feature of Visual Studio and is not unique to Open Ria Services. A Open Ria Services link between projects can exist, but it is not required to use linked files. When you use the linked files approach, you implement a "pull" model for sharing source code files. The client project does not contain a copy of the file. Instead, the client project just references the file in the server project.
Linked File
You can also link both the server and the client projects to a file in another project.
Linked Files
The linked file approach has the following advantages:
Linked Files Advantages
Existing Visual Studio feature
The developer does not have to learn a new convention.
File is not copied
The file only exists in the server project. Therefore, the developer cannot change a copied version of the client and lose those changes during the next compilation.
The linked file approach has the following disadvantages:
Linked Files Disadvantages
Requires explicit user action
The developer must link each shared file.
No self-maintenance
Each client project must be updated as shared files are added or removed.
Lacks transparency
The developer must examine the project file to determine which files are shared.
Awkward debugging experience
It is not easy to determine which tier prompted a breakpoint.
For more information, see How to: Share Code through Source Files.

Shared Assemblies

Instead of sharing source files between projects, you can compile code into a class library and then share the library through assembly references. You use the Open Ria Services class libraries to make sure that the assemblies are compatible, even when used with different frameworks (such as the .NET Framework version 4 and Silverlight 5).
The following diagram shows an n-tier application that uses Open Ria Services class libraries to share code. The middle tier and the client tier use assembly references to the class libraries.
Class Library Structure
For more information about Open Ria Services class libraries, see Creating Open Ria Services Solutions and Walkthrough: Creating a Open Ria Services Class Library.