Silverlight实战示例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,否则会有编译错误.

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值