龍騎少校

玩的就是技术。ko!!!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用Ilungasoft Framework加速Web开发(ASP.Net 2.0)

Posted on 2008-05-13 14:55  龍騎少校  阅读(224)  评论(0编辑  收藏  举报
Ilungasoft Framework是Teddy正在开发中的Web快速开发框架,该框架基于.Net 2.0,设计的出发点就是易用性,快速开发,兼顾性能。同时,Ilungasoft Framework也充分利用了许多.Net 2.0中的新特性以简化框架的易用性。本文将以一个Sample Web应用的分析为基础,介绍Ilungasoft Framework的使用。在本文中,您将看到,Ilungasoft Framework对Web开发的极大简化。

Ilungasoft Framework - A rapid web application development framework based on ASP.NET 2.0. It provides persistence, business logic, web controls, utils support, which make web development based on ASP.NET 2.0 super easy.

整个Framework结构如下:

- Framework.Common: 常用组件(Design By Contract, Utils, Strong Typed Entity Factory, Object Pooling...)

- Framework.Data: 持久化组件(基于DbHelper的持久化设计; Sql2K Provider, Sql2K5 Provider, MsAccess Provider; Page Splitable Select, Sql Statement Factory...)

- Framework.Data.Facade: 持久化组件Facade(强类型封装简化后持久层访问接口,包括Common Gateway和基于Cache Dependency的Cachable Gateway)

- Framework.Web: Web框架组件(MasterPage, Page, UserControl基类; Simple Hierarchical DataSource, Custom Web Controls: Auto Complete TextBox, Upload Progress Bar, Serial Number Validater Control..., UrlRewrite Module...)

- ASP.NET 2.0 Web Site: 表现层(直接引用Framework. Common, Framework. Data. Facade, Framework. Web)

- Framework.Tools.EntityGen: 实体类生成工具

- Framework.Tools.StringFormatHelper: 字符串辅助格式化工具


下载

如果想更直观的阅读本文及体验Ilungasoft Framework,建议您下载下面的文件:
Ilungasoft Framework dist v1.0 beta with Sample
从SF.NET下载最新版本

版本更新:

-2006/04/14 Version 1.4.0 All In One
Update:
1) The second OneToMany Class: OneToMany which is a simplized version of the existed one.
2) Some refactorings which made code more readable.
3) New entity serialize support in SerializeHelper.cs which significiently enhanced the serialize performence for entities.
4) Change the quick Get/Update gateways in Framework.Data.Facade from int type to object type so that they can now work with guid.
5) New OneToManyEntity and OneToOneEntity class in Framework.Common help to describe related entities
6) New high performence serialize support for OneToManyEntity and OneToOneEntity and their arrays in SerializeHelper class.


Sample解析

1. Web.config

1
2
3    
4        

5    
6    
7        
8    

9    
10        
11    

12    
13        
14            
15    

16        
17        
18            
19        

20    

21    
22        
26    

27

Web.config非常简单,除了基本的配置项,分别介绍一下手动增加的内容:

1)Line 3- 5 and line 21-26. 这几行设置用于实现UrlRewrite的section。

这里的UrlRewriteRules,采用标准的正则表达式来进行rewrite。也就是说key代pattern,value则是替换字符串。

另外注意Line 7. 这一行设置了web程序的基目录,如果程序不启用UrlRewrite,则可以忽略这个设置项,否则则建议设置。一旦设置RootUrl,并在Global.asax中将其load到Ilungasoft.Framework.Web.UI.Page.RootUrl这个静态实例,就能通过Ilungasoft.Framework.Web.UI下的Page, UserControl, MasterPage类的ParseUrl()方法,在运行时计算链接的真实路径,而不必担心UrlRewrite可能带来的路径错乱问题。请在Sample程序中搜索ParseUrl,并查看Global.asax文件,查看实际的使用方法。

2)Line 9-11. 设置Connection String. 值得注意的是,如果访问SqlServer,则ProviderName必须设为Ilungasoft.Framework.Data.SqlServer.SqlDbProvider,如果是MsAccess则设为Ilungasoft.Framework.Data.MsAccess.AccessDbProvider。

2. App_Code

1)Facade.cs和CachableFacade只是对Ilungasoft.Framework.Data.Facade下的Gateway和CachableGateway的简单继承,这样的继承并不必须,但是继承以后,由于ASP.NET2.0中的一个Web应用是没有命名空间的。就可以在page的code-behind中以Facade.XXX这样的方式调用组件中的方法,而不需在每个页面都using命名空间或者用完整路径访问了。

2)同样的,Entity.cs也是起到一个简化命名空间的作用,只需将Framework.Tools.EntityGen.exe生成的实体类粘贴至这里就行,当然,自定义的实体类也可以定义在这里。例如:

1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using Ilungasoft.Framework.Common;
11
12public abstract class Entity
13{
14    Paste Auto Generated Entities Here#region Paste Auto Generated Entities Here
15
16    public interface Region : IEntity
17    {
18        int RegionID { get; set; }
19        string RegionDescription { get; set; }
20    }
21
22    public interface order_Details_Extended : IEntity
23    {
24        int orderID { get; set; }
25        int ProductID { get; set; }
26        string ProductName { get; set; }
27        decimal UnitPrice { get; set; }
28        short Quantity { get; set; }
29        float Discount { get; set; }
30        decimal ExtendedPrice { get; set; }
31    }
32
33    #endregion
34
35    Add Custom Entities Here#region Add Custom Entities Here
36
37    #endregion
38}

3. 页面

MasterPage.master和Default.aspx都很简单,只是可以根据需要继承Ilungasoft.Framework.Web.UI下的Page, UserControl, MasterPage类。

注意SampleUserControl.ascx这个控件中的Repeater控件,这里唯一值得一提的就是StrongTyped<>方法,它提供了简单(强类型,基类扩展)高效(避免反射)的数据绑定语法。

1
2
3<%# StrongTyped(Container.DataItem).RegionDescription %>

4

5

下面再具体列举一下对应的SampleUserControl.ascx.cs。

1using System;
2using System.Data;
3using System.Data.Common;
4using System.Configuration;
5using System.Collections;
6using System.Web;
7using System.Web.Security;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11using System.Web.UI.HtmlControls;
12using Ilungasoft.Framework.Common;
13
14public partial class controls_SampleControl : Ilungasoft.Framework.Web.UI.UserControl
15{
16    protected void Page_Load(object sender, EventArgs e)
17    {
18        if (!IsPostBack)
19        {
20            Entity.Region[] regions = Facade.SelectAll();
21            Repeater1.DataSource = regions;
22
23            Entity.Order_Details_Extended[] orderDetails = Facade.Select(5, 3, \"[OrderID] > @OrderID\", \"[ProductID] desc\", 0);
24            GridView1.DataSource = orderDetails;
25
26            DataBind();
27        }
28    }
29    protected void Button1_Click(object sender, EventArgs e)
30    {
31        int nextID = Facade.SelectScalar(\"select max(RegionID) from [Region]\");
32        Entity.Region newRegion = EntityFactory.CreateObject();
33
34        DbTransaction tran = Facade.BeginTransaction();
35
36        try
37        {
38            newRegion.RegionID = nextID + 1;
39            newRegion.RegionDescription = \"new region - \" + DateTime.Now.ToString();
40            Facade.Insert(newRegion, tran);
41            tran.Commit();
42        }
43        catch
44        {
45            tran.Rollback();
46            throw;
47        }
48        finally
49        {
50            Facade.CloseTransaction(tran);
51        }
52
53        Response.Redirect(\"Default.aspx\");
54    }
55}

Line 20首先演示了如何通过Facade从数据库读取记录。Facade中定义了所有常用数据库操作,这些操作都将返回强类型的变量,实体类或集合。也包括了非常方便的分页查询支持,如Line 23获取每页5条记录的第三页。

Line 32演示了如何通过EntityFactory创建一个实体类实例。

Line 34-51演示了一个事务过程。

ok,是不是很简单呢?

这里只是演示了通过Facade读写数据库的方法,对于绝大多数查询,Facade足够了。文中演示了Ilungasoft.Framework.Data下的另几个类SimpleDbAccess和SimpleDbHelper以及Database的调用示例,通过这些类提供了不同层面灵活访问数据库的能力。

关于Ilungasoft Framework更多文章介绍及后续更新,请参见基于.Net 2.0 (C# 2.0, ASP.NET 2.0)的快速Web开发框架设计(文章索引) 。

4. 其他

Sample中只是演示了Ilungasoft Framework最基本的功能,更多功能细节的使用我会陆续介绍。另外,您会注意到压缩包内包含的Framework.Tools.StringFormatHelper.exe这个工具,这个工具目前只有一个功能,就是将一段Javascript封装转换为一个字符串。用于编写web控件的,大家感兴趣可以试试。从网页复制js片断,点击按钮就行了。