4业务逻辑与服务层

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,否则会有编译错误.

文章导航