# Shared Code

\[ **This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date** \
&#x20;Please see [Release Notes](https://github.com/OpenRIAServices/OpenRiaServices/releases) or [Changelog](https://github.com/OpenRIAServices/OpenRiaServices/blob/main/Changelog.md) 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](https://openriaservices.gitbook.io/openriaservices/ee707349/ee707359) 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](https://1585985709-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_5pS6LfA6GSEBmCbd9%2Fsync%2F5ade84032a802971c5ddaaabdd7e215e7551548c.png?generation=1620474361429901\&alt=media)

The shared naming convention has the following advantages:

| Shared Naming Convention Advantages     | Description                                                                                                                           |
| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| Built-in support                        | No further action by the developer is required to keep the shared files synchronized.                                                 |
| Transparent                             | The name clearly indicates the file is intended for sharing.                                                                          |
| Self-maintained                         | 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 | Description                                                                                                                                                                    |
| -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 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](https://openriaservices.gitbook.io/openriaservices/ee707348/ee707371/ee707369).

### 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](https://1585985709-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_5pS6LfA6GSEBmCbd9%2Fsync%2Fabb39c56c6745bcb8d018ea0d8ba9eb7fa358569.png?generation=1620474361486299\&alt=media)

You can also link both the server and the client projects to a file in another project.

![Linked Files](https://1585985709-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_5pS6LfA6GSEBmCbd9%2Fsync%2F92a3efede671e990bf1d6ce4d30a5baa1bf29173.png?generation=1620474361195323\&alt=media)

The linked file approach has the following advantages:

| Linked Files Advantages        | Description                                                                                                                                                           |
| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 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    | Description                                                                      |
| ----------------------------- | -------------------------------------------------------------------------------- |
| 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](https://openriaservices.gitbook.io/openriaservices/ee707348/ee707371/ee707369).

## 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](https://1585985709-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_5pS6LfA6GSEBmCbd9%2Fsync%2F134d9ee4afc55ce766333c4001d396a4b10f7e4e.png?generation=1620474361587482\&alt=media)

For more information about Open Ria Services class libraries, see [Creating Open Ria Services Solutions](https://openriaservices.gitbook.io/openriaservices/ee707336) and [Walkthrough: Creating a Open Ria Services Class Library](https://openriaservices.gitbook.io/openriaservices/ee707348/broken-reference).
