Middle Tier

[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 section provides information and guidance on the using domain services, shared code and data entities within the middle tier of a WCF RIA Services application. In a 3-tier application, the middle tier contains the logic for managing the interaction between the presentation tier and the data tier. You apply business rules and validation checks in the middle tier to make sure that the data is in an acceptable state. For example, in a human resources application, you may provide a user interface that enables employees to submit a vacation request, but you have to make sure that an employee's vacation balance is never less than zero. Therefore, you add logic in the middle tier to check the employee’s vacation balance before submitting a vacation request.

To create the best use experience in a RIA Services client, such as a Silverlight application, you often want to enforce the same business rules on the client that you would enforce on the server. This requires that the middle-tier code on the client and server be synchronized. RIA Services enables you to use the .NET Framework to write application logic in the middle tier. RIA Services automatically generates code for the presentation tier from the middle-tier code, so those tiers are always synchronized. This topic and its subsidiary topics describe how to use domain services, shared code, and data entities to build the middle tier.

Data Access Layer

You can work with any type of data access layer when you develop the middle tier by using RIA Services. For example, you can connect to the following data access layers:

  • Entity Data Model.

  • LINQ to SQL object model (This is an option only if you installed the RIA Services Toolkit).

  • Common language runtime (CLR) object.

  • Web service that exposes data from its source.

You can apply validation checks to your data model to enforce restrictions on data values submitted from the presentation tier.

In some cases, you have to interact with data that exists in more than one table. RIA Services provides a programming framework that supports modifying hierarchical data models (such as an Order and OrderDetails relationship), inheritance data models (such as Parent and Child relationship), and data projection models (such as de-normalizing data by pulling values from Customer and Address tables into a single data model). For more information, see Data.

In your application, you may need to display data from a variety of data sources or expose a single entity to more than one domain service. WCF RIA Services enables this scenario by supporting references between entities from different DomainContext types. For more information, see Walkthrough: Sharing Entities between Multiple Domain Services.

By default, RIA Services does not pass the entire original entity along with the changed values and to the data access layer to check for data concurrency. Instead, RIA Services stores and passes back only those members that are marked with the RoundtripOriginalAttribute attribute, the ConcurrencyCheck attribute, or the TimeStamp attribute. For more information, see Data.

Domain Services

A domain service is the public abstraction of the business logic for a domain. It includes the entities and operations that comprise the domain business logic. RIA Services provides the DomainService class as the base class for all classes that serve as an interface to the business logic in the data layer. When you implement a domain service, you specify the entity classes that you want to expose for access from the client. You also specify the data operations that are permitted through the domain service, and you can add application logic to the domain service. For each domain service that you enable for client access, RIA Services will generate a DomainContext class for the client application. For more information, see Domain Services.

For information about securing your domain service, see Building Secure Applications with WCF RIA Services.

Shared Code

In some scenarios, you have to add code that is shared between the middle-tier project and the presentation-tier project, but you do not want to put that code in the domain service or an entity class. For example, you may have to reference a library that contains application logic, which is used in many other projects. Or, you may have to create customized properties that combine or change values from the entity class, such as a display name in the format "last name, first letter of first name". RIA Services enables you to include classes or assemblies in the middle tier that can be accessed without changes to the client project. For more information, see the Shared Code topic.

Client Code Generation

When you link a middle-tier project and a presentation-tier project by using RIA Services, the RIA Services framework automatically generates code for the client from the middle tier. Client code is generated for the following middle-tier code:

  • Each domain service that is annotated with the EnableClientAccessAttribute attribute.

  • Each entity class that is referenced by a domain service.

  • Code files that are named with the shared naming convention (*.shared.cs or *.shared.vb) are copied without modification to the client project.

This middle-tier code is available to the client through client proxy classes. When the code on the middle tier changes, the client proxy classes are automatically re-generated so that the presentation tier is always synchronized with the middle tier.

For more information, see Client Code Generation.