概述
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,可以開啟指導包管理器:
Step2:選擇Repository Factory
Step3:同時還需要在項目上右鍵菜單中,選擇Specify project responsibility
Step4:分別選擇Business Entities Project、Data Access Project、Host Project。
添加資料庫連接
Step1:接下來需要添加資料庫連接
Step2:輸入資料庫連接串的名稱:
建立串連完成後,將會在設定檔添加代碼:
<connectionStrings><add name="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:建立業務實體
Step2:選擇資料庫連接字串
Step3:選擇資料表、視圖和欄位
Step4:設定實體的屬性了,可以設定業務實體的名稱,預設的是表名;設定業務實體的屬性名稱和該屬性是否為唯讀屬性
點擊完成後,會產生業務實體的代碼,並且為局部類型,這樣便於使用者在該業務實體上添加自己的一些操作,範例程式碼如下:
public partial class DimGeography{public DimGeography(){}public DimGeography(System.String city, System.String countryRegionCode, System.String englishCountryRegionName,
System.String frenchCountryRegionName, System.Int32 geographyKey, System.String postalCode,
Nullable<System.Int32> salesTerritoryKey, System.String spanishCountryRegionName, System.String stateProvinceCode,
System.String stateProvinceName){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;}private System.String cityField;public System.String City{get { return this.cityField; }set { this.cityField = value; }}private System.String countryRegionCodeField;public System.String CountryRegionCode{get { return this.countryRegionCodeField; }set { this.countryRegionCodeField = value; }}private System.String englishCountryRegionNameField;public System.String EnglishCountryRegionName{get { return this.englishCountryRegionNameField; }set { this.englishCountryRegionNameField = value; }}private System.String frenchCountryRegionNameField;public System.String FrenchCountryRegionName{get { return this.frenchCountryRegionNameField; }set { this.frenchCountryRegionNameField = value; }}private System.Int32 geographyKeyField;public System.Int32 GeographyKey{get { return this.geographyKeyField; }set { this.geographyKeyField = value; }}private System.String postalCodeField;public System.String PostalCode{get { return this.postalCodeField; }set { this.postalCodeField = value; }}private Nullable<System.Int32> salesTerritoryKeyField;public Nullable<System.Int32> SalesTerritoryKey{get { return this.salesTerritoryKeyField; }set { this.salesTerritoryKeyField = value; }}private System.String spanishCountryRegionNameField;public System.String SpanishCountryRegionName{get { return this.spanishCountryRegionNameField; }set { this.spanishCountryRegionNameField = value; }}private System.String stateProvinceCodeField;public System.String StateProvinceCode{get { return this.stateProvinceCodeField; }set { this.stateProvinceCodeField = value; }}private System.String stateProvinceNameField;public System.String StateProvinceName{get { return this.stateProvinceNameField; }set { this.stateProvinceNameField = value; }}}
建立CRUD的預存程序
利用Repository Factory可以很方便的產生針對資料表的CRUD預存程序,可以產生Insert、Update、Delete、GetAll、GetOne、GetMany六種類型的預存程序。
Step1:選擇Create CRUD Stored Procedures菜單
Step2:仍然是選擇串連
Step3:選擇要產生預存程序的資料表
Step4:設定是否產生上面所說的六種預存程序以及預存程序的名稱
Step5:設定預存程序的輸出檔案
產生的預存程序部分代碼:
------------------------------------------------------------------ [dbo].[DimGeography] Table--IF NOT EXISTS (SELECT NAME FROM sys.objects WHERE TYPE = 'P' AND NAME = 'InsertDimGeography')BEGINEXEC('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,@geographyKey int OUT,@postalCode nvarchar(15) = NULL,@salesTerritoryKey int,@spanishCountryRegionName nvarchar(50) = NULL,@stateProvinceCode nvarchar(3) = NULL,@stateProvinceName nvarchar(50) = NULLASBEGINSET NOCOUNT ONBEGIN TRYINSERT 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()END TRYBEGIN CATCHEXEC RethrowError;END CATCHSET NOCOUNT OFFENDGO結束語
使用Repository Factory產生業務實體和預存程序,就介紹到這裡,限於篇幅,分成了上下兩篇,其他內容放在微軟輕量級“代碼產生器”—Repository Factory使用(下)。
參考:微軟輕量級“代碼產生器”—Repository Factory使用(下)