How to: Share Code through Source Files
[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]
This topic describes how to share code, and so functionality, between the middle tier and the presentation tier of a WCF RIA Services application using source files. When you change the source file in the middle tier and rebuild your application, the updated code is automatically synchronized in the client project. You put code in a shared source file when the code does not belong in a domain service or an entity class, and you do not want the code to go through the client code generation process.
You can share source files by either using a shared naming convention or by file links. This topic describes how to share source files by using both of these approaches. For information about the advantages and disadvantages of both approaches, see the Shared Code topic.
This topic assumes that you have already created a RIA Services solution. For a description of the procedures for doing this, see the Walkthrough: Creating a RIA Services Solution topic.
To share a source file by using a shared naming convention
In Solution Explorer for a middle-tier project, create any directory structure that you want to contain shared code files.
The directory structure will be re-created in the client project under the Generated_Code folder.
Add a C# or Visual Basic code file to the directory structure.
The file can exist anywhere in the directory structure of the middle-tier project.
Depending on the language of the project, name the class by using the shared naming convention: *.shared.cs or *.shared.vb.
For example, ExampleCode.shared.cs or ExampleCode.shared.vb.
Add code to the shared code file.
Build (Ctrl+Shjift+B) the solution.
In the client project, open the Generated_Code folder and notice that the code file has been copied.
At the top of Solution Explorer, you might have to click the Show All Files option in the client project to see the Generated_Code folder.
Open the code file and notice that no changes have been made to the code during compilation.
To share a source file by using file links
In the middle-tier project, add a new code file.
Add code to the code file.
In the presentation-tier project, right-click the project, select Add and then select Existing Item.
Select the code file that you added to the middle tier project.
On the Add button, click the down array and select Add As Link.
When you compile the project, you will see that the file has not been copied to the client project. Instead, a reference was added to the file.