三层架构:指的是PL(present layer)层,BLL(business logical layer)层,DAL(data access layer)层,是一种总体设计的思想。
- PL层(web层):是表现层,主要是用来展示数据;
- BLL层:是处理层,主要是用来处理数据;
- DAL层:主要是从数据库获取数据的。
- PL面向用户,DAL面向数据库。
具体阐述:PL是web应用程序,调用BLL的方法将数据传给BLL或者从BLL中获取处理好的数据;BLL层是一个类库,调用DAL的方法获取数据或者将数据处理结果给DAL。
实现方式:
1.新建-项目-空解决方案
![2789632-1412b994aeb8b025.png](https://i-blog.csdnimg.cn/blog_migrate/6c5c202203a425a8fb92a1c012d404c6.webp?x-image-process=image/format,png)
2.右键空解决方案-添加新项- web应用程序(PL层,当然还要添加web窗体)
![2789632-03faa255ad003380.png](https://i-blog.csdnimg.cn/blog_migrate/7546c37e865f40f722672c8a482a06c7.webp?x-image-process=image/format,png)
3.右键空解决方案-添加新项- 类库(BLL层)
![2789632-8dfc70e23af4fce9.png](https://i-blog.csdnimg.cn/blog_migrate/3837d40a0b2553a68c6f1546404cd9ad.webp?x-image-process=image/format,png)
4.右键空解决方案-添加新项-类库(DAL层)
![2789632-3616c451a2d34b99.png](https://i-blog.csdnimg.cn/blog_migrate/a345e576c34800eb992c75e0a6641414.webp?x-image-process=image/format,png)
5.添加PL层的对BLL层的引用
![2789632-a8b6dd353b4f1d28.png](https://i-blog.csdnimg.cn/blog_migrate/4e906fca362faf2382a9b07d4b95db37.webp?x-image-process=image/format,png)
6.添加BLL层对DAL层的引用
![2789632-b08fb52ed919fa9b.png](https://i-blog.csdnimg.cn/blog_migrate/518cc4cf368f8bf59f7248adf93e9121.webp?x-image-process=image/format,png)
PL《——————BLL《——————DAL
7.注意类库的修饰符是否为公共的
![2789632-315b053506b45f5e.png](https://i-blog.csdnimg.cn/blog_migrate/5f10072a77022088676f11d10a145836.webp?x-image-process=image/format,png)
8.Default.aspx
<div>
10+12=
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
10*12=<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<br />
</div>
9.Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
BLL.Calculate c = new BLL.Calculate();
Label1.Text = "" + c.add(10, 12);
Label2.Text = "" + c.mul(10, 12);
}
10.BLL下的Class1
namespace BLL
{
public class Calculate
{
DAL.Class1 c = new DAL.Class1();
public int add(int x, int y)
{
return c.getX() + c.getY();
}
public int mul(int x, int y)
{
return c.getX() * c.getY();
}
}
}
12.DAL下的Class1
namespace DAL
{
public class Class1
{
public int getX()
{ return 10; }
public int getY()
{
return 12;
}
}
}
![2789632-2c7b75227698eca7.gif](https://i-blog.csdnimg.cn/blog_migrate/87a98744f5e0f582acfd6357d4480378.webp?x-image-process=image/format,png)
补充说明:
1.一般类库的名字不是这样命名的,这个只是一个简单例子,看着比较容易理解。一般是公司名+项目名称+BLL(或DAL)。
2.三层架构可以用动软代码生成器生成。下载链接地址是:
http://www.onlinedown.net/soft/63546.htm
总结:此实例先是在web中定义了界面,然后在后台先是对BLL下的Calculate类实例化,再利用该类下的add方法;
BLL的add方法里又使用到了DAL下getX()和getY()方法;
也就是说,先在DAL里获得数据,并返回,由BLL获取数据操作,并返回操作所得的结果,最后由PL层的后台得到数据,最后显示再web上面
所以Label1.Text = " " + c.add(10,12);这里面的参数并没有实际所用,只是为了调用方法而已。里面无论写什么参数,只要是两个int型的,返回的结果是一样的,在这只是展示,并没有实际作用。
思考:明明在web界面的后台程序可以直接处理,为什么要一层一层嵌套才在web层获得数据呢?
优缺点:
优点:
1、开发人员可以只关注整个结构中的其中某一层;
2、可以很容易的用新的实现来替换原有层次的实现;
3、可以降低层与层之间的依赖(高内聚,低耦合);
4、有利于标准化;
5、利于各层逻辑的复用。
缺点:
1、降低了系统的性能。这是不言而喻的。如果不采用分层式结构,很多业务可以直接造访数据库,以此获取相应的数据,如今却必须通过中间层来完成。
2、有时会导致级联的修改。这种修改尤其体现在自上而下的方向。如果在表示层中需要增加一个功能,为保证其设计符合分层式结构,可能需要在相应的业务逻辑层和数据访问层中都增加相应的代码。
![2789632-e205fb9a4fcf66cf.png](https://i-blog.csdnimg.cn/blog_migrate/af57591e156a9a737d3d11af2db75d39.webp?x-image-process=image/format,png)
![2789632-7b3841388c39fdf1.png](https://i-blog.csdnimg.cn/blog_migrate/308e42696fe6d5b5e067b4df689e1519.webp?x-image-process=image/format,png)
![2789632-672cb0108969c879.png](https://i-blog.csdnimg.cn/blog_migrate/480f910d5d936c410c1d4c7bb5c8b63f.webp?x-image-process=image/format,png)