Microsoft .NET RIA Services快速上手.doc_第1页
Microsoft .NET RIA Services快速上手.doc_第2页
Microsoft .NET RIA Services快速上手.doc_第3页
Microsoft .NET RIA Services快速上手.doc_第4页
Microsoft .NET RIA Services快速上手.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Microsoft .NET RIA Services快速上手在MIX 09上,Nikhil Kothari发布了微软的一神作Microsoft .NET RIA Services。虽然目前的版本仅仅是可怜的March 09 Preview”,但它已经足够让人兴奋不已。简单地说,在这之前,如果你用到了现在的RIA技术比如Silverlight,你只能选择写大量的服务或者WCF来实现数据的操作功能;而有了.NET RIA Services,你在RIA项目上操作数据,就像ASP.NET那样方便!Nikhil Kothari在MIX09上介绍.NET RIA Services的视频:/RIA-Services-MIX09.aspxMicrosoft .NET RIA Services March 09 Preview及文档下载地址:/downloads/details.aspx?displaylang=en&FamilyID=76bb3a07-3846-4564-b0c3-27972bcaabceMSDN Code Gallery中的.NET RIA Services Samples/RiaServices好了,以上是概要,下面让我们说得更详细些。传统的RIA是怎样操作数据的在去年这个时候,Silverlight 2Beta刚发布,有个朋友问我能不能使用Silverlight直接操作数据库。当时的答案当然是:很遗憾,不行。我们不得不使用大量的Web Services或者WCF来提供对数据库操作的每一个环节,Silverlight只能与数据层“间接接触”。上图表明了整个过程。这样的数据操作虽然已经被大家习惯,但它是不合理的。就像是在实现“三通”以前,咱们去台湾只能先去香港转机。博客园的大牛Shareach前几天写了一个Silverlight的聊天程序,数据操作使用的是WCF Duplex Service实现双向通讯,非常牛,大家可以去看看。(围观连接:/yinpengxiang/archive/2009/03/23/slChat.html)这是Silverlight操作数据层的一个成功案例,但也会让人觉得悲哀:这样一个表面上很简单的聊天程序,为什么有了WCF的参与就变得很复杂? 这是因为,这样的“间接接触”,不仅不直观,还浪费了开发者大量的经理去考虑一些不该考虑的问题。开发者需要在客户端、Web Service端,BLL端各写一个不同版本的数据操作代码,并且还要考虑他们之间交互的安全性、网络情况等等,简直就是一个浪费大量ATP只产生微量GDP的过程。合理的数据操作应该怎样的上图展示了微软在RIA与数据库交互上的宏伟构想:无论是Silverlight,WPF,Javascript,还是ASP.NET,WCF,它们都应该使用无差别的数据逻辑,能够直接访问到数据层面,而不需要通过一层类似“代理”的数据服务。Microsoft .NET RIA Services将如何实现“合理”以上就是.NET RIA Services的实现原理。开发者在ASP.NET端的数据处理类(本图中是HRService)继承自一个叫做DomainService的类,在里面实现一些数据操作。.NET RIA Services就会自动生成相应的客户端类(本图中是HRContext)。而在我们开发客户端的时候,我们就可以直接调用.NET RIA Services生成的那个类,直接操作数据层面。入门实例:在了解.NET RIA Services想要完成的任务及其具体实现方法后,我们可以开始通过实例的方式来体验一下了。1. 开发环境:Visual Studio 2008 SP1 ,Silverlight 3 Beta SDK ,Silverlight Tools 3.0 , Microsoft .NET RIA Services March 09 Preview , SQL Server 2005 2. 在VS2008中新建Silverlight项目3. 将Silverlight连接到ASP.NET Server project上。完成该步骤后的Solution Explorer如下图所示4. 在Web项目上单击右键,新建5. 选择SQL Server2005里的数据库和表。VS会帮我们生成一个ADO.NET的实体(Entity)。生成的文件后缀名为.edmx,如本例中的 6. 编译整个Solution。 7. 再次在Web项目上右击,新增本文的主角Domain Service Class 。Domain Service Class”这名字挺熟的吧?嗯,上文介绍过了。根据提示勾选需要的部分。在本例中,我们选择了Messages表作为实体,并选择”Enable editing”,这样在生成的类中会初始包括Get,Insert,Update,Delete 4个基本的实体操作方法8. 完成上面的操作后,会在Web项目下生成RdChat_DomainService.cs类。CodenamespaceRiaServices_1.WebusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.ComponentModel.DataAnnotations;usingSystem.Linq;usingSystem.Web.Ria;usingSystem.Web.Ria.Data;usingSystem.Web.DomainServices;usingSystem.Data;usingSystem.Web.DomainServices.LinqToEntities;/ImplementsapplicationlogicusingtheRdChatEntitiescontext./TODO:Addyourapplicationlogictothesemethodsorinadditionalmethods.EnableClientAccess()publicclassRdChat_DomainService:LinqToEntitiesDomainService/TODO:Consider/1.Addingparameterstothismethodandconstrainingreturnedresults,and/or/2.Addingquerymethodstakingdifferentparameters.publicIQueryableGetMessages()returnthis.Context.Messages;publicvoidInsertMessages(Messagesmessages)this.Context.AddToMessages(messages);publicvoidUpdateMessages(MessagescurrentMessages,MessagesoriginalMessages)this.Context.AttachAsModified(currentMessages,originalMessages);publicvoidDeleteMessages(Messagesmessages)if(messages.EntityState=EntityState.Detached)this.Context.Attach(messages);this.Context.DeleteObject(messages);9. 再次编译整个Solution。 10. 点击Show All Files,你会发现系统已经为你生成了客户端的类,放在了Generated_Code目录下。我们将它include进来。这个RiaServices_1.Web.g.cs,是服务端RdChat_DomainService.cs的对应客户端版本。它包含了所有服务端版本类中定义的方法、实体等,而可在客户端直接调用。它打代码如下:Code/-/Thiscodewasgeneratedbyatool./RuntimeVersion:2.0.50727.3053/Changestothisfilemaycauseincorrectbehaviorandwillbelostif/thecodeisregenerated./-namespaceRiaServices_1.WebusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.ComponentModel.DataAnnotations;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.Web.Ria.Data;usingSystem.Windows.Ria.Data;DataContract(Namespace=/2004/07/RiaServices_1.Web)publicsealedpartialclassMessages:Entityprivatestring_body;privatestring_name;privatestring_partitionKey;privatestring_rowKey;privateNullable_timestamp;DataMember()publicstringBodygetreturnthis._body;setif(this._body!=value)this.ValidateProperty(Body,value);this.RaiseDataMemberChanging(Body);this._body=value;this.RaiseDataMemberChanged(Body);DataMember()publicstringNamegetreturnthis._name;setif(this._name!=value)this.ValidateProperty(Name,value);this.RaiseDataMemberChanging(Name);this._name=value;this.RaiseDataMemberChanged(Name);DataMember()Key()publicstringPartitionKeygetreturnthis._partitionKey;setif(this._partitionKey!=value)this.ValidateProperty(PartitionKey,value);this.RaiseDataMemberChanging(PartitionKey);this._partitionKey=value;this.RaiseDataMemberChanged(PartitionKey);DataMember()Key()publicstringRowKeygetreturnthis._rowKey;setif(this._rowKey!=value)this.ValidateProperty(RowKey,value);this.RaiseDataMemberChanging(RowKey);this._rowKey=value;this.RaiseDataMemberChanged(RowKey);DataMember()publicNullableTimestampgetreturnthis._timestamp;setif(this._timestamp!=value)this.ValidateProperty(Timestamp,value);this.RaiseDataMemberChanging(Timestamp);this._timestamp=value;this.RaiseDataMemberChanged(Timestamp);publicoverrideobjectGetIdentity()returnEntityKey.Create(this._partitionKey,this._rowKey);publicsealedpartialclassRdChat_DomainContext:DomainContext#regionQueryrootfieldsprivatestaticIQueryable_MessagesQuery=newMessages0.AsQueryable();#endregion/Defaultconstructor./publicRdChat_DomainContext():base(newHttpDomainClient(newUri(DataService.axd/RiaServices_1-Web-RdChat_DomainService/,System.UriKind.Relative)/ConstructorusedtospecifyadataserviceURI./TheRdChat_DomainServicedataserviceURI./publicRdChat_DomainContext(UriserviceUri):base(newHttpDomainClient(serviceUri)/ConstructorusedtospecifyaDomainClientinstance./TheDomainClientinstancetheDomainContextshoulduse./publicRdChat_DomainContext(DomainClientdomainClient):base(domainClient)publicEntityListMessagesgetreturnthis.Entities.GetEntityList();#regionQueryrootpropertiespublicstaticIQueryableMessagesQuerygetreturn_MessagesQuery;#endregion#regionLoadMessagesmethodoverloads/Invokestheserver-sidemethodGetMessagesandloadstheresultinto./LoadMethod(typeof(Messages)publicvoidLoadMessages(IQueryablequery,MergeOptionmergeOption,objectuserState)base.Load(GetMessages,null,query,mergeOption,userState);/Invokestheserver-sidemethodGetMessagesandloadstheresultinto./LoadMethod(typeof(Messages)publicvoidLoadMessages()this.LoadMessages(null,MergeOption.KeepCurrentValues,null);/Invokestheserver-sidemethodGetMessagesandloadstheresultinto./LoadMethod(typeof(Messages)publicvoidLoadMessages(IQueryablequery,objectuserState)this.LoadMessages(query,MergeOption.KeepCurrentValues,userState);#endregionprotectedoverrideEntityContainerCreateEntityContainer()returnnewRdChat_DomainContextEntityContainer();internalsealedclassRdChat_DomainContextEntityContainer:EntityContainerpublicRdChat_DomainContextEntityContainer()this.CreateEntityList(EntityListOperations.All);11. 打开Silverlight项目的 ,我们放入一些控件,做简单的界面。包括一个DataGrid ,TextBox和Button。在按钮中添加Click方法。 data:DataGridTextBoxButtonStackPanelStackPanelGridUserControl12. 在MainPage.xaml后台对应的cs文件 中,写入初始方法和处理按钮点击的方法。初始时,将所有已有的数据绑定到DataGrid中。点击Click方法后,将添加一条新数据到数据库,并刷新外观使新数据显示出来。在这个过程中,我们直接使用.NET RIA Services为你生成的客户端版本数据处理类了!很爽吧? usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Animation;usingSystem.Windows.Shapes;usingRiaS

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论