1)業務邏輯層:DynamicDataBusi.cs
~~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MEntities;
using System.Data.SqlClient;
namespace BBusiness
{
?? public class DynamicDataBusi
?? {
?????? public DynamicDataTable GetDynamicDataTable(string strSQL, string ConnStr)
?????? {
?????????? SqlConnection theConn = new SqlConnection(ConnStr);
?????????? DataTable theTable = new HDatabase.DynamicDataAccess().GetDataTable(strSQL, theConn);
?????????? DynamicDataTable theDynamicTable = new DynamicDataTable();
?????????? if (theTable != null)
?????????? {
?????????????? foreach (DataColumn col in theTable.Columns)
?????????????? {
?????????????????? DynamicDataColumn theCol = new DynamicDataColumn();
?????????????????? theCol.Caption = col.Caption;
?????????????????? theCol.DataType = col.DataType.Name;
?????????????????? theCol.FieldName = col.ColumnName;
?????????????????? theCol.Length = col.MaxLength;
?????????????????? theCol.FormatString = "";
?????????????????? theDynamicTable.Columns.Add(theCol);
?????????????? }
?????????????? foreach (DataRow row in theTable.Rows)
?????????????? {
?????????????????? DynamicDataRow theRow = new DynamicDataRow();
?????????????????? for (int i = 0; i < theTable.Columns.Count; i++)
?????????????????? {
?????????????????????? DynamicDataField theDataField = new DynamicDataField();
?????????????????????? theDataField.FieldName = theTable.Columns[i].ColumnName;
?????????????????????? theDataField.DataType = theTable.Columns[i].DataType.Name;
?????????????????????? theDataField.Value = row[i];
?????????????????????? theRow.DataFields.Add(theDataField);
?????????????????? }
?????????????????? theDynamicTable.Rows.Add(theRow);
?????????????? }
?????????? }
?????????? return theDynamicTable;
?????? }
?? }
}
~~~
所有要提供給客戶端得實體的打包,以及服務端得實體緩存之類的都可以封裝到這一層。業務邏輯層另外的最主要的功能就是業務邏輯的處理了,簡單的新增,修改,刪除和查詢都可在這里封裝,有的雖然只是簡單的調用數據訪問層,但也不要讓服務層直接調用。因為在這一層可以增加很多功能,比如沖突檢測,邏輯檢查等。
2)RIA 服務層:DynamicDataService
~~~
namespace RIAServices.Web
{
?? using System;
?? using System.Collections.Generic;
?? using System.ComponentModel;
?? using System.ComponentModel.DataAnnotations;
?? using System.Linq;
?? using System.ServiceModel.DomainServices.Hosting;
?? using System.ServiceModel.DomainServices.Server;
?? using MEntities;
?? using BBusiness;
?? // TODO: 創建包含應用程序邏輯的方法。
?? [EnableClientAccess()]
?? public class DynamicDataService : DomainService
?? {
?????? static string conn = "Data Source=127.0.0.1;Initial Catalog=DEVTEST;Persist Security Info=True;User ID=sa;Password=tian777888";
??????
???????[Invoke]
?????? public DynamicDataTable GetDynamicTable(string strSQL)
?????? {
?????????? //在這里檢查調用是否合法
?????????? return new DynamicDataBusi().GetDynamicDataTable(strSQL, conn);
?????? }
?? }
}
~~~
大家要注意,我的數據庫連接出現在這一層,純粹是巧合,數據庫連接應該放到數據訪問層或者配置文件里,如果是比較復雜的應用,比如SaaS,還并需用單獨的類進行管理。
另外注意,這里我沒有直接將服務層放在承載silverlight客戶端得webapp上,而是建立的RIA服務類庫。
到這里,服務端的實現就完成了,編譯后,客戶端就可以看到我們的實體,并可調用服務方法。
后面,我們繼續建立客戶端的應用。
友情提示:以上代碼經過實測,絕對可以OK的。另外注意你們的WCF RIA Services 至少要到SP1,否則會有編譯錯誤.