专注于.NET平台的Web开发。

(原)微软轻量级“代码生成器”—Repository Factory使用(上)

上一篇 / 下一篇  2007-12-06 23:12:11 / 个人分类:Software Factory

概述

Repository Factory微软模式与实践小组发布的一个开发指南包,它把之前的Web Service Software Factory(WSSF)集成的Data Access Guidance Package分离出来,形成了一个单独的开发指南包。引用Johnny Halife的话说:“它不是一个对象-关系映射(Object-Relational Mapping,ORM)工具,它的目的是作为一个轻量级的代码生成器,以自动化完成绝大部分生成领域模型对象,并将之持久化到数据库的任务代码。”本文为微软轻量级“代码生成器”—Repository Factory使用上篇。

Johnny指出了Repository Factory的改进之处:

1.开发包被移植到GAT/GAX上。

2.对WSSF的依赖全部移除,因此Repository Factory现在是一个完全独立的指南开发包。

3.之前由开发包生成并且包含多个基类的通用代码,现在被打包成为一个独立的DLL,并由Repository Factory项目引用。

4.生成了一个通用基本接口,来支持IOC模式。

5.除通用基本接口外,还生成了一个Factory类,并可以在项目配置文件中进行配置。因此,Repository Factory的实现方式可以通过修改配置文件切换。

6.为自定义存储操作的方便,加入了从实体字段到存储过程参数的自动映射。

7.数据库名称和配置从生成的Repository移植到了Repository<T>基类,连接字符串定义在配置文件中。

8.Repository方案的设置(操作和映射)现在可以保存起来以供重用。

下载安装

可以到RepositoryFactory官方主页上下载最新版本:http://www.codeplex.com/RepositoryFactory

注意安装之前请确保安装:

1.Guidance Automation Extensions

2.Guidance Automation Toolkit

启用RepositoryFactory

Step1:在Visual Studio 2005工具菜单中,选择Guidance Package Manager,可以打开指导包管理器:

TerryLee_RF_001

Step2:选择Repository Factory

TerryLee_RF_002

Step3:同时还需要在项目上右键菜单中,选择Specify project responsibility

TerryLee_RF_005

Step4:分别选择Business Entities Project、Data Access Project、Host Project。

TerryLee_RF_006

添加数据库连接

Step1:接下来需要添加数据库连接

TerryLee_RF_003

Step2:输入数据库连接串的名称:

TerryLee_RF_004

创建连接完成后,将会在配置文件添加代码:

<connectionStrings>
<addname="RFConnectionString"connectionString="Data Source=Esint-lhj\Sql2005;Initial Catalog=AdventureWorksDW;Persist Security Info=True;User ID=sa;Password="providerName="System.Data.SqlClient"/>
</connectionStrings>

创建实体类

用Repository Factory可以很方便的通过数据库架构,生成业务实体的代码。

Step1:创建业务实体

TerryLee_RF_007

Step2:选择数据库连接字符串

TerryLee_RF_008

Step3:选择数据表、视图和字段

TerryLee_RF_009

Step4:设置实体的属性了,可以设置业务实体的名称,默认的是表名;设置业务实体的属性名和该属性是否为只读属性

TerryLee_RF_010

点击完成后,会生成业务实体的代码,并且为局部类型,这样便于用户在该业务实体上添加自己的一些操作,示例代码如下:

public partial classDimGeography{publicDimGeography()
{
}publicDimGeography(System.Stringcity, System.StringcountryRegionCode, System.StringenglishCountryRegionName,
System.StringfrenchCountryRegionName, System.Int32geographyKey, System.StringpostalCode,
Nullable<System.Int32> salesTerritoryKey, System.StringspanishCountryRegionName, System.StringstateProvinceCode,
System.StringstateProvinceName)
{this.cityField = city;this.countryRegionCodeField = countryRegionCode;this.englishCountryRegionNameField = englishCountryRegionName;this.frenchCountryRegionNameField = frenchCountryRegionName;this.geographyKeyField = geographyKey;this.postalCodeField = postalCode;this.salesTerritoryKeyField = salesTerritoryKey;this.spanishCountryRegionNameField = spanishCountryRegionName;this.stateProvinceCodeField = stateProvinceCode;this.stateProvinceNameField = stateProvinceName;
}privateSystem.StringcityField;publicSystem.StringCity
{get{return this.cityField; }set{this.cityField =value; }
}privateSystem.StringcountryRegionCodeField;publicSystem.StringCountryRegionCode
{get{return this.countryRegionCodeField; }set{this.countryRegionCodeField =value; }
}privateSystem.StringenglishCountryRegionNameField;publicSystem.StringEnglishCountryRegionName
{get{return this.englishCountryRegionNameField; }set{this.englishCountryRegionNameField =value; }
}privateSystem.StringfrenchCountryRegionNameField;publicSystem.StringFrenchCountryRegionName
{get{return this.frenchCountryRegionNameField; }set{this.frenchCountryRegionNameField =value; }
}privateSystem.Int32geographyKeyField;publicSystem.Int32GeographyKey
{get{return this.geographyKeyField; }set{this.geographyKeyField =value; }
}privateSystem.StringpostalCodeField;publicSystem.StringPostalCode
{get{return this.postalCodeField; }set{this.postalCodeField =value; }
}privateNullable<System.Int32> salesTerritoryKeyField;publicNullable<System.Int32> SalesTerritoryKey
{get{return this.salesTerritoryKeyField; }set{this.salesTerritoryKeyField =value; }
}privateSystem.StringspanishCountryRegionNameField;publicSystem.StringSpanishCountryRegionName
{get{return this.spanishCountryRegionNameField; }set{this.spanishCountryRegionNameField =value; }
}privateSystem.StringstateProvinceCodeField;publicSystem.StringStateProvinceCode
{get{return this.stateProvinceCodeField; }set{this.stateProvinceCodeField =value; }
}privateSystem.StringstateProvinceNameField;publicSystem.StringStateProvinceName
{get{return this.stateProvinceNameField; }set{this.stateProvinceNameField =value; }
}
}

创建CRUD的存储过程

利用Repository Factory可以很方便的生成针对数据表的CRUD存储过程,可以生成Insert、Update、Delete、GetAll、GetOne、GetMany六种类型的存储过程。

Step1:选择Create CRUD Stored Procedures菜单

TerryLee_RF_011

Step2:仍然是选择连接

TerryLee_RF_012

Step3:选择要生成存储过程的数据表

TerryLee_RF_013

Step4:设置是否生成上面所说的六种存储过程以及存储过程的名称

TerryLee_RF_014

Step5:设置存储过程的输出文件

TerryLee_RF_015

生成的存储过程部分代码:

----------------------------------------------------------------
-- [dbo].[DimGeography] Table
--IF NOT EXISTS(SELECT NAME FROMsys.objectsWHERETYPE ='P'AND NAME='InsertDimGeography')BEGIN
EXEC('CREATE PROCEDURE [dbo].[InsertDimGeography] AS RETURN')ENDGOALTER PROCEDURE[dbo].[InsertDimGeography]
@city nvarchar(30) =NULL,
@countryRegionCode nvarchar(3) =NULL,
@englishCountryRegionName nvarchar(50) =NULL,
@frenchCountryRegionName nvarchar(50) =NULL,
@geographyKeyintOUT,
@postalCode nvarchar(15) =NULL,
@salesTerritoryKeyint,
@spanishCountryRegionName nvarchar(50) =NULL,
@stateProvinceCode nvarchar(3) =NULL,
@stateProvinceName nvarchar(50) =NULL
AS
BEGIN
SET NOCOUNT ON
BEGINTRYINSERT INTO[dbo].[DimGeography] ([City], [CountryRegionCode],
[EnglishCountryRegionName], [FrenchCountryRegionName], [PostalCode],
[SalesTerritoryKey], [SpanishCountryRegionName], [StateProvinceCode], [StateProvinceName])VALUES(@city, @countryRegionCode, @englishCountryRegionName,
@frenchCountryRegionName, @postalCode, @salesTerritoryKey,
@spanishCountryRegionName, @stateProvinceCode, @stateProvinceName)SET@geographyKey = SCOPE_IDENTITY()ENDTRYBEGINCATCHEXECRethrowError;ENDCATCHSET NOCOUNT OFF
ENDGO

结束语

使用Repository Factory生成业务实体和存储过程,就介绍到这里,限于篇幅,分成了上下两篇,其他内容放在微软轻量级“代码生成器”—Repository Factory使用(下)。
参考:微软轻量级“代码生成器”—Repository Factory使用(下)

TAG: web client software factory mvp v-p

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar