先在根据需求分析,概要设计文档,用PowerDesigner设计概念模型CDM(E-R图),后用PowerDesigner 转换为物理模型PDM,再由PDM生成数据库脚本(或直接导成库),然后生成库.
生成库后利用Castle的自动生成器生成业务模型类(Castle.ActiveRecord.Generator).
举例:多对多关系业务模型类
1.CDM:People; Company
2.CDM -->PDM :PDM自动生成 People,Company的对照表,PeopleCompany
3.PDM --> SqlServer: People,Company,PeopleCompany
4.SqlServer --> Model: 由Castle.ActiveRecord.Generator生成的MODEL,也为三个
实际的MOEDL应为2个,如
[ActiveRecord("company")]
public class Company : ActiveRecordBase
{
private int id;
private String name;
private IList _people;
public Company()
{
}
public Company(string name)
{
this.name = name;
}
[PrimaryKey]
public int Id
{
get { return id; }
set { id = value; }
}
[Property]
public String Name
{
get { return name; }
set { name = value; }
}
[HasAndBelongsToMany( typeof(Person),
Table="people_companies",
ColumnRef="person_id", ColumnKey="company_id" )]
public IList People
{
get { return _people; }
set { _people = value; }
}
}
[ActiveRecord("people")]
public class Person : ActiveRecordBase
{
private int _id;
private String _name;
private IList _companies;
public Person()
{
_companies = new ArrayList();
}
[PrimaryKey]
public int Id
{
get { return _id; }
set { _id = value; }
}
[Property]
public string Name
{
get { return _name; }
set { _name = value; }
}
[HasAndBelongsToMany( typeof(Company),
Table="people_companies",
ColumnRef="company_id", ColumnKey="person_id" )]
public IList Companies
{
get { return _companies; }
set { _companies = value; }
}
}