文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>N层架构学习笔记

N层架构学习笔记

时间:2010-11-14  来源:麻将我会

1、数据访问层的设计:首先定义一个接口,里面定义了对一张表或一个对象的增删改查操作,然后定义一个类去实现上面的接口;

2、业务逻辑层:同样先定义一个接口,里面定义各种业务逻辑,然后定义一个类去实现上面的接口;

3、表示层:引用业务逻辑层的接口,调用其中的方法;

4、业务实体层;

改进(1)

泛型的引入:首先在设计数据访问层时,第一个接口就定义为泛型接口  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IData
{
  public   interface IData<T>
    {
      IList<T> GetAll();
    }
}

                                                                                       

 

然后每个实体类去分别实现泛型接口:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainTest;
using IData;
using System.Data.SqlClient;
namespace IDataLLimpl
{
   public  class CustomerDataimpl:IData<Customer>
    {
       public  IList<Customer> GetAll()
       {
           SqlConnection con = new SqlConnection(Connectionstring.str);
           SqlCommand cmd = new SqlCommand("select CustomerID,CompanyName from Customers", con);
           con.Open();
           SqlDataReader reader = cmd.ExecuteReader();
           IList<Customer> ls = new List<Customer>();

           while (reader.Read())
           {
               Customer c = new Customer();
               c.CustomerId = reader[0].ToString ();
               c.CompanyName = reader[1].ToString ();
               ls.Add(c);
           }
           con.Close();
           return ls;
       }
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IData;
using DomainTest;
using System.Data.SqlClient;
using System.Configuration;
namespace IDataLLimpl
{
   public  class ProductDataimpl:IData<Product>
    {
      
       public IList<Product> GetAll()
       {  
           SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["constr"]);
           SqlCommand cmd = new SqlCommand("select ProductId,ProductName from Products",con);
           con.Open();
           SqlDataReader reader = cmd.ExecuteReader();
           List<Product> ls = new List<Product>();
           while (reader.Read())
           {
               Product p = new Product();
               p.Id = reader[0].ToString();
               p.Productname = reader[1].ToString();
               ls.Add(p);
           }
           con.Close();
           return ls;
       }
    }
}

 

这样一来数据访问层就基本实现了,如果以后需要增加业务对象,只需要直接继承和实现泛型接口,利于扩展,下面开始业务逻辑层的设计:

 

这里需要也需要一个接口,里面定义了一些泛型方法:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainTest;
namespace IBLL
{
   public  interface IBLLTest
    {
       IList<T> GetAll<T>();
    }
}

 

然后接着在另一个程序集里引用并实现这些泛型方法;

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainTest;
using IBLL;
using IData;
using IDataLLimpl;
namespace Bllimpl
{
   public  class BllimplTest:IBLLTest 
    {
       private IData<T> GetObj<T>( )
       {
           if (typeof(T) == typeof(Product))
           {
               return (IData<T>)new ProductDataimpl();
           }
           else
           {
               return (IData<T>)new CustomerDataimpl();
           }
        
       }
       public   IList<T> GetAll<T>()
       {
           return GetObj<T>().GetAll();
        }
    }
}

 

客户端调用:

 

 protected void Page_Load(object sender, EventArgs e)
    {
        IBLLTest h = new BllimplTest();
        GridView1.DataSource = h.GetAll<Customer>();
        GridView1.DataBind();
    }

 

这样一个结构就算是基本完成了;引入泛型的好处是使得程序的扩展性更强,

实例下载

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载