Ibatisnet示例:npetshop学习一
当大概的观看NPetshop的分层架构后,突然发现与上家公司使用的框架异常相似......
今天学习目标是.service .presentation两层。
一、NPetshop.Presentation
记得在Peshop4.0中有用到Facde设计模式,可以很好的使用分层结构,Presentation应该也是起这个作用;在Npetshop中,我看这层所属文件名后都带有‘Action’字样,action在这里是什么意思?活动、动态?
1.UserControl.cs
所有用户控件都继承自它。某些子控件中仍有其他子控件,比如startup.ascx就包含了SideBar.ascx子控件,因为Npetshop中是将页面作为'容器'来设计的,所以必须将子控件的CurrentController指向页面,便于管理,代码如下:
UserControl类的IsRequestCurrentView属性,在ui层的Catelog.ascx.cs 及 productlist.ascx.cs的page_load事件中,通过对该属性的判断,将当前产品分类或产品名称显示出来(代码已注释)。
该属性的类型为IController接口,页面容器继承自该接口,这种模式不知能否理解为与parent属性类似。
UserControl类还有重要属性:WebLocalSingleton ,该类位于Npetshop.Presentation.Core命名空间。这是一种单态设计模式,确保每个类只有一个实例,并提供它的全局访问点 。
2.WebLocalSingleton.cs
通过静态方法以及context上下文信息,以确保在任何时间都只返回一个实例,如代码:
未完......
今天学习目标是.service .presentation两层。
一、NPetshop.Presentation
记得在Peshop4.0中有用到Facde设计模式,可以很好的使用分层结构,Presentation应该也是起这个作用;在Npetshop中,我看这层所属文件名后都带有‘Action’字样,action在这里是什么意思?活动、动态?
1.UserControl.cs
所有用户控件都继承自它。某些子控件中仍有其他子控件,比如startup.ascx就包含了SideBar.ascx子控件,因为Npetshop中是将页面作为'容器'来设计的,所以必须将子控件的CurrentController指向页面,便于管理,代码如下:
1
public IController CurrentController
2
{
3
get
4
{
5
return _currentController;
6
}
7
set
8
{
9
for(int i=0; i< this.Controls.Count; i++)
10
{
11
if (this.Controls[i] is NPetshop.Presentation.UserControl)
12
{
13
((NPetshop.Presentation.UserControl) this.Controls[i]).CurrentController = value;
14
}
15
}
16
this._currentController = value;
17
}
18
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
UserControl类还有重要属性:WebLocalSingleton ,该类位于Npetshop.Presentation.Core命名空间。这是一种单态设计模式,确保每个类只有一个实例,并提供它的全局访问点 。
2.WebLocalSingleton.cs
通过静态方法以及context上下文信息,以确保在任何时间都只返回一个实例,如代码:
1
private HttpContext _context;
2
static readonly private object _synRoot = new Object();
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
/// <summary>
5
///
6
/// </summary>
7
/// <param name="ctx"></param>
8
/// <remarks>Call it with HttpContext.Current.ApplicationInstance.Context.</remarks>
9
/// <returns></returns>
10
public static WebLocalSingleton GetInstance(HttpContext ctx)
11
{
12
WebLocalSingleton singleton = ctx.Items[WebConstants.SINGLETON_KEY] as WebLocalSingleton;
13
if (singleton==null)
14
{
15
lock(_synRoot)
16
{
17
if (singleton == null)
18
{
19
singleton = new WebLocalSingleton(ctx);
20
ctx.Items[WebConstants.SINGLETON_KEY] = singleton;
21
}
22
}
23
}
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
return singleton;
26
}
同时,在该类还保存了当前context下的Action 、Account、ShoppingCart、Order、IPaginatedList 数据,在这里让我费解的就是为何要将IPaginatedList对象放入到该context中,因为它不需要被全局使用,应该是随时获取、释放的。![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
未完......