zoukankan      html  css  js  c++  java
  • ado.net data services开发框架学习

    ado.net data services开发框架学习

    什么是ado.net data services

    Ado.net 数据服务可以很方便的将企业内部数据发布一个数据服务器供web客户端调用,ado.net 数据服务使用标准的http请求获取和操作(CRUD)数据服务,并且实现这些操作都非常简单。

    Ado.net数据服务可以适用于AJAX客户端应用程序,智能客户端应用程序,web应用程序通过web方式存储数据

    Ado.net数据服务是一个轻量级的web服务,可以非常方便快捷的方式发布服务,提供数据共享。

    环境

    • Visual Studio 2008 SP1
    • .NET Framework 3.5 SP1

    数据源

        ado.net数据服务可以很方便的将ado.net entity framework创建的model发布出一个数据服务,ado.net entity framework支持几乎所有的数据源(sql,xml,web service,应用层数据接口),同时也可以自定义一个数据通过实现IUpdatable<T>,IQueryable<T>接口

    创建一个ado.net数据服务

    Ado.net数据服务是一个wcf的特殊形式,从而可以设定配置适用于各种环境,下面是一个创建ado.net数据的实例,创建一个数据服务必须先创建一个web project,接着向你需要与数据建立连接(ado.net entity framework),然后再创建一个ado.net数据服务实现数据的发布。

    创建一个web project

    创建一个"web Application" project 选择"asp.net web application" name"SimpleDataService" OK

    使用ado.net entity framework创建一个数据库实体模型

    创建一个northwind数据实体模型。

    选择需要发布服务的表明(Orders,Order Detail)

    创建数据服务

    在项目文件中新增一个Item选择ado.net data service name:NorthwindDataService

    根据提示(todo:put you data source class name here)将Northwind model添加进去;(TODO:set rules to indicate which entity sets and service operations are visible, updatable, etc. )设定对实体类的访问权限;

    启用数据服务

    根据提示初始化必要的信息(读写访问权限等)

    public class NorthwindDataService : DataService<NorthwindModel.NorthwindEntities>

    {

    // 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, updatable, etc.

    // Examples:

    config.SetEntitySetAccessRule("*", EntitySetRights.All);

    // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);

    }

    }

    测试数据服务

    编译项目后F5通过浏览器查看发布的服务,服务中包含orders,order_details 数据实体对象

    查看Orders数据实体的记录

    注意:地址栏(http://localhost:27383/SimpleDataService/NorthwindDataService.svc/Orders)页面通过RSS的格式显示,我可以看到一共有830条记录;

    点击查看页面[源代码],可以看到所有xml数据

    通过url查询所要记录(查询Order关键字=10248)URL=http://localhost:27383/SimpleDataService/NorthwindDataService.svc/Orders(10248) 查询到单条的记录

    注意:使用ie8 beta2有异常,所以这里使用google chrome浏览器

    Ado.net数据服务url格式

    http://host/%3Cservice%3E/%3CEntitySet%3E[(%3CKey%3E)[/%3CNavigationProperty%3E[(%3CKey%3E)/...]]]

    1. The data service URI.URI<service>对应发布的数据服务文件NorthwindDataService.svc. 比如:http://localhost:27383/SimpleDataService/NorthwindDataService.svc/.
    2. The entity-set name (optional).查看数据服务下面的实体名称,比如 /Orders 返回所有Orders表中记录,如果该实体包含关键字,要通过关键字查询 '10248, 则URI可以表示为/Orders(10248).还可以通过扩张的筛选表达式实现更复杂的查询
    3. A navigation property (optional).导航属性可以通过URI实现将有关联关系的实体查询出来比如Orders下面的子表Order_details.查询orders关键字(10248)下面明细记录 /Orders(10248)/Order_Details

    查询字符串选项

    Option

    Description

    Example

    expand

    The 'expand' option allows you to embed one or more sets of related entities in the results. For example, if you want to display a customer and its sales orders, you could execute two requests, one for /Customers('ALFKI') and one for /Customers('ALFKI')/Orders. The 'expand' option on the other hand allows you to return the related entities in-line with the response of the parent in a single HTTP request.

    You may specify multiple navigation properties to expand by separating them with commas, and you may traverse more than one relationship by using a dot to jump to the next navigation property.

    --a customer with related sales orders
    /Customers('ALFKI')?$expand=Orders

    --a customer with related sales orders and employee information related to those orders
    /Customers('ALFKI')?$expand=Orders/Employees

    --Orders with related employees information and related shipper information
    /Orders(10248)?$expand=Employees,Shippers

    orderby

    Sort the results by the criteria given in this value. Multiple properties can be indicated by separating them with a comma. The sort order can be controlled by using the "asc" (default) and "desc" modifiers.

    /Customers?$orderby=City

    /Customers?$orderby=City desc

    /Customers?$orderby=City desc,CompanyName asc

    skip

    Skip the number of rows given in this parameter when returning results. This is useful in combination with "top" to implement paging (e.g. if using 10-entity pages, saying $skip=30&top=$10 would return the fourth page). NOTE: Skip only makes sense on sorted sets; if an orderby option is included, 'skip' will skip entities in the order given by that option. If no orderby option is given, 'skip' will sort the entities by primary key and then perform the skip operation.

    --return all customers except the first 10

    /Customers?$skip=10

    --return the 4th page, in 10-row pages

    /Customers?$skip=30&$top=10

    top

    Restrict the maximum number of entities to be returned. This option is useful both by itself and in combination with skip, where it can be used to implement paging as discussed in the description of 'skip'.

    --top 5 sales orders

    /Customers?$top=5

    --top 5 sales orders with the highest TotalDue

    /Orders?$orderby=TotalDue&$top=5

    filter

    Restrict the entities returned from a query by applying the expression specified in this operator to the entity set identified by the last segment of the URI path.

    -- all customers in London

    /Customers?$filter=City eq 'London'

    -- Match all Customers with the value of the property 'fullname' equal to 'Wayne, John' /Customers?$filter='Wayne, John' eq insert(ContactName, length(lastname), ',')

    查询表达式

    Operator

    Description

    Example

    Logical Operators

       

    eq

    Equal

    /Customers?filter=City eq 'London'

    ne

    Not equal

    /Customers?filter=City ne 'London'

    gt

    Greater than

    /Product?$filter=UnitPrice gt 20

    ge

    Greater than or equal

    /Orders?$filter=Freight ge 800

    lt

    Less than

    /Orders?$filter=Freight lt 1

    le

    Less than or equal

    /Product?$filter=UnitPrice le 20

    and

    Logical and

    /Product?filter=UnitPrice lteq 20 and UnitPrice gt 10

    or

    Logical or

    /Product?filter=UnitPrice lteq 20 or UnitPrice gt 10

    not

    Logical negation

    /Orders?$ ?$filter=not endswith(ShipPostalCode,'100')

    Arithmetic Operators

       

    add

    Addition

    /Product?filter=UnitPrice add 5 gt 10

    sub

    Subtraction

    /Product?filter=UnitPrice sub 5 gt 10

    mul

    Multiplication

    /Orders?$filter=Freight mul 800 gt 2000

    div

    Division

    /Orders?$filter=Freight div 10 eq 4

    mod

    Modulo

    /Orders?$filter=Freight mod 10 eq 0

    Grouping Operators

       

    ( )

    Precedence grouping

    /Product?filter=(UnitPrice sub 5) gt 10

    更多详细的说明请查看(http://msdn.microsoft.com/en-us/library/cc907912.aspx)

    客户端使用数据服务

    Ajax应用程序客户端

    具体可以参考(http://www.codeplex.com/aspnet/Wiki/View.aspx?title=AJAX

    .net client类库

    客户端库使用HTTP和atompub格式,所以它工作在企业网络和互联网的环境;通过http方式连接到数据服务

    创建一个.net client应用需要引用System.Data.Services.Client.dll 代码如下

    using System;

    using System.Data.Services.Client;

    namespace TestApplication

    {

        public class Region

        {

            public int RegionID { get; set; }

            public string RegionDescription { get; set; }

        }

        class Program

        {

            static void Main(string[] args)

            {

                DataServiceContext ctx = new

                     DataServiceContext("http://localhost:1234/Northwind.svc");

                IEnumerable<Region> regions = ctx.Execute<Region>(

                      new Uri("Region?$orderby=RegionID", UriKind.Relative));

                foreach (Region r in regions)

                {

                    Console.WriteLine(r.RegionID + ", " + r.RegionDescription);

                }

            }

        }

    }

    通过Service Reference创建客户端应用

    如图

    代码

    客户端类库提供的操作和服务端NorthwindEntities 一致也就是说实现了ado.net entity framwork提供的所有操作(CRUD),下面我就不多写了

  • 相关阅读:
    微信公共平台(一)网址接入
    微信公共平台(二)自动回复消息
    Android4.0上的Toast显示太小解决方法
    Android 时间轴
    Node.js开发指南——第3章安装Node.js快速入门(三) 事件
    Node.js开发指南——第3章安装Node.js快速入门(二) 异步式I/O
    Android应用开发实战第二版 demo 基于服务的微博照片分享(修改版)
    android skia decode returned false
    在Myeclipse中查看android源码就是这么easy
    关于android分辨率兼容问题
  • 原文地址:https://www.cnblogs.com/neozhu/p/1287445.html
Copyright © 2011-2022 走看看