Understanding the N-layer Silverlight application-Microsoft. NET Ria services framework White Paper)

Source: Internet
Author: User

(ArticleThis is a white paper published by Microsoft's ria services framework in July. It reads and translates without polishing. The statements are not fluent and reasonable, but do not affect understanding, this gives you a brief understanding of the Framework)
 

Understand n-layer Silverlight applications Program

Microsoft. NET Ria services simplifies the traditional multi-layer application mode by combining Asp.net and Silverlight. Use queries, updates, custom methods, and service operations to apply the logicCodeIn the middle layer, the. NET Ria services feature automatically generates corresponding proxy classes on the client to call these operations. because both the middle layer and the client are based on the same CLR, developers can write code to run on both ends.

This document explains key concepts by building a simple multi-tier application.

 

1.1 create a project

Create a Silverlight web application project to start a. Net Ria Services Multi-tier application. File | new project [1]:

Starting from the preceding dialog box, if. Net Ria services has been installed, you can see the highlighted check boxes in the following dialog box:

[1] You can use add new project to add a Silverlight application to an existing web application.

By default, this check box is not selected. To use. Net Ria services to create a multi-tier application, you must manually select it.

The solution result is as follows:

 

At this point, we have established a solution consisting of two projects:

1.Silverlightapplication1-This project includes the Silverlight code. We use this as the client project, that is, our client layer.

2.Silverlightapplication1.web-This project is an Asp.net web application code, a server project, and a middle layer.

The main difference between the Enable. Net Ria services selection box and the standard Silverlight application is that the selection box establishes a connection between the server project and the client project, allowing logical sharing. For more information, see the following section.

1.2 convert an existing web application (converting an existing Web Application)

As mentioned above, you can change whether to establish an "RIA link" later, as shown in:

1.3 publish data from the middle layer (exposing data from the mid-tier)

To explain the new features, we need to prepare some effective data on the intermediate layer.

1.4 access data from the middle layer

In this example, the middle layer needs to use data in the database. The database is not required by. Net Ria services. The client does not care about the data access count and its structure.

When using the database in the middle layer, we use the Entity Framework to access the adventureworks sample database. we add a new project on the server project and select the data category:

Select two tables from the database for the Application

 

Select two tables from the database for the Application

 

1.5 create a domainservice on the middle layer

Now we have a key step to demonstrate the features of the. NET Ria services framework. CreateDomainservice. A domainservice is used to publish objects and operations of a specific data domain. It is also the place where developers add application logic .. Net Ria services framework includes the Pipeline Code (plumbing code) so that the entities and Operations declared in the domainservice class can be accessed at other layers.

We use a template to create the domainservice (Add a new project/Web category/domain service) of the sample, and set the domainservice name as productservice:

The template requests a wizard to help build the domainservice class. In this example, we select the model we just created and publish an object: product. If "enabled editing" is not selected, the product entity is read-only.

(Note: The following list only appears after a build)

 

In the confirmation dialog box, the new domainservice is created in the selected project. The generated code is as follows:

 

Note:

-This class is inherited from linqtoentitiesdomainservice (base class built in. Net Ria services framework) [1].

-This generic base class is associated with our previously established adventureworks_dataentities.

-The [enableclientaccess] feature of the domainservice class indicates that it is visible to the Client layer.

-Generate a unique getproduct () query method to publish the product entity.

-Getpruduct () implements code to get data from the context of the adventureworks_dataentities object

This new domainservice class is where we add the application logic. For example, you can modify the getproduct () method to select only products in stock. Or you can add a parameter to specify the product category.

The most important thing is that the data in the middle layer provides a public interface. The code generated by the Wizard is only a starting point. We need to add the application logic here.

Second, in fact, we can run the application without any modification. The product entity is visible on both the client and the middle layer. Next we will introduce this mechanism as possible.

[1] the. NET Ria services framework has base classes for the Entity Framework and LINQ to SQL. It also has a technology-agnostic domainservice base class.

1.6 access data from the Silverlight Client

Will you remember the "RIA link" when we created the project? Now it works.

If we compile the solution and click show all files on the client project, we can see:

 

Note that the client project has a folder named generated_code that contains the generated file. The screenshot above shows several lines of code for this file.

The generated file contains classes called client proxy, which are generated by the domainservice class of the server.

This mechanism will be introduced in the section "Understanding Silverlight client code generation. We need to realize that:

· These proxy classes will be re-generated as long as the domainservice class on the server is modified or the client or solution is re-compiled, so do not modify these classes because they will be overwritten.

· Create an independent proxy class for each domainservice class. This example is the product entity.

· A generated domaincontext class is used to provide the client to access the published operations of each domainservice class. In this example, a productcontext class is generated and a getproductquery () method is published as a domaincontext. the parameter of the load () method requests the getproduct query method of the productservice domainservice class of the server. For details, see "How to query entities ."

1.7 use data on the Silverlight Client

Every domainservice class marked with the [enableclientaccess] custom feature on the server generates a domaincontex class in the file generated by the client. The Silverlight client can instantiate and interact with these domaincontext classes. The following example shows how to display product information in a grid:

Add a DataGrid to the client project as follows:

 

 

The generated domaincontext is instantiated in the back-end code file, and the data is retrieved from the server and bound to the DataGrid:

 

 

Run the application as follows:

 

 

Conclusion 1.8

This topic describes the key concepts used to build a multi-layer Silverlight application using the. NET Ria serives framework:

· A standard Silverlight web application can establish a multi-tier Application between a "RIA link" and a web application. [1].

· The most simplified form of this multi-presentation application is composed of two independent projects in a single solution. One is the Asp.net server project (intermediate layer), and the other is the Silverlight client project (client)

· A domainservice class on the server publishes a series of entities and operations, in which developers can customize applications at multiple levels.

· When the solution is compiled, the client proxy class for the entities and Operations published by the service end domainservice is automatically generated and added to the Silverlight client project.

· The Silverlight client code interacts with the generated domaincontext to obtain or operate the corresponding entity.

· Client and server operations have a set of entity types, making it possible to share business logic and verification rules across layers.

[1] select the Enable. Net Ria services check box during project Creation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.