日期:2014-05-18  浏览次数:20349 次

CodeSmith for Nhibernate的问题:它是如何实现NHibernate.cst这个模板文件中引用NHibernate.class.cst这个模板中的代码的?
如何在NHibernate.cst这个模板文件中引用NHibernate.class.cst这个模板文件?
我们生成代码时只要Execute   NHibernate.cst这一个模板就好了,分析代码我发现,NHibernate.cst这个模板显然是用到了NHibernate.class.cst和NHibernate.hbm.cst这个模板中的代码,但我怎么也没有找到NHibernate.cst文件对这两个模板的引用;

比如在C#中,如果要引用另一个文件中的方法,怎么也应该   using   一下这个类,但CodeSmith中既没看到对这两个模板的   Import,也没看到   Assembly;
请问CodeSmith是如何实现对另一个模板文件的引用的?

------解决方案--------------------
public void Generate()
{
foreach(TableSchema sourceTable in SourceDatabase.Tables)
{
Response.Write(string.Format( "Processing Table {0} ... ", sourceTable.Name));
if (IsManyToManyTable(sourceTable))
{
Response.WriteLine( "skipped, many-to-many link table ");
}
else if (IsSubClassTable(sourceTable))
{
Response.WriteLine( "skipped, sub-class table ");
}
else if (sourceTable.PrimaryKey == null)
{
Response.WriteLine( "skipped, no primary key ");
}
else
{
try
{
string className = sourceTable.Name;
if (className.StartsWith(RemoveTablePrefix))
className = className.Substring(RemoveTablePrefix.Length);

string classFileName = className;
classFileName += ".cs ";
classFileName = Path.Combine(OutputDirectory, classFileName);

string mappingFileName = className + ".hbm.xml ";
mappingFileName = Path.Combine(OutputDirectory, mappingFileName);

this.ClassTemplate.SetProperty( "SourceTable ", sourceTable);
this.ClassTemplate.SetProperty( "Namespace ", Namespace);
this.ClassTemplate.SetProperty( "Assembly ", Assembly);
this.ClassTemplate.SetProperty( "RemoveTablePrefix ", RemoveTablePrefix);
this.ClassTemplate.SetProperty( "ForceId ", ForceId);

this.ClassTemplate.RenderToFile(classFileName, true);

Response.Write(string.Format( "{0} ", classFileName));

this.MappingTemplate.SetProperty( "SourceTable ", sourceTable);
this.MappingTemplate.SetProperty( "Namespace ", Namespace);
this.MappingTemplate.SetProperty( "Assembly ", Assembly);
this.MappingTemplate.SetProperty( "RemoveTablePrefix ", RemoveTablePrefix);
this.MappingTemplate.SetProperty( "ForceId ", ForceId);

this.MappingTemplate.RenderToFile(mappingFileName, true);

Response.WriteLine(string.Format( "{0} ", mappingFileName));

}
catch (Exception ex)
{
Response.WriteLine( "Error: " + ex);
}
}
}
}


不知道这个够不够