silverlight学习日记--第一个试水架构

采用微软提供的silverlight+wcf ria service+ado.net entity或者是linqtosql对于开发小的,且业务逻辑不太复杂的系统来说确实很简单,也很快,在这两天的学习中,有些小体会,

但采用已有的框架(比如,nhibernate,sprint.net等)一向来都不是我的风格,因为我比较喜欢可控性,虽然说这些框架都是非常好的东西,但毕竟:一、它们都是为全世界服务的,想得太全就必然“不精”,而我的选择只需要对我自己的系统负责,那里面太多的东西对我来说要么没有用,要么就太累赘;二、东西是好,但毕竟被封起来了,要增加一些适合自己系统的快速开发功能,就不太容易了。

通过这几天的学习,我为我自己的系统构架了一个简单的框架,因为初入银光,能力有限,还望大家赐教。

总的架构就是:数据库+模型层+UI层(视图+视图模型层)(这个就是silverlight的MVVM模式).

我主要集中在模型层,将其分为数据库访问层,数据访问层,业务逻辑层和对外服务层,外加一个公用的实体层。

数据库访问层:主要是与数据库打交道,之所以单独作为一层,主要是为了实现多种数据库访问的支持,当然,在这一层我会提供一个统一的访问接口给数据访问层。

数据访问层:主要实现具体的业务数据访问,比如获取实体等,这一层封装具体业务sql的访问,为了避免数据库sql语句的差异性,这一层,同样通过统一的接口提供给业务逻辑层来访问。

业务逻辑层:业务逻辑的控制主要都在这里,这一层是数据库类型无关的,一般情况下只对实体或者DataTable进行操作。

前面的几层采用传统的编程方式形成类库,供其它层调用即可。

对外服务层:采用WFC RIA Service提供服务给silverlight客户端调用。除了一些权限控制之外,这层基本都是调用业务逻辑层。

实体层:就是数据库表的实体化一层,实现数据库表与实体类的映射。

 

上面的层次可能比较多,之所以不用对外服务层做业务逻辑层,主要原因是对外服务层是专门针对silverlight客户端,其它的应用都没法访问。而目前的这种方式的好处就是业务逻辑层以下,winform,web,webservice,wcf等都可以用,有很大的灵活性。

 

下面是具体的代码(类可以分库存放,有些层太大还可以分工程):

public class DbHelper
    {
        /// <summary>
        /// 执行查询语句,返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public DataSet QueryData(string SQLString, DbConnection conn, DbTransaction Trans)
        {
            SqlConnection connection = conn as SqlConnection;
            connection.Open();
            try
            {
                DataSet ds = new DataSet();
                SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                command.Fill(ds, "ds");
                command.Dispose();
                return ds;


            }
            catch
            {
                return null;
            }
        }
    }


    public class Employee_D
    {
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public List<Employee_E> GetModelList(string StrWhere, DbConnection Conn, DbTransaction Trans)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from EmployeeInfo");
            strSql.Append(" where 1=1 ");
            strSql.Append(StrWhere);
            List<Employee_E> theLists = new List<Employee_E>();
            DataSet ds = new  DbHelper().QueryData(strSql.ToString(),Conn,Trans);
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    Employee_E theModel = new Employee_E();
                    theModel.EmployeeID = int.Parse(row["EmployeeID"].ToString());
                    theModel.EmployeeAge = int.Parse(row["EmployeeAge"].ToString());
                    theModel.EmployeeDesc = row["EmployeeDesc"].ToString();
                    theModel.EmployeeName = row["EmployeeName"].ToString();
                    theModel.Dicts.Add("sss", 100);
                    theLists.Add(theModel);

                }
            }
            return theLists;
        }
    }

 

public class Employee_B
    {
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public List<Employee_E> GetModelList(string StrWhere,string ConnStr)
        {
            SqlConnection theConn = new SqlConnection(ConnStr);

            return new Employee_D().GetModelList(StrWhere, theConn, null);
        }
    }

 

 

// TODO: Create methods containing your application logic.
    [EnableClientAccess()]
    public class MyCommonBusi : DomainService
    {
        static string conn = "Data Source=127.0.0.1;Initial Catalog=DEVTEST;Persist Security Info=True;User ID=sa;Password=tian777888";

        public List<Employee_E> GetEntitys()
        {
            return new Employee_B().GetModelList("", conn);
        }
        [Invoke]
        public string UpdateEmployee(Employee_E model)
        {
            return model.KeyValue+" is updated!";
        }
        [Invoke]
        public int GetTemp(Employee_E model, string aaa)
        {
            return 100;
        }
    }

 public interface IEntitiBase
    {
        [DataMember]
        Dictionary<string, int> Dicts{get;}

        [DataMember]
        [Key]
        string KeyValue
        {
            get;
        }
       
    }
    [DataContractAttribute(IsReference = true)]
    public class Employee_E : IEntitiBase
    {
        // Metadata classes are not meant to be instantiated.
        [DataMember]
        public Nullable<int> EmployeeAge { get; set; }
        [DataMember]
        public string EmployeeDesc { get; set; }
        [DataMember]
        [Key]
        public int EmployeeID { get; set; }
        [DataMember]
        public string EmployeeName { get; set; }
      
        [DataMember]
        public int sss
        {
            get
            {
                return Dicts["sss"] ;
            }
        }
        [DataMember]
        private Dictionary<string, int> _dicts = new Dictionary<string, int>();
        public Dictionary<string, int> Dicts
        {
            get { return _dicts; }
        }

        public string KeyValue
        {
            get { return EmployeeID.ToString(); }
        }
    }

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值