經過不斷的完善,代碼生成器已經實現了一些功能,并做好了相應的插件接口,版本1提供了以下接口:數據源(Source)、模板(Template)、另存為(SaveAs)、外接工具(Tool)和數據擴展(DataExtend)五個插件接口,并做了相應的示例。

一、數據源
目前只實現了從OleDB和PowerDesign中獲取表信息,其他方式大家可以進行研究,比如Visio的,還有ERwin的。
數據源菜單

OleDb數據管理器,可以自動生成連接字符串。

打開數據源后,讀出所有的表結構,如果要列出視圖,請在“工具”--“選項”中配置。

右邊“屬性”窗口可以對表及字段進行定義,比如外鍵、枚舉等。
外鍵編輯器,在“屬性”窗口中單擊外鍵編輯按鈕,進行對應關系的編輯。

枚舉編輯器,為實體屬性定義枚舉,同樣在“屬性”窗口中單擊枚舉編輯器。

關于屬性類型,是由模板進行加載的,注意到在fbd_cs.bct中有convert="csconvert.config"這樣的定義,字段類型與屬性類型的轉換就在這個文件里,目前只配置了OleDb以及SqlServer的。
除此之外,在code目錄中,oracle_property.cs和sqlserver_property.cs分別獲取Oracle及SqlServer字段的默認值、說明等,在Source.Database.config中定義了各種OldDb提供者所要引用的代碼文件:
代碼
<config>
<provider id="SQLOLEDB" language="CSharp" filename="code\sqlserver_property.cs" />
<provider id="MSDAORA" language="CSharp" filename="code\oracle_property.cs" />
<datasource>
<name>FbTestDb</name>
<connectionString>Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=fbTesTdb;Data Source=.</connectionString>
</datasource>
</config>
二、模板
目前只做了一個模板,即Template.FbBase,但已經足夠強大了,分別又定義了HTML、Sql腳本、FaibClass.Data及NHibernate(由于本人沒有研究過它的映射,因此只定義了簡單的映射,更復雜的表間關系可以參考code中的相關代碼)。
Template.FbBase模板是由XML語言定義的,如下面這個示例:
代碼
<!-- 該文件由faib studio定義,版本為1.0 -->
<config name="生成MSSQL腳本" language="Sql" convert="csconvert.config">
<!-- 定義一些常量 -->
<defkeys>
<key name="Author">Faib</key>
</defkeys>
<!-- 定義函數集,引入代碼文件編譯 -->
<functions>
<!-- Name要與下面代碼中的$Name$對應
language=CSharp|VBasic
file=代碼文件
-->
<function name="ColumnSql" language="CSharp" for="Column" file="code\sqlcolumn.cs" />
</functions>
<!-- 定義生成部份 -->
<parts>
<!-- 部份1
file=保存路徑
loop=None|Tables|Columns|PrimaryKeys|SubObjects|SubObjects|Enums|EnumValues 循環的對象體
-->
<part name="SQL" file="Design\create.sql" loop="None">
<!-- 定義節-->
<sections>
<section name="TableCreate" loop="Tables">
<sections>
<section name="ColumnArray" loop="Columns" endchar=",">
[$ColumnName$] $GetColumnSql()$</section>
<section name="ColumDescript" loop="Columns">
<![CDATA[$AddColumnDescription()$
go
]]>
</section>
<section name="PrimaryCreate" loop="PrimaryKeys">
<![CDATA[alter table $TableName$
add constraint PK_$TableName$_$ColumnName$ primary key ($ColumnName$)
go
]]>
</section>
</sections>
<!-- 代碼體 -->
<body>
<![CDATA[
create table [$TableName$] ($ColumnArray$
)
go
$PrimaryCreate$
-- Region 添加說明
$AddTableDescription()$
go
$ColumDescript$
-- EndRegion
]]>
</body>
</section>
<section name="ReferenceDelete" loop="Tables">
<sections>
<section name="ReferenceDelete1" loop="RefObjects">
<![CDATA[if exists (select 1
from sysobjects
where id = object_id('FK_$FKTableName$_$PKTableName$')
and type = 'F')
alter table $FKTableName$
drop constraint FK_$FKTableName$_$PKTableName$
go
]]>
</section>
</sections>
<body>
<![CDATA[$ReferenceDelete1$]]>
</body>
</section>
<section name="ReferenceCreate" loop="Tables">
<sections>
<section name="ReferenceCreate1" loop="RefObjects">
<![CDATA[alter table $FKTableName$
add constraint FK_$FKTableName$_$PKTableName$ foreign key ($FKColumnName$)
references $PKTableName$ ($PKColumnName$)
go
]]>
</section>
</sections>
<body>
<![CDATA[$ReferenceCreate1$]]>
</body>
</section>
<section name="TableDelete" loop="Tables">
<![CDATA[
if exists (select 1
from sysobjects
where id = object_id('$TableName$')
and type = 'U')
drop table [$TableName$]
go
]]>
</section>
</sections>
<body>
<![CDATA[
-- =======================================================
-- 本腳本由CodeBuilder工具生成
-- 版權所有 (C) Faib Studio 2009
--
-- 創建人員: $Author$
-- 創建時間: $Now$
-- =======================================================
-- Region 刪除關系
$ReferenceDelete$
-- End Region
-- Region 刪除表
$TableDelete$
-- End Region
-- Region 創建表
$TableCreate$
-- End Region
-- Region 創建關系
$ReferenceCreate$
-- End Region
]]>
</body>
</part>
</parts>
</config>
三、外接工具
可將工具掛接到CodeBuilder內,可以設置放在“工具”菜單、工具欄按鈕、表菜單以及托盤菜單。目前系統中一共做了三個工具:收藏夾、RSS訂閱以及表處理。
四、另存為
做了一個簡單的將架構保存到XML的插件。
五、數據擴展
如果你覺得現有的架構接口不夠你使用,那么可以自己做擴展,在這個版本里有CaitORMExtend示例,加載后會在“屬性”窗口的擴展處進行設置。

大概功能就這些,歡迎大家多多提出指定意見,小弟在此不勝感激。
下載1446版本 https://files.cnblogs.com/faib/CodeBuilder_1.0.0.1446_setup.rar

浙公網安備 33010602011771號