N层架构学习笔记(可下载实例代码)

数据访问层、业务逻辑层、表示层、业务实体层;

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();
    }

 

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

实例下载

另外添加工厂设计模式,通过 IoC 或者 "配置反射" 来获得具体的数据层DAL实现类,可以减少层之间的耦合,也便于数据系统的替换(多层加IOC模式);

posted @ 2010-11-14 19:29  麻将我会  阅读(408)  评论(0编辑  收藏  举报