Silverlight實戰樣本5(兼集合屬性的妙用)–用戶端模型層(M)

來源:互聯網
上載者:User

1)服務代理層

      這裡的代碼都是系統自動產生的,主要負責代理服務端得方法執行,沒什麼特別可說的.

2)用戶端模型層DynamicDataSource.cs

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RIAServices.Web;
using MEntities;
using System.ServiceModel.DomainServices.Client;
namespace MAppStructure.Datasource
{
    /// <summary>
    /// 正常化自己的調用參數,目的是將InvokeOperation調用參數轉換為
    /// 自己易於處理的參數形式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class InvokeEventArgs<T>
    {
        public T Value { get; set; }
        public string ErrorMsg { get; set; }
        public object UserState { get; set; }
        public bool HasError { get; set; }
    }
    /// <summary>
    /// 負責與服務代理層通訊,負責調用代理層方法,並將參數轉換為客戶
    /// 端易於理解和處理的形式
    /// </summary>
    public class DynamicDataSource
    {
        public void GetDynamicDataTable(string strSQL, Action<InvokeEventArgs<DynamicDataTable>> CallBack, object UserState)
        {
            DynamicDataContext theContext = new DynamicDataContext();
            theContext.GetDynamicTable(strSQL, op => {
                CallBack(CreateEventArgs<DynamicDataTable>(op));
            },UserState);
        }
        //這個函數的目的可以將服務調用的錯誤集中在此進行處理,或者將錯誤資訊轉換成用戶端比較容易處理的形式。
        private static InvokeEventArgs<T> CreateEventArgs<T>(InvokeOperation<T> InvokeOp)
        {
            InvokeEventArgs<T> theArgs = new InvokeEventArgs<T>();

            if (InvokeOp.HasError == true)
            {
                //將錯誤標誌為已處理.
                InvokeOp.MarkErrorAsHandled();
                theArgs.HasError = true;
                //這裡的提示方式可以根據自己的需要進行處理
                theArgs.ErrorMsg = InvokeOp.Error.InnerException.Message;
            }
            else
            {
                theArgs.HasError = false;
                theArgs.Value = InvokeOp.Value;
            }
            theArgs.UserState = InvokeOp.UserState;
            return theArgs;
        }
    }
}

這一層的目的就是進行資料及服務的預先處理,用戶端得緩衝也需放在這一層。因為VM層是針對V的,一般情況下很難共用,而M層中的很多服務都可以出現在不同的VM 中,這也是為什麼要單獨在服務代理層上再加一層的原因,而且這一層可以建立基類,便於控制和擴充。服務代理層是沒辦法做基類的,因為代碼都是動態產生的。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.