? ? ?對于在Silverlight中訪問數據,初學者的誤解之一就是他們在Silverlight中尋找ADO.NET類庫。別找了,找不到的。記住,Silverlight是部署在互聯網上的客端技術,你不能要求一個瀏覽器插件去直接訪問你的數據庫……除非你想把數據庫直接暴露在網絡上。我們都知道絕對不能這么做。
所以比較可行的方法是在服務層上暴露數據。這也是Silverlight進行數據通信的方式。這里有一些主要的訪問手段:
- Web服務: SOAP, ASP.NET web services (ASMX), WCF services, POX, REST 終端
- 套接字: 網絡套接字通信(Network Socket Communication)
- 文件: 通過Web請求訪問靜態內
? ? ?對于本項目來說技框架已搭建完成,它是采用RIA Service服務來與數據層交換的:那么什么是RIA Service服務呢?簡單的說就是ADO.NET服務應用的WCF實現。
? ?
? ? 為了數據的安全性,添加了數據邏輯層來處理silverlight展示的GIS數據。


1 /// <summary> 2 /// 用戶服務 3 /// </summary> 4 public class UserService 5 { 6 public event EventHandler<BLL.Bases.ObjectEventArgs<User>> OnGetUserByCodeCompleted; 7 8 public void GetUserByCode(string pUserCode) 9 { 10 UserDALContext myUserDal = new UserDALContext(); 11 myUserDal.GetUser(pUserCode, this.GetUserCompleted, "GetUserByCode"); 12 } 13 14 private void GetUserCompleted(InvokeOperation<User> pUserValue) 15 { 16 if (this.OnGetUserByCodeCompleted != null) 17 { 18 this.OnGetUserByCodeCompleted(this, new Bases.ObjectEventArgs<User>(pUserValue.Value)); 19 } 20 } 21 22 /// <summary> 23 /// 更新用戶信息 24 /// </summary> 25 /// <param name="pUser"></param> 26 public void UpdateUser(User pUser) 27 { 28 UserDALContext myUserDAL = new UserDALContext(); 29 pUser.TableShowedConfigString = pUser.GetTablesShowedConfig().GetXml(); 30 myUserDAL.UpdateUser(pUser, this.UpdateUserOK, ""); 31 } 32 33 /// <summary> 34 /// 當更新用戶信息成功后觸發的事件 35 /// </summary> 36 public event EventHandler<EventArgs> OnUpdateUserOK; 37 38 /// <summary> 39 /// 當用戶更新之后執行的函數 40 /// </summary> 41 /// <param name="pValue"></param> 42 private void UpdateUserOK(InvokeOperation pValue) 43 { 44 if (this.OnUpdateUserOK != null) 45 { 46 this.OnUpdateUserOK(this, new EventArgs()); 47 } 48 } 49 }
?