📄 templates.cs
字号:
//This file is part of ORM.NET.
//
//ORM.NET is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//ORM.NET is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with ORM.NET; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Reflection;
using System.Text;
using ORMBiz;
namespace ORM.NET
{
/// <summary>
/// TemplatesGenerator.
/// </summary>
public class TemplatesGenerator
{
private TemplateParser parser = new TemplateParser();
public TemplatesGenerator()
{
}
public string Generate(Database db)
{
string namespaceString = parser.LoadTemplate(@"Biz\Templates\Namespace");
StringBuilder sb = new StringBuilder();
foreach(Table t in db.Tables)
{
if ( t.GenerateCodeFor.ToLower() == "true" )
sb.Append( GetFormattedClassTemplate( t ) );
}
return parser.RestoreAfterFormatting( string.Format(namespaceString, Shared.NameSpace, sb.ToString() ) );
}
private bool HasValueAndIsNullProperty( string type )
{
Type t = null;
PropertyInfo pi = null;
if ( type.ToLower().StartsWith("system.data") )
{
t = Assembly.LoadWithPartialName("System.Data").GetType(type);
}
if ( t == null )
return false;
pi = t.GetProperty("Value");
if ( pi == null ) return false;
pi = t.GetProperty("IsNull");
if ( pi == null ) return false;
return true;
}
public string GetFormattedClassTemplate(Table table)
{
//string GetAccessor = "";
//string SetAccessor = "";
StringBuilder sbProperties = new StringBuilder();
StringBuilder sbParentProperties = new StringBuilder();
StringBuilder sbChildProperties = new StringBuilder();
StringBuilder sbCollections = new StringBuilder();
string FullyQualifiedAccessorName = Shared.NameSpace + "." + table.AccessorName;
string classTemplate = parser.LoadTemplate(@"Biz\Templates\Class");
// changes here will need to be reflected in the Colections Generator class
foreach(Column c in table.Columns)
{
GetColumnStatements(c, sbProperties);
}
foreach(Relation r in table.ParentRelations)
{
GetParentStatements(r,sbParentProperties);
}
foreach(Relation r in table.ChildRelations)
{
if ( r.ChildTable.GenerateCodeFor.ToLower() == "true" )
{
if (r.RelationshipType == "1:1")
{
sbChildProperties.Append( GetFormattedChildProperty( Shared.NameSpace + "." + r.ChildTable.AccessorName, r.ChildAccessorName, r.ParentAccessorName, r.ForeignKeyColumnName) );
if ( r.ChildGetAccessor.ToLower() != "none" )
sbChildProperties.Append( GetFormattedGetChildProperty( Shared.NameSpace + "." + r.ChildTable.AccessorName, r.ChildAccessorName, r.ParentAccessorName, r.ForeignKeyColumnName, r.ChildTable.Name) );
if ( r.ChildSetAccessor.ToLower() != "none" )
sbChildProperties.Append( GetFormattedSetChildProperty( Shared.NameSpace + "." + r.ChildTable.AccessorName, r.ChildAccessorName, r.ChildTable.Name, r.ForeignKeyColumnName) );
}
else
{
sbCollections.Append( GetFormattedCollection( Shared.NameSpace + "." + r.ChildTable.AccessorName, r.ForeignKeyColumnName, r.ChildAccessorName, r.ChildTable.Name, TemplateParser.GetScope(r.ChildGetAccessor), TemplateParser.GetScope(r.ChildSetAccessor)) );
if ( r.ChildGetAccessor.ToLower() != "none" )
sbCollections.Append( GetFormattedGetCollection( Shared.NameSpace + "." + r.ChildTable.AccessorName, r.ForeignKeyColumnName, r.ChildAccessorName, r.ChildTable.Name, TemplateParser.GetScope(r.ChildGetAccessor), TemplateParser.GetScope(r.ChildSetAccessor)) );
if ( r.ChildSetAccessor.ToLower() != "none" )
sbCollections.Append( GetFormattedSetCollection( Shared.NameSpace + "." + r.ChildTable.AccessorName, r.ForeignKeyColumnName, r.ChildAccessorName, r.ChildTable.Name, TemplateParser.GetScope(r.ChildGetAccessor), TemplateParser.GetScope(r.ChildSetAccessor)) );
}
}
}
return string.Format( classTemplate, table.AccessorName, sbProperties.ToString(), sbParentProperties.ToString(), sbChildProperties.ToString(), sbCollections.ToString());
}
public void GetParentStatements( Relation relation, StringBuilder sb )
{
if ( relation.ParentTable.GenerateCodeFor.ToLower() != "true" ) return;
// is its a required parent, set the attribute so we can pick it up in the collection
string requiredAttribute = "";
if ( relation.NullableFKey.ToLower() == "true" && relation.IsMandatory.ToLower() == "true" )
{
if ( Shared.GenerateVbCode )
{
requiredAttribute = "<OrmLib.RequiredParent>_";
}
else
{
requiredAttribute = "[OrmLib.RequiredParent]";
}
}
if ( relation.ParentTable.IsLookupTable.ToLower() == "true" && relation.ChildTable.IsLookupTable.ToLower() != "true")
{
string pkeyName = "";
string pkeyAccessor = "";
string pkeyDataType = "";
foreach(Column c in relation.ParentTable.Columns)
{
if (c.PrimaryKey.ToLower() == "true")
{
pkeyName = c.Name;
pkeyAccessor = c.AccessorName;
pkeyDataType = c.DataType.NetType;
}
}
sb.Append( GetFormattedLookupProperty( Shared.NameSpace + "." + relation.ParentTable.AccessorName, relation.ParentAccessorName, relation.ParentTable.AccessorName, pkeyName, pkeyAccessor, pkeyDataType, relation.ForeignKeyColumnName, requiredAttribute) );
}
else
{
// get and set same scope
if ( relation.ParentGetAccessor.ToLower() == relation.ParentSetAccessor.ToLower() && relation.ParentGetAccessor.ToLower() != "none" )
sb.Append( GetFormattedParentProperty( Shared.NameSpace + "." + relation.ParentTable.AccessorName, relation.ParentAccessorName, relation.ChildTable.Name, relation.ForeignKeyColumnName, TemplateParser.GetScope(relation.ParentGetAccessor), requiredAttribute) );
else
{
// public?
if ( relation.ParentSetAccessor.ToLower() == "public" )
sb.Append( GetFormattedParentPropertySet( Shared.NameSpace + "." + relation.ParentTable.AccessorName, relation.ParentAccessorName, relation.ChildTable.Name, relation.ForeignKeyColumnName, TemplateParser.GetScope(relation.ParentSetAccessor), requiredAttribute) );
else if ( relation.ParentSetAccessor.ToLower() != "none" )
sb.Append( GetFormattedParentPropertySetFn( Shared.NameSpace + "." + relation.ParentTable.AccessorName, relation.ParentAccessorName, relation.ChildTable.Name, relation.ForeignKeyColumnName, TemplateParser.GetScope(relation.ParentSetAccessor), requiredAttribute) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -