如何使用CslaGen生成CSLA DAL层代码

原文:How to Use CslaGen to Generate CSLA Data Access Layer Code

 

最近用起了csla,这东东没个代码生成感觉很不方便。找到了CslaGen这个工具,

优点嘛,1,图形化界面,设计BO方便;2,设计文档用xml保存,便于管理。

缺点嘛,只有DAL层的代码,权限、验证这些还得找其他模板了

 

本示例中的cslagen版本:Nightly Build 2012.03.11

相关工具的下载:

1,CslaGen

2,CodeSmith插件

1.数据库结构(如图1),由Mother表和Daughter表组成,后者通过Daughter字段关联到前者。

                         图1

 

ExpandedBlockStart.gif Sql Script
/* ***** Object:  Table [dbo].[Mother]    Script Date: 08/07/2007 08:07:46 ***** */    
SET ANSI_NULLS  ON    
GO    
SET QUOTED_IDENTIFIER  ON    
GO    
CREATE  TABLE  [ dbo ]. [ Mother ](    
     [ ID ]  [ int ]  IDENTITY( 1, 1NOT  NULL,
     [ Forename ]  [ nvarchar ]( 15NOT  NULL,
     [ Surname ]  [ nvarchar ]( 50NOT  NULL,
     [ BirthDate ]  [ datetime ]  NOT  NULL,
     [ BirthPlace ]  [ nvarchar ]( 30NOT  NULL,
     [ LastChanged ]  [ timestamp ]  NOT  NULL,
  CONSTRAINT  [ PK_Person ]  PRIMARY  KEY  CLUSTERED     
(    
     [ ID ]  ASC
) WITH (PAD_INDEX   =  OFF, STATISTICS_NORECOMPUTE   =  OFF, IGNORE_DUP_KEY  =  OFF, ALLOW_ROW_LOCKS   =  ON, ALLOW_PAGE_LOCKS   =  ONON  [ PRIMARY ]    
ON  [ PRIMARY ]    
    
    
/* ***** Object:  Table [dbo].[Daughter]    Script Date: 08/07/2007 08:07:26 ***** */    
SET ANSI_NULLS  ON    
GO    
SET QUOTED_IDENTIFIER  ON    
GO    
CREATE  TABLE  [ dbo ]. [ Daughter ](    
     [ DaughterID ]  [ int ]  IDENTITY( 1, 1NOT  NULL,
     [ Forename ]  [ nvarchar ]( 15NOT  NULL,
     [ Surname ]  [ nvarchar ]( 50NOT  NULL,
     [ BirthDate ]  [ datetime ]  NOT  NULL,
     [ BirthPlace ]  [ nvarchar ]( 30NOT  NULL,
     [ MotherID ]  [ int ]  NOT  NULL,
     [ LastChanged ]  [ timestamp ]  NOT  NULL,
  CONSTRAINT  [ PK_Child ]  PRIMARY  KEY  CLUSTERED     
(    
     [ DaughterID ]  ASC
) WITH (PAD_INDEX   =  OFF, STATISTICS_NORECOMPUTE   =  OFF, IGNORE_DUP_KEY  =  OFF, ALLOW_ROW_LOCKS   =  ON, ALLOW_PAGE_LOCKS   =  ONON  [ PRIMARY ]    
ON  [ PRIMARY ]    
    
GO    
ALTER  TABLE  [ dbo ]. [ Daughter ]   WITH  CHECK  ADD   CONSTRAINT  [ FK_Daughter_Mother ]  FOREIGN  KEY( [ MotherID ])    
REFERENCES  [ dbo ]. [ Mother ] ( [ ID ])    
GO    
ALTER  TABLE  [ dbo ]. [ Daughter ]  CHECK  CONSTRAINT  [ FK_Daughter_Mother ]    
    
INSERT  INTO Mother    
    (Forename, Surname, BirthDate, BirthPlace)
VALUES    
    ( ' Josefina '' Silva '' 01-01-1950 0:00:00 '' Braga ')
    
INSERT  INTO Mother    
    (Forename, Surname, BirthDate, BirthPlace)
VALUES    
    ( ' Maria Antónia '' Cazenga '' 01-01-1940 0:00:00 '' Malanje ')
    
INSERT  INTO Mother    
    (Forename, Surname, BirthDate, BirthPlace)
VALUES    
    ( ' Sarah '' Johnson '' 01-01-1960 0:00:00 '' London ')
    
INSERT  INTO Mother    
    (Forename, Surname, BirthDate, BirthPlace)
VALUES    
    ( ' Françoise '' Auteil '' 01-01-1965 0:00:00 '' Marseille ')
    
INSERT  INTO Mother    
    (Forename, Surname, BirthDate, BirthPlace)
VALUES    
    ( ' Martha '' Kent '' 01-01-1961 0:00:00 '' Smallville ')
    
INSERT  INTO Mother    
    (Forename, Surname, BirthDate, BirthPlace)
VALUES    
    ( ' Pascale '' Chaumier '' 01-01-1975 0:00:00 '' Avignon ')
    
INSERT  INTO Daughter    
    (Forename, Surname, BirthDate, BirthPlace, MotherID)
VALUES    
    ( ' Antonieta '' Silva '' 01-01-1970 0:00:00 '' Gondomar '' 1 ')
    
INSERT  INTO Daughter    
    (Forename, Surname, BirthDate, BirthPlace, MotherID)
VALUES    
    ( ' Ricardina '' Silva '' 01-01-1973 0:00:00 '' Porto '' 1 ')
    
INSERT  INTO Daughter    
    (Forename, Surname, BirthDate, BirthPlace, MotherID)
VALUES    
    ( ' Inácia '' Cazenga '' 01-01-1960 0:00:00 '' Cabinda '' 2 ')
    
INSERT  INTO Daughter    
    (Forename, Surname, BirthDate, BirthPlace, MotherID)
VALUES    
    ( ' Francisca '' Cazenga '' 01-01-1961 0:00:00 '' Luanda '' 2 ')
    
INSERT  INTO Daughter    
    (Forename, Surname, BirthDate, BirthPlace, MotherID)
VALUES    
    ( ' Marianne '' Chaumier '' 01-01-1995 0:00:00 '' Le Thor '' 6 ')
    
INSERT  INTO Daughter    
    (Forename, Surname, BirthDate, BirthPlace, MotherID)
VALUES    
    ( ' Nadine '' Chaumier '' 01-01-2000 0:00:00 '' Pernes-les-Fontaines '' 6 ')

2.BO层次结构(见图2)

                                                     图2

假设Mother,Daughter都是很大对象,有一堆的属性,载入他们的集合对象,开销会很大。

通常我们会设计一个轻量级的abcInfo对象(只定义些Mother/Daughter的必要信息,比如名字什么的)

,然后载入abcInfo的集合对象abcList。注意abcList和abcInfo都只是Readonly对象.

这里,MotherList是Readonly Root Collection对象,他的元素MotherInfo是Readonly Child。

要编辑某个母亲时,载入Mother这个Editable Root对象。Mother Root对象有DaughterList(Readonly Child Collection)这么个属性,后者是由DaughterInfo(Readonly Child对象组成),当编辑某个女儿时,

载入Daughter这个Editable Root对象。

3.创建ReadOnly Root Collection对象

先添加一个新项目,File/New,然后连到数据库上"Connect to Database"

3.1. 在"Schema Objects"中, 选择Mother表.
3.2. 在"Columns"中, 选择ID, Forename, Surname and LastChanged这几个列
3.3. 右键菜单中点Create Read Only -> ReadOnly Root Collection


3.4. 在"New Object Defaults"界面,"CollectionName”栏输入MotherList,"ItemName" 栏输入MotherInfo.
3.5. 给MotherList对象添加criterion

    3.5.1. 在"Csla Objects"中选择"MotherList"
    3.5.2. 在"Csla Object Info"/"03. Criteria",点"Criteria Objects",见图3.5.2-1

                                                          图3.5.2-1
    3.5.3.在"Criteria Collection Editor"界面, 添加一个名为"AllCollection" 的Criteria(你也可以使用其他名字).
    3.5.4. 在"Misc"/"cGetOptions",设置"DataPortal", "Factory","Procedure"属性为True, ("ProcedureName" 会自动变成GetMotherList ),然后点OK

3.6. 再添加一个criterion,根据姓/名查找母亲。

    3.6.1. 重复3.5.1 to 3.5.4,Name栏输入name
    3.6.2. "ProcedureName"栏输入GetMotherListByName.
    3.6.3. 点"Criteria Properties" .


   

     3.6.4. 点"DbBindColumn".
    3.6.5. 在"Schema Objects"界面, 选择Mother表的Forename字段
    3.6.6. 然后再添加Surname.

 

 

 

4.创建Mother(Editable Root)

    4.1. 在”Schema Objects", 选择Mother表, 右键菜单中点"Create Editable Root".

    4.2. 在"09. System.Object Overrides", 把"ToString”栏的下拉菜单中选中Forename和Surname

  

5.创建DaughterList(Readonly Child Collection),DaughterInfo(Readonly Child)

    5.1. 在“Schema Objects”, 选择"Daughter"表.
    5.2. 在"Columns",选择DaughterID, Forename, Surname, MotherID, LastChanged这几个字段

    5.3. 右键菜单中,点Create Readonly -> Read Only Collection -> Read Only Child Collectioin 

    5.4. 在"New Object Defaults",CollectionName栏输入DaughterList,ItemName中输入DaughterInfo.
    5.5. Parent Type下拉菜单中选中Mother
    5.6.然后要把Mother对象与这个DaughterList对象关联起来
        5.6.1. "Property Name In Parent T" 输入DaughterList

 

6.创建Daughter(Editable Root Object)

    6.1. 在"Schema Objects", 选择Mother表,右键菜单中点"Create Editable Root". 

    6.2. "09. System.Object Overrides", 修改"ToString"属性,选择Forename,Surname.

至此,所有BO对象都已经建好。选择代码输出路径"Output Directory",然后点"generate"生成代码

 

 

 


转载于:https://www.cnblogs.com/wordmy/archive/2012/04/01/2429311.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值