Part 2: Data Access
This exercise will lead us to access data with Silverlight. This exercise requires
(everything is free and will remain):
1 - VS2008 SP1 (including Sql Express 2008)
2 - Silverlight 3.0
3 -. NET RIA Services July'09 Preview (for this exercise, this is optional)
The English original is available here: Part 2: Rich Data Query
The online application is available here: http://www .hanselman.com / abrams / # / Home
You can download the complete solution here: MonApplication.zip
First, retrieve the base northwnd.mdf available here: northwnd.mdf
Let us place there in the directory MonApplication.web / AppData.
Come in and select Data ADO.NET Entity Data Model and call it Northwind.edmx .
In the window option in Data Source, select and choose Change Microsoft SQL Server Database File.
boutonBrowse click on the Database File Name and select the base northwnd.mdf .
We can click the button Test Connection to make sure everything goes smoothly.
Let's click OK , and a new window appears. Next click on .
On the next window, we will select the table that interests us, namely, here, you must check SuperEmployees (dbo) . Validate our choice by clicking Finish .
At this stage it is necessary to compile the solution again to take everything into account. To do this, do a right click on our solution and select Build Solution . Here, we created our data access layer.
Now, once everything is in place, we can talk about access to data. Almost all business applications need data access. We will first start with the web project. For this example, I used an Entity Framework data model, but works fine RIA Services with any type of data from objects or XML, web services in Linq to Sql.
Now the question is: how do we access this data from our Silverlight client? Traditionally, most enterprise applications start with a model of two-thirds. This creates many problems in terms of flexibility and scalability ... Moreover, this does not simply with the Silverlight client architecture / web.
As a result, developers are moving more in the world n-tier. It is very easy. NET RIA Services to create n-tier services that are flexible and scalable built on WCF and ADO.NET Data Services.
these services. NET RIA model the logic of our application UI-third and encapsulate access to our various data such as data POCO (Plain Old CLR Object), as Azure Cloud Services , S3, etc.. via REST, etc.. One great feature of this is that you can migrate a SQL Server database to a remote service without Azure have to change anything in the application logic.
Let us now how it is easy to create these services RIA.
conducting a right-click the server project (MonApplication.web) and select add a new class Domain Service. Call the new class SuperEmployeeDomainService.cs .
In the wizard, select our data source (here NORTHWNDEntities ). Note that we could choose a class LINQ2SQL a POCO class, etc.. Let us also check the boxes Enable Client Access SuperEmployees, Enable editing Generate and associated classes for MetaDir .
In the newly created class, we have a draft of all methods to access our data. We should of course modify our application. For the next steps we will use the method GetSuperEmployees (), so we'll have to modify it as follows:
Now basculons on the client side. First, compile the solution again to client side to access directly. These projects are linked.
's perform a drag and drop the DataGrid the toolbox in our view Home.xaml , just after the TextBlock , before closing StackPanel.
's modify the xaml and then added as follows:
Now in code behind (ie, in the file Home.xaml.cs ), add the clause
using MonApplication.Web . Note that
Interestingly MonApplication.Web that is defined on the server ... We can now access the proxy server client DomainService locally.
In line 1 we created our SuperEmployeeDomainContext ... This is the SuperEmployeeDomainService client side. Note the naming convention here.
On line 2, we bind data to datagrid that we created earlier. Finally, line 3, we load the data using the method GetSuperEmployees () we defined on the server.
Note that it is all done asynchronously and we do not have to worry about the complex world of asynchronisation.
And here's the result! We have all our starters, but in the real world, do not we want to paginate and ensure that the server sort and filter the results? Let's see how.
First, completely erase the lines of code that we added in code behind (ie, the three lines in our file Home.xaml.cs). Then, in our view Home.xaml , add a DomainDataSource by dragging and dropping from toolbox.
Then publish the code to be inserted. First add the namespace in our application.
In 1, we have the game namespace.
In 2, we call the method GetSuperEmployeesQuery DomianContext the specified 4.
In 3, we set the size of the load 20. This means that we will download data every 20.
Now let's tie it all in DataGrid and display a small loading indicator.
To do this, we first add a namespace to access the progress indicator.
Then we will modify our views as follows:
Note: when doing a drag and drop a control on the view, the namespace is automatically added. So let's add the DataPager in this way.
At line 27, we see the Datagrid which is linked to property DDS.Data. Then we see a DataPager line 31, which is linked to the same data source. This we lavish interface paging.
line 31 Note that we choose to display ten records at a time. Finally, we include all in a control Activity to show progress.
What's good with the controls Activity the Datagrid and DataPager is that they can be used with any data source as WCF services, REST services, etc..
Support F5 and see the result ...
Note that we load 20 records at a time, but we do not only display 10. So if we go one page, it will be managed solely by the customer, but if we move more, we make a call server and we upload more than 20 recordings. Also note that sorting works well. And where is the code that handles the sorting? Have we wrote the code on the client side or server? Not at all, this is the magic of Linq, it all happens automatically and the result falls.
We can already add a group function. Pou this we will first add a namespace as follows:
Then we joutons code:
And here's the result:
Now add a filter. First, add a Label and TextBox .
Finally, add these filters to our DomainDataSource:
When we press F5, we get a filter box and when you type something in it We're doing a server side filtering of results.
Now, suppose we want to create an input field automatically rather than a simple text box. The first thing we must do is get all possible origins. Note that we obtain from the server (the client may in fact not all have to the extent we load the data in groups of 20, we do pagination, etc.).. To do this, we'll add a method in our DomainService. In the file SuperEmployeeDomainService.cs , first add this class:
Then the method in the class that returns SuperEmployeeDomainService Origins:
In our view Home.xaml , replace the TextBox by AutoCompleteBox like this:
Then add the code to load it all behind:
Let's start with the F5 and everything we should get this:
OK to update data
Now, we certainly have an application that can display data in a sophisticated business applications but also require d authorize the update data. Let's see how. First, replace the xaml below, under DomainDataService. This will provide us with a beautiful view Master-detail.
(Please go here for the full text of Home.xaml ).
Now, run the application with F5 and then we get this:
If one tries to modify a record in the detail view, we see an asterisk notifying us that this entry is amended and what should be returned to the server. If you edit one or more entries and you cancel these changes, the asterisk disappear.
Now we must connect the "Send" button. To do this in your file Home.xaml.cs , add the following delegate:
First, we must validate the item that is being edited, then we just send the changes. These changes are grouped by lot and sent to the server. Then our Update method is called. Note that the asterisk disappears.
That is done, but what about the validation data? Base, we have a validation type level (if it is an integer, a string, etc..). For example, if we fill the EmployeeID field with a string, we get an error (Note: for this to work, you must run your project without debugging by pressing CTRL-F5).
Now, we go a little further. To do this, we edit SuperEmployeeDomainService.metadata.cs on the server. It is important to do this on the server, so the system performs all the checks in the first instance to provide a good experience user and a second time on the server to verify data integrity. When your Update method is called on your DomainService, you can be sure that the validation was made. Here
what we can do validations:
Now we must rebuild the solution for these changes to take effect. Launching the application with CTRL-F5 and we see our validation rules that work.
Note that we can navigate between the errors and that the focus is on the error selected.
So much for the validation data. But what is it about adding data?
To do this, we will use the new child window ( ChildWindow ) Silverlight 3.
Make a right click View, Add ..., New Item . Select Silverlight Child Window and name AddNewWindow .
Lions this window to a button to enable its display. First, add a button to the main form (Home.xaml) to display the new window :
Then the lions-in code behind ( Home.xaml.cs ):
First, add the clause Using:
Then write our event as follows:
And here's the result:
We see this new window is already equipped with an OK button and a Cancel button that is already functional (although they do not make much). We have only to add a DataForm . For this, we have only to resume as we have defined for the update data in our view master / detail. (You can download this file here .)
Now behnind in code, we must proceed with these steps. First, not omit to add our calluses Using.
We will put this class declaration:
Initialize the builder ...
And manage the OK button:
is the result:
Well, now we will reflect this change locally ( Home.xaml.cs ):
First, we will subscribe event AddNewButton_Click an event that will detect the close window AddNewWindow:
And we will create the delegate:
And voila! Our application is fully functional. Note that the changes are passed on to the remote database, once the window closed AddNewWindow, we have to click the send button.
0 comments:
Post a Comment