微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐