Ilungasoft Framework是Teddy正在开发中的Web快速开发框架,该框架基于.Net 2.0,设计的出发点就是易用性,快速开发,兼顾性能。同时,Ilungasoft Framework也充分利用了许多.Net 2.0中的新特性以简化框架的易用性。本文将以一个Sample Web应用的分析为基础,介绍Ilungasoft Framework的使用。在本文中,您将看到,Ilungasoft Framework对Web开发的极大简化。

下载

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

Sample解析

1. Web.config

 1ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发<?xml version="1.0"?>
 2ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发<configuration>
 3ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    <configSections>
 4ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发        <section name="UrlRewriteRules" type="Ilungasoft.Framework.Web.Modules.UrlRewriteRules, Framework.Web"/>
 5ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    </configSections>
 6ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    <appSettings>
 7ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发        <add key="RootUrl" value="/Sample/"/>
 8ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    </appSettings>
 9ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    <connectionStrings>
10ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发        <add name="DatabaseConnection" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="Ilungasoft.Framework.Data.SqlServer.SqlDbProvider"/>
11ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    </connectionStrings>
12ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    <system.web>
13ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发        <compilation debug="true">
14ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发            <assemblies />
15ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    </compilation>
16ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发        <authentication mode="Windows"/>
17ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发        <httpModules>
18ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发            <add type="Ilungasoft.Framework.Web.Modules.UrlRewriteModule, Framework.Web" name="UrlRewriteModule"/>
19ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发        </httpModules>
20ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    </system.web>
21ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    <UrlRewriteRules>
22ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发        <!--
23ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    <Rule key="^/Category/(.+).html" value="/Category.aspx?ID=$1" />
24ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    <Rule key="^/(.+).html" value="/$1.aspx" />
25ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    -->
26ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发    </UrlRewriteRules>
27ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发</configuration>

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生成的实体类粘贴至这里就行,当然,自定义的实体类也可以定义在这里。例如:

 1ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System;
 2ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Data;
 3ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Configuration;
 4ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web;
 5ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web.Security;
 6ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web.UI;
 7ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web.UI.WebControls;
 8ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web.UI.WebControls.WebParts;
 9ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web.UI.HtmlControls;
10ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using Ilungasoft.Framework.Common;
11ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发
12ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发public abstract class Entity
13}

3. 页面

MasterPage.master和Default.aspx都很简单,只是可以根据需要继承Ilungasoft.Framework.Web.UI下的Page, UserControl, MasterPage类。关于这些基类的更详细的介绍,可以参见Asp.Net 2.0 开发加速之 - Ilungasoft.Helper.Web.UI.MasterPage/Page/UserControl 精装版

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

1ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发<asp:Repeater ID="Repeater1" runat="server">
2ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发<ItemTemplate>
3>

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

 1ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System;
 2ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Data;
 3ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Data.Common;
 4ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Configuration;
 5ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Collections;
 6ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web;
 7ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web.Security;
 8ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web.UI;
 9ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web.UI.WebControls;
10ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web.UI.WebControls.WebParts;
11ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using System.Web.UI.HtmlControls;
12ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发using Ilungasoft.Framework.Common;
13ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发
14ASP.Net 2.0 - 使用Ilungasoft Framework加速Web开发public partial class controls_SampleControl : Ilungasoft.Framework.Web.UI.UserControl
15}

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

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

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

ok,是不是很简单呢?

这里只是演示了通过Facade读写数据库的方法,对于绝大多数查询,Facade足够了。如果您觉得不够灵活,请参见Teddy之前的另一篇文章来一点反射,再来一点Emit —— 极度简化Entity!文中演示了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片断,点击按钮就行了。

//End

相关文章:

  • 2022-01-14
  • 2022-01-17
  • 2021-10-15
  • 2021-09-23
  • 2022-12-23
  • 2022-01-03
  • 2022-01-31
猜你喜欢
  • 2021-09-27
  • 2022-12-23
  • 2021-06-02
  • 2022-01-22
  • 2021-11-17
  • 2021-10-10
  • 2022-01-03
相关资源
相似解决方案