Silverlight RIA学习笔记一

    一,向中间代码添加业务逻辑

   在RIA service中通常会涉及到一些业务逻辑不需要客户端调用,但中间层又不可或缺,也就是只在中间层中访问,客户端不需要访问,不需要将该方法公开为服务,使用 IgnoreAttribute 特性来标记该方法,这个在客户端不可见,下面的演示将添加一个新的应用,(如果应用名称不重复)该方法使用 IgnoreAttribute 特性进行了标记,以防止从客户端将该方法作为服务调用。

      
中间层业务逻辑
 1         /// <summary>
 2 
 3         /// 判断是否存在相同的应用名称
 4         /// </summary>
 5         /// <param name="name"></param>
 6         /// <returns></returns>
 7         [Ignore]
 8         public bool isExist(string name)
 9         {
10             var temp = this.ObjectContext.BSMG_T_Content.Where(c => c.Name == name).OrderByDescending(c => c.ID).FirstOrDefault();
11             return temp == null ? false : true;
12         } 
13         /// <summary>
14         /// 添加应用
15 
16         /// </summary>
17         /// <param name="bSMG_T_Content"></param>
18         public void InsertBSMG_T_Content(BSMG_T_Content bSMG_T_Content)
19         {
20             if (!this.isExist(bSMG_T_Content.Name))//调用isExist()方法
21             {
22                 if ((bSMG_T_Content.EntityState != EntityState.Detached))
23                 {
24                     this.ObjectContext.ObjectStateManager.ChangeObjectState(bSMG_T_Content, EntityState.Added);
25                 }
26                 else
27                 {
28                     this.ObjectContext.BSMG_T_Content.AddObject(bSMG_T_Content);
29                 }
30             }
31         }

二添加命名更新方法

 

在域服务类中,添加与命名更新方法的预期签名匹配的方法。

该方法应使用 UpdateAttribute 特性标记(同时 UsingCustomMethod 属性设置为 true),或是不返回任何值,而是接受某个实体作为第一个参数。

下面的示例演示一个方法,该方法允许具有更新当前应用中各个属性, 

 

在客户端调用的时候,可直接调用,然后提交
更新当前应用实例
 1         /// <summary>
 2         /// 更新当前应用
 3         /// </summary>
 4         /// <param name="current"></param>
 5         public void SetContentType(BSMG_T_Content current)
 6         {
 7             if (current.EntityState==EntityState.Detached)
 8             {
 9                 this.ObjectContext.BSMG_T_Content.Attach(current);
10             }
11             
12         }
            bSMG_T_Content.SetContentType();
            _rAPAdminAppMgmtDomainContext.SubmitChanges(onSubmitCallback, null);

三:添加调用操作服务方法如下:

 

View Code
1  [Invoke]
2 public IEnumerable GetName()
3 {
4 return new[] { "Tom""Mary""Green" }.Select(i => i.ToString());
5 }

 

客户端调用可作如下调用:

View Code
 1 InvokeOperation invokeOp = customerContext.GetName(OnInvokeCompleted, null);
 2 private void OnInvokeCompleted(InvokeOperation invOp)
 3 {
 4 if (invOp.HasError)
 5 {
 6  MessageBox.Show(string.Format("Method Failed: {0}", invOp.Error.Message));
 7  invOp.MarkErrorAsHandled();
 8 }
 9 else
10 {
11  result = invOp.Value;
12 }
13 }
未完....待续
posted @ 2011-04-22 22:08  渲起浪花  阅读(794)  评论(0编辑  收藏  举报