.NET RIA Service入门系列文章九:自定义函数

很多时候我们可能十分希望自定义一些方法可以让sl端直接调用,对SOA的设计思想,这个是十分有作用的,因为一个函数就完成一个功能,这是SOA最常见不过的设计。这里我将讲述一下.net ria services在sl应用中如何自定义函数.

1.在.net ria service端先定义一个方法,并返回一个自定义的类,这个类必须有主键,可以通过“[Key]”标识一个字段为主键。并把此函数用“[ServiceOperation]”标识,这样sliverlgiht端才会出来这个函数。

1:  [EnableClientAccess()]

2:  public class myDomainService : DomainService

3:  {

4:      [ServiceOperation]

5:      public myclass myText(string userId)

6:      {

7:          myclass mc = new myclass();

8:          mc.userid = userId;

9:          mc.input = userId;

10:          return mc;

11:      }

12:  }

13:  

14:  public class myclass

15:  {

16:      [Key]

17:      public string userid { get; set; }

18:      public string input { get; set; }

19:  }

2.调用相当简单,以下是silverlight端的调用代码

1:  public Home()

2:  {

3:      InitializeComponent();

4:      Business.Web.Services.myDomainContext md = new Business.Web.Services.myDomainContext();

5:      var c = md.myText("FunSL.com");

6:      c.Completed += new EventHandler(c_Completed);

7:  }

8:  

9:  void c_Completed(object sender, EventArgs e)

10:  {

11:      var b = (System.Windows.Ria.Data.InvokeOperation<Business.Web.Services.myclass>)sender;

12:      Business.Web.Services.myclass mc = (Business.Web.Services.myclass)b.Value;

13:      MessageBox.Show(mc.userid + ";" + mc.input);

14:  }

 

posted on 2012-05-10 14:29  $tommix  阅读(176)  评论(0编辑  收藏  举报

导航