Friday, November 21, 2008

Silverlight+ADO.net Data Service (1)

Firstly let's go from test page, there are numerous post how how to build a ado.net data service.

In order to use ado.net data service, you need to install many packages, for these, just go to the microsoft offical website to download and install them.


And we create tables in our database quickly, We just create customers, products, Shippings table.


Let's new a silverlight application project (you need to also install the silverlight 2 packages), and in server side add ado.net entity data model (call it mymodel.edmx, call the entity name: MyStoreEntities1). Add an ADO.net Data Service, called it MyDataService.svc.

The defaulted MyDataService class has the following code:

public class MyDataService : DataService< /* TODO: put your data source class name here */ >
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(IDataServiceConfiguration config)
{ // TODO: set rules to indicate which entity sets and service operations
//are visible, datable, etc.
// Examples:
// config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
// config.SetServiceOperationAccessRule("MyServiceOperation",
//erviceOperationRights.All);
}
}

Change the code to be:

public class MyDataService : DataService<>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(IDataServiceConfiguration config)
{ // TODO: set rules to indicate which entity sets and service operations
//are visible, datable, etc.
// Examples:
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
}
}

We set all the rights to be all, and EntitySet, ServiceOperation to be "*", so that in client side we have right to deal with the data in server side.

And do one more thing, in MyModel.edmx designer changed the class Products to be Product, Customers to be Customer, so the in furture use, the entity data set name and entity class name will not be confused. Entity Data Set name will changed to CustomerSet, ProductSet and the Entity class be Customer and Product.

In this case the design of server side are finshed.

No comments: