Consuming ADO.NET Data Service (Astoria) from Silverlight

本文介绍如何利用ADO.NET Data Services Silverlight Add-On让Silverlight应用访问Astoria数据服务。通过创建示例项目,展示从设置数据库到创建ASP.NET Web应用程序,再到实现Silverlight ListView显示数据的全过程。

This post describes how to access the ADO.NET Data Service (Astoria) from Silverlight by using “ADO.NET Data Services Silverlight Add-On“.

Download : SourceCode

ADP.NET Data Service (Astoria) from Silverlight (Silverlight ListView)

Contents

  • Introduction
  • Prerequisites
  • Getting Started
  • SQL Database
  • Creating Web Application in Visual Studio 20008
  • Creating ADO.NET Entity Data Model
  • Creating ADO.NET Data Service
  • Silverlight ListView
  • Conclusion

Introduction

This article is just an introduction of how to use Astoria in Silverlight application. The sample that I used in this article is nearly as same as the sample which is mentioned in ASP.NET 3.5 Extensions Preview (Quick Start). But I used the Astoria Silverlight Addon and create the Silverlight listview that looks cool.

Background

Project Astoria Team released ADO.NET Data Service library for Silverlight on 11st January, 2008. This is very interesting news for Silverlight developer. I was thinking to give a try when I heard that news but a lot of things keep me busy until 15th of Jan. Now, I got a chance to try and make this sample for you all. I hope you guys like it.

Prerequisites

You need to install the following installers in order to use Astoria Service, Silverlight.

Getting Started

After installing all required installers, you started creating new project to give a try how to use ADO.NET Data Service in Silverlight application. Like my other articles, I will give you the step-by-step details on how to consume Astoria in Silverlight. If you are not clear something, please just drop a comment in this post. I will get back to you as soon as I can.

SQL Database

You need to create one database that can be used in our sample. (If you already download the sample, you can find the SQL database called EmployeeMgmt.mdf in zip file.) If you want to create your own database, open SQL 2005 management studio and create new database. Then, create new table and fill some data in that table. (As this article is not for creating the database in SQL, I won’t go step by step. If you have some problems in doing this, please let me know.)

Creating Web Application in Visual Studio 20008

After creating the database, we will start creating new ASP.NET web application in Visual Studio 2008.

  • Click “New Project” icon or Press “Ctrl+Shift+N”
  • Select “ASP.NET Web Application” and name the application as “ADO.NETSrv”
  • Click “OK” button
  • Go to the properties of “ASP.NET Web Application”
  • Select “Web” tab
  • Select “Specific” port as the screenshot below (Visual Studio uses the dynamic port for ASP.NET by default. But using the dynamic port will give us the trouble “Cross-domain issue” since we are going to hard-code the service URL in our silverlight project. So, we just make sure our site to run with static port.)Making the static port in Visual Studio 2008

This is about creating ASP.NET web application in Visual Studio 2008. We will create “ADO.NET Entity Data Model” in next step.

Creating ADO.NET Entity Data Model

  • Add new item “ADO.NET Entity Data Model” to our web application
    ADO.NET Entity Data Model
  • Click “OK” button (Entity Data Model Wizard will be shown as the picture below).
  • Select “Generate from Database”Model Generate from Database
  • Click “Next” button
  • Choose Your Data Connection

    Choose Your Data Connection

  • Click “Next” button
  • You have to choose your table in that step. (Customer table in my case)
    Choose Your Database Objects
  • Click “Finish” button (Visual Studio will create Model1.edmx in your project.)
    Customers Table

That’s all about creating “ADO.NET Entity Data Model” in Visual Studio 2008. We will create “ADO.NET Data Service” in next step.

Creating ADO.NET Data Service

  • Add “ADO.NET Data Service” to your web application (Note : If you don’t have ASP.NET 3.5 extension installed in your machine, you won’t see this template.)

    ADO.NET Data Service

  • Click “Add” button
  • Open “WebDataService1.svc.cs” file. ( You will see “WebDataService< /* TODO: put your data source class name here */ >” in WebDataService1 class.)
  • Write your data source class name in that place. ( WebDataService<EmployeeMgmtEntities> in my case.) If you don’t see the class in your intellisense, add your data model namespace (e.g. using EmployeeMgmtModel;) in that class. (if you don’t know what your data model namespace, you can check it in “Model Browser” )
  • Write the following code in InitializeService method
    config.SetResourceContainerAccessRule("*", ResourceContainerRights.All);
    

That’s all about creating ADO.NET Data Service in Visual Studio. You can run the project now. It will show you like Feed. So, if you want to see the XML, you need to disable the “Feed Reading View”. Go to “Internet Options”>Content>Feeds Setting. Uncheck “Turn on feed reading view”.

“Turn on feed reading view”.

Then, you can run the application. You will see the data as XML format in browser. You can query the data from url like http://localhost:49970/WebDataService1.svc/Customers or http://localhost:49970/WebDataService1.svc/Customers(1001)/ in address bar. (1001 is the id of first customer.) This is one cool thing about ADO.NET Data service. If you want to know more about this query, you can read here. Okay!!. We will create the Silverlight project in next step.

Creating Silverlight project with ADO.NET Data Service Silverlight Addon

  • Add the Silverlight project to your solution
  • Add “Microsoft.Data.WebClient.dll” as a reference in your silverlight project
  • Add the SilverlightLink to Web application

    Add Silverlight Link

  • Copy TestPage.html, TestPage.html.js and Silverlight.js to Web Application
  • Set TestPage.html as a startup page
  • Create the class like below. (Note: The class name should be the same as your table name. The fields should be the same as your table structure.)
     
    public class Customer {
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    }
    

    I don’t like to create that class in Silverlight project but this is by-design issues so that I have no choice. I hope Astoria Team will do something about it.

  • Write the following code in Page_Load event to invoke the ADO.NET Data Service from Silverlight
    try {
    WebDataContext ctx = new
    WebDataContext("http://localhost:50527/WebDataService1.svc");WebDataQuery<Customer> customers = ctx.CreateQuery<Customer>("/Customers?$orderby=CustomerID");
    
    foreach (Customer c in customers) {
    Console.WriteLine(c.CustomerID + "," + c.CustomerName);
    }
    }
    catch (Exception ex) {
    Console.WriteLine(ex.Message);
    }
    

You can set the breakpoint in foreach loop and run the application. You will get all records from your database. but how we can show the data in Silverlight?

Silverlight ListView

There is no Gridview or ListView in Silverlight application. So, we have to create our own control for that. Based on the design of Microsoft Download Beta, I have created one simple Silverlight ListView to show the data in Silverlight application.

Let’s try to check how our listview looks like. We have to comment all codes in Page Load event and write the following code to show the ListView. Then, run the application.

ListView _listview = new ListView();
_listview.Items.Add(new ListViewItem("", ""));
_listview.Items.Add(new ListViewItem("", ""));
_listview.Items.Add(new ListViewItem("", ""));
_listview.Items.Add(new ListViewItem("", ""));
_listview.UpdateView();
this.Children.Add(_listview);

Great! our listview looks cool.

Silverlight ListView

then, update the code like below in Page load event. And run the application. All records from Database will be shown in our ListView through ADO.NET Data Service.

try {

WebDataContext ctx = new
WebDataContext("http://localhost:50527/WebDataService1.svc");

ListView _listview = new ListView();

WebDataQuery<Customer> customers = ctx.CreateQuery<Customer>("/Customers?$orderby=CustomerID");

foreach (Customer c in customers) {
Console.WriteLine(c.CustomerID + "," + c.CustomerName);
_listview.Items.Add(new ListViewItem(c.CustomerID.ToString(), c.CustomerName));
}

_listview.UpdateView();

this.Children.Add(_listview);

That’s all about consuming the ADO.NET Data Service from Silverlight application. Even though this article doesn’t cover any details about ADO.NET Data Service, I hope you will get some understanding about how to use that service in Silverlight application. This is the purpose of writing this article. Hopefully, you enjoy reading it. Feel free to drop a comment if you have any suggestion or comment. Thanks.

 原文地址:http://michaelsync.net/2008/01/15/consuming-adonet-data-service-astoria-from-silverlight

采用PyQt5框架与Python编程语言构建图书信息管理平台 本项目基于Python编程环境,结合PyQt5图形界面开发库,设计实现了一套完整的图书信息管理解决方案。该系统主要面向图书馆、书店等机构的日常运营需求,通过模块化设计实现了图书信息的标准化管理流程。 系统架构采用典型的三层设计模式,包含数据存储层、业务逻辑层和用户界面层。数据持久化方案支持SQLite轻量级数据库与MySQL企业级数据库的双重配置选项,通过统一的数据库操作接口实现数据存取隔离。在数据建模方面,设计了包含图书基本信息、读者档案、借阅记录等核心数据实体,各实体间通过主外键约束建立关联关系。 核心功能模块包含六大子系统: 1. 图书编目管理:支持国际标准书号、中国图书馆分类法等专业元数据的规范化著录,提供批量导入与单条录入两种数据采集方式 2. 库存动态监控:实时追踪在架数量、借出状态、预约队列等流通指标,设置库存预警阈值自动提醒补货 3. 读者服务管理:建立完整的读者信用评价体系,记录借阅历史与违规行为,实施差异化借阅权限管理 4. 流通业务处理:涵盖借书登记、归还处理、续借申请、逾期计算等标准业务流程,支持射频识别技术设备集成 5. 统计报表生成:按日/月/年周期自动生成流通统计、热门图书排行、读者活跃度等多维度分析图表 6. 系统维护配置:提供用户权限分级管理、数据备份恢复、操作日志审计等管理功能 在技术实现层面,界面设计遵循Material Design设计规范,采用QSS样式表实现视觉定制化。通过信号槽机制实现前后端数据双向绑定,运用多线程处理技术保障界面响应流畅度。数据验证机制包含前端格式校验与后端业务规则双重保障,关键操作均设有二次确认流程。 该系统适用于中小型图书管理场景,通过可扩展的插件架构支持功能模块的灵活组合。开发过程中特别注重代码的可维护性,采用面向对象编程范式实现高内聚低耦合的组件设计,为后续功能迭代奠定技术基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值