Created
February 14, 2014 16:01
-
-
Save jbubriski/9003634 to your computer and use it in GitHub Desktop.
T4 template for creating strongly typed classes for Kentico Custom Tables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template debug="True" hostspecific="True" language="C#" #> | |
<#@ output extension=".cs" #> | |
<#@ Assembly Name="System.Configuration" #> | |
<#@ assembly name="System.Core" #> | |
<#@ Assembly Name="System.Collections" #> | |
<#@ Assembly Name="System.Data" #> | |
<#@ Assembly Name="System.Linq" #> | |
<#@ Assembly Name="System.Xml" #> | |
<#@ Assembly Name="System.Xml.Linq" #> | |
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #> | |
<#@ assembly name="Microsoft.SqlServer.Smo" #> | |
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #> | |
<#@ import namespace="System.Collections" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ Import Namespace="System.Configuration" #> | |
<#@ Import Namespace="System.Data" #> | |
<#@ import namespace="System.Data.SqlClient" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ Import Namespace="System.Xml" #> | |
<#@ Import Namespace="System.Xml.Linq" #> | |
<# | |
var namespaceName = "CurriculumAssociates.Models.CustomTables"; | |
#> | |
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Linq; | |
using System.Text; | |
using CMS.GlobalHelper; | |
using CMS.SiteProvider; | |
namespace <#= namespaceName #> | |
{ | |
<# | |
var path = Host.ResolvePath(@".."); | |
var config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path + @"\app.config" }, ConfigurationUserLevel.None); | |
var connectionString = config.ConnectionStrings.ConnectionStrings["CMSConnectionString"].ConnectionString; | |
var sqlCommandText = @"SELECT ClassDisplayName, ClassName, ClassFormDefinition | |
FROM CMS_Class | |
WHERE ClassIsCustomTable = 1"; | |
using (var sqlConnection = new SqlConnection(connectionString)) | |
using (var sqlCommand = new SqlCommand(sqlCommandText, sqlConnection)) | |
{ | |
sqlCommand.Connection.Open(); | |
using (var reader = sqlCommand.ExecuteReader()) | |
{ | |
while (reader.Read()) | |
{ | |
var displayName = reader["ClassDisplayName"].ToString(); | |
var className = reader["ClassName"].ToString(); | |
var classFormDefinition = reader["ClassFormDefinition"].ToString(); | |
var formDefinitionXml = XDocument.Parse(classFormDefinition); | |
var displayNameItems = displayName.Split(' '); | |
for(var i = 0; i < displayNameItems.Length; i++) | |
{ | |
displayNameItems[i] = displayNameItems[i][0].ToString().ToUpper() + displayNameItems[i].Substring(1).ToLower(); | |
} | |
displayName = string.Join("", displayNameItems) + "Item"; | |
#> | |
public partial class <#= displayName #> : CustomTableItem | |
{ | |
public const string CLASS_NAME = "<#= className #>"; | |
<# | |
foreach (var xField in formDefinitionXml.Elements("form").Elements("field")) | |
{ | |
var columnName = xField.Attribute("column").Value; | |
var columnType = xField.Attribute("columntype").Value; | |
var isSystem = xField.Attributes("system").Any(); | |
if (columnName != "ItemID" && !isSystem) | |
{ | |
if (columnType == "text") | |
{ | |
#> | |
public string <#= columnName #> | |
{ | |
get | |
{ | |
return ValidationHelper.GetString(GetValue("<#= columnName #>"), ""); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
else if (columnType == "integer") | |
{ | |
#> | |
public int <#= columnName #> | |
{ | |
get | |
{ | |
return ValidationHelper.GetInteger(GetValue("<#= columnName #>"), 0); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
else if (columnType == "double") | |
{ | |
#> | |
public decimal <#= columnName #> | |
{ | |
get | |
{ | |
return this.GetDecimalValue("<#= columnName #>", 0); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
else if (columnType == "boolean") | |
{ | |
#> | |
public bool <#= columnName #> | |
{ | |
get | |
{ | |
return ValidationHelper.GetBoolean(GetValue("<#= columnName #>"), false); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
else if (columnType == "guid") | |
{ | |
#> | |
public Guid <#= columnName #> | |
{ | |
get | |
{ | |
return ValidationHelper.GetGuid(GetValue("<#= columnName #>"), Guid.Empty); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
else if (columnType == "datetime") | |
{ | |
#> | |
public DateTime <#= columnName #> | |
{ | |
get | |
{ | |
return ValidationHelper.GetDateTime(GetValue("<#= columnName #>"), DateTime.MinValue); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
} | |
} | |
#> | |
public <#= displayName #>() : base(CLASS_NAME) | |
{ | |
} | |
public static <#= displayName #> GetItem(int itemId) | |
{ | |
var customTableItemProvider = new CustomTableItemProvider(); | |
var items = customTableItemProvider.GetItems(<#= displayName #>.CLASS_NAME, "ItemId = " + itemId, null); | |
if (!DataHelper.DataSourceIsEmpty(items)) | |
{ | |
return <#= displayName #>.New<<#= displayName #>>(items.Tables[0].Rows[0], <#= displayName #>.CLASS_NAME, customTableItemProvider); | |
} | |
return null; | |
} | |
public static List<<#= displayName #>> GetItems(string where) | |
{ | |
var customTableItemProvider = new CustomTableItemProvider(); | |
var items = customTableItemProvider.GetItems(<#= displayName #>.CLASS_NAME, where, null); | |
var list = new List<<#= displayName #>>(); | |
if (!DataHelper.DataSourceIsEmpty(items)) | |
{ | |
foreach(DataRow dataRow in items.Tables[0].Rows) | |
{ | |
list.Add(<#= displayName #>.New<<#= displayName #>>(dataRow, <#= displayName #>.CLASS_NAME, customTableItemProvider)); | |
} | |
return list; | |
} | |
return list; | |
} | |
} | |
<# | |
} | |
} | |
} | |
#> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment