Getting Started with SubSonic
By Scott Kuhl (http://www.geekswithblogs.net/scottkuhl)
SubSonic is an open-source toolset, created by Rob Conery, as an attempt to put the fun
back into programming and just get the job done. Inspired by Ruby on Rails, SubSonic
takes a minimalist approach to coding and emphasizes convention over configuration.
While it takes its inspiration from Ruby on Rails, it is not a port of it. (Check out MonoRail it
that's what you're looking for.) Instead, SubSonic takes the best ideas of Ruby on Rails
and adapts them into the already existing ASP.NET framework. Currently SubSonic,
version 1.05, implements two core features:
SubSonic 是一种开源工具,由 Rob Conery 创造,作为一种尝试把乐趣带入计划并且完成
这项工作。灵感来自 Ruby on Rails,SubSonic 采取了最低限度的办法,编码,并且强调结
构上的惯例。虽然它的灵感来自 Ruby on Rails,但是这并不是它的一种端口。(查看单轨这
是您要找的内容。)相反,SubSonic 继承了 Ruby on Rails 最好的思想,并且使它们融入已
经现有的 ASP.NET 框架。目前,1.05 版本 SubSonic 有两个核心特点:
ActiveRecord
This design pattern in its simplest form is one class per database table, one object per
database row. SubSonic includes a build-time code generator to implement this
pattern that acts as an object-relational mapper eliminating the need to write SQL. It
also includes a dynamic query tool and simple store procedure support to extend the
model when needed.
这种设计模式的最简单的形式是每个数据库表一类,每个数据库行一个对象。SubSonic
包括建立时间码发生器实施这一模式,作为一个对象关系映射无需编写 SQL。它 还包括
一个动态查询工具和简单的存储过程支持在需要时扩展该模型。
Scaffolding
Admin pages are a chore that scaffolding helps remove. Simply point a scaffold
control at a table and you get the standard grid view and detail view to find and update
data in the underlying table. While not meant to ever be shown to users, it makes a
nice, quick and easy developer tool.
管理的网页是件 scaffolding 帮助删除的苦差事。只要在表格中指点棚架控制,你得到
标准的网格视图和细节视图,用来在潜在的表格中找到并更新数据。虽然并不意味着永
远向用户显示,但它提出了好的、快速简便的开发工具。
Requirements 需求
SubSonic will work fine with Visual Web Developer 2005 Express Edition and SQL Server
2005 Express Edition, so you can get started without dropping a dime. You can also use
MySQL or any database that can be accessed through Enterprise Library for .NET
Framework 2.0, but SQL Server is probably the most likely setup. Note: The sample web
site included with the SubSonic source code includes an SQL script to create the
Northwind database. This article will use that database when examples are needed.
SubSonic可以与Visual Web Developer 2005 Express Edition和SQL Server 2005 Express
Edition很好的工作,这样你就可以未留任何遗漏的使用。您也可以将MySQL或任何
Enterprise Library可读取的数据库用于.NET Framework 2.0,但SQL Server的也许是最有
可能的安装。注:样本的网站包括SubSonic的源代码包含一个SQL脚本创建Northwind数据
库。本文在例子需要时将使用该数据库。
Setup 安装
Setup is easy, just download SubSonic from CodePlex and reference SubSonic.dll
found in the bin directory. Alternatively, you can open the solution and compile a
release build yourself.
(You will need Visual Studio 2005 Standard Edition to open the solution because it also
includes a sample web site or you can use Visual C# 2005 Express Edition to open just
the project.)
安装非常简单,只需从CodePlex下载SubSonic并参考SubSonic.dll在bin路径中建立。
另外,您也可以打开解决方案,并自己新建一个。
(您需要的Visual Studio 2005标准版开来打开解决方案,因为它也包括一个范例网站
或您可以使用Visual C# 2005 Express Edition仅仅来打来项目。 )
Configuration 配置
SubSonic requires a minimal amount of configuration to get going.
SubSonic的使用要求一个最低的配置数量。
SubSonic Configuration Section SubSonic配置节
Start by adding a SubSonic configuration section inside the configuration tag in the
web.config file. This default configuration should work for most projects.
首先,在Web.config文件中,在<configSections>内增加一个SubSonic配置节。这种默
认配置应该对大多数项目有用。
<configSections>
<section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic"
allowDefinition="MachineToApplication" restartOnExternalChanges="true"
requirePermission="false"/>
</configSections>
Data Provider 数据供给者
Second, you will need to setup a data provider. Three are currently supported by
SubSonic: SQL Server, MySQL and Enterprise Library. The following are sample
configurations for each of these. This information is also added inside the
configuration tag.
第二,您将需要设置一个数据提供者。目前被SubSonic支持的有三个: SQL Server,
MySQL和Enterprise Library。下面是这三个的示例配置。这些信息也添加在
<configSections>内。
<SubSonicService defaultProvider="SqlDataProvider" spClassName="SPs"
fixPluralClassNames="true">
<providers>
<add name="SqlDataProvider" type="SubSonic.SqlDataProvider, SubSonic"
connectionStringName="NorthwindConnection"/>
<add name="ELib2DataProvider" type="ActionPack.ELib2DataProvider,
ActionPack" connectionStringName="NorthwindSQL"/>
<add name="MySqlDataProvider" type="ActionPack.MySqlDataProvider,
ActionPack" connectionStringName="NorthwindMySQLConnection"/>
</providers>
</SubSonicService>
There are five values that can be set in the SubSonicService tag.
在<SubSonicService>内有五个值可以设置。
Ÿ defaultProvider - Multiple providers can be setup in the configuration. This value
indicates which provider to use.
n 多个供应者可以在配置内设置。这个值表示使用的是哪个供应者。
Ÿ fixPluralClassNames - SubSonic can remove the plural characters from the end
of table names to make class names more consistent. For example, the Products
table would produce a Product class.
n SubSonic可以从表名末尾删除复数字符,以使类别名一致。例 如,产品表会显
示产品类别。
Ÿ generatedNamespace - By default all classes generated will be part of the
project's global namespace. This value overrides that behavior and includes all
classes in the given namespace. For example, by setting this to Northwind you
would get Northwind.Product.
n 默认情况下产生的所有类别将是该项目的全局命名空间的一部分。这个值忽略
那种行为,并且在特定的命名空间包含所有类别。例如,通过设置这值为
Northwind你会获得Northwind.Product 。
Ÿ spClassName - Each stored procedure will generate a method of the same
name. The value will be the class these methods are included under. For
example, by setting this to SPs the CustOrderHist stored procedure would be
SPs.CustOrderHist. Using the above namespace example in conjunction with this
value would produce Northwind.SPs.CustOrderHist.
n 每个存储过程会产生相同名称的一种方法。这个值是将这些方法包含在其中的
类。例如,通过设置这值为SPs,CustOrderHist存储过程将会成为
SPs.CustOrderHist 。使用上述命名空间例子与这个值相结合将会产生
Northwind.SPs.CustOrderHist 。
Ÿ templateDirectory - It is possible to override the code generated by SubSonic.
This directory would contain the code templates to override the default templates
supplied. This will be covered in greater detail later when discussing Code
Generation Templates.
n 有可能覆盖SubSonic产生的代码。此目录将包含的代码模板,以取代默认模板
提供的。这将会在稍后讨论代码生成模板时更详细地讲解。
Ÿ useSPs - If you do not want a class generated for stored procedures, set this
value to false.
n 如果你不想要一个为存储程序而产生的类,设置此值为false。
Database Connection String 数据库连接字符串
Third, you need to define a database connection string.
第三,你需要定义一个数据库连接字符串。
<connectionStrings>
<add name="NorthwindConnection" connectionString="Data
Source=localhost\SQLExpress; Database=Northwind; Integrated Security=true;"/>
</connectionStrings>
Build Provider Configuration 建立供应商配置
Fourth, you need to setup a build provider to create the auto generated classes. This
needs to be added to the compilation tag.
第四,你需要设置一个建立供应商来产生自动生成的类。这 需 要 在 <compilation>内添加。
<buildProviders>
<add extension=".abp" type="SubSonic.BuildProvider, SubSonic"/>
</buildProviders>
Build Provider Definition 建立供应商的定义
Last, you need to create an .abp file for this build provider to use. You do this by
adding a text file named Builder.abp to the App_Code folder. Inside this file you
indicate which database tables should have auto generate classes. If you want all
tables, just enter *, otherwise, list the tables one per line.
最后,您需要创建一个.abp 文件供此建立供应商使用。通过在 App_Code 文件夹中增
加一个命名为 Builder.abp 的文本文件来这样做。在这个文件中你说明哪些数据库表应
该有自动生成类。如果你想所有表格,只需输入*,否则,每行一个的列出各个表。
Summary 总结
评论0