Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Presentation Models

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

WCF RIA Services enables you to create data models that aggregate data from multiple entities in the data access layer that are known as a presentation model. You use this feature when you do not want to directly expose the entities in your data access layer to the client. When using a presentation model, you can respond to changes in the data access layer by changing only the presentation model and not the client. Also, you can simplify the client code by designing a model that aggregates only those fields that are relevant to users of the client. This topic describes how to create, query and update a presentation model and how to pass values back to the client when changes have been set in either middle-tier or in the data source.

Creating the Presentation Model

The database structure that is needed to maintain data integrity may be more complicated than what you need for the entities in your client application. You can create a presentation model that simplifies this data structure by combining the fields that are relevant to your application into one presentation model. For example, in the AdventureWorksLT sample database, you retrieve customer and address data through the Customer, CustomerAddress, and Address tables.


You create a presentation model by creating a class in the server project, and defining the properties you want to make available. The properties you define correspond to the properties you want to expose from the entities. For example, you can create the following CustomerPresentationModel class in the server project to present only the field you want from the Customer, CustomerAddress, and Address tables.

Querying and Modifying Values in the Presentation Model

After creating the presentation model, you expose it to the client project by adding a domain service that interacts with the presentation type. The entity values are only exposed through this domain service, and are not exposed through a domain service that exposes the whole entity. The following example shows a domain service that derives from the DomainService class.

public class CustomerDomainService : DomainService
    AdventureWorksLT_DataEntities context = new AdventureWorksLT_DataEntities();

To retrieve data, you add a query method to the domain service. In the query method, you retrieve the relevant data from the entities in the data access layer and set those values to the corresponding properties in a new instance of the presentation model. From the query method, you return either an instance of the presentation model type or an IQueryable’1 where the generic type is your CustomerPresentationModel type. The following example shows a query method for the customer presentation model.

Because the (Customer, CustomerAddress, and Address) entities in the data access layer are not exposed by the domain service, those types are not generated in the client project. Instead, only the CustomerPresentationModel type is generated in the client project.

To update data through the presentation model, you create an update method and define the logic for saving the values from the presentation model to the entities. An example of an update method is shown at the end of the next section.

Passing Values Back to the Client

After submitting changes, you may want to pass values back to the client that are set in either middle-tier logic or in the data source. RIA Services provides the Associate method to map values from the entity back to the presentation model. In this method, you provide a callback method that is called after the changes have been submitted. In the callback method, you assign any values to the presentation model that have been modified in the middle tier. You perform this step to ensure that the client possesses the current values.

The following example shows how to update values in the entities and how to map modified data back to the presentation model.

© 2015 Microsoft