

After rotating it
Brian O'Neil's Blog named in honor of OK Foundry. The Cope is the top and the Drag is the bottom, and if you want to know more you'll have to go to Richmond and buy some castings from my brother.

<BaseballConfig>
<WorldSeriess>
<add Year="2008">
<Teams>
<add Name="Rays" />
</Teams>
</add>
</WorldSeriess>
</BaseballConfig>
<BaseballConfig>
<WorldSeriess>
<WorldSeries Year="2008">
<Teams>
<Team Name="Phillies" />
<Team Name="Rays" />
</Teams>
</WorldSeries>
</WorldSeriess>
</BaseballConfig>
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
private static void addConfigurationElementCollectionTypeProperty(CodeTypeDeclaration configElements)
{
CodeTypeReferenceExpression ctre = new CodeTypeReferenceExpression(typeof(ConfigurationElementCollectionType));
CodeTypeReference ctr =
new CodeTypeReference(typeof(ConfigurationElementCollectionType));
CodeMemberProperty propCollectionType = new CodeMemberProperty();
propCollectionType.Name = "CollectionType";
propCollectionType.Attributes = MemberAttributes.Override | MemberAttributes.Public;
propCollectionType.HasSet = false;
propCollectionType.HasGet = true;
propCollectionType.Type = ctr;
propCollectionType.GetStatements.Add(
new CodeMethodReturnStatement(
new CodePropertyReferenceExpression(ctre, "BasicMap")));
configElements.Members.Add(propCollectionType);
}
public static string GetMappingFile(SqlConnection Connection, string TableName)
{
string fileName = TableName + "hbm.xml";
hibernatemapping newMapping = new hibernatemapping();
newMapping.@namespace = "Acme.DataLayer";
newMapping.@class = new hibernatemappingClass();
hibernatemappingClass newClass = newMapping.@class;
newClass.name = TableName;
newClass.table = TableName;
Columns cols = PopulateColumns(Connection, TableName, CommandType.TableDirect);
newClass.property = new hibernatemappingClassProperty[cols.Column.Count];
int index = 0;
foreach (Columns.ColumnRow cRow in cols.Column)
{
newClass.property[index] = new hibernatemappingClassProperty();
hibernatemappingClassProperty prop = newClass.property[index];
prop.column = cRow.Column_Name;
prop.name = cRow.Column_Name;
prop.type = cRow.Data_Type;
switch (cRow.Data_Type.ToUpper())
{
case "DECIMAL":
break;
case "INT":
prop.type = "Int32";
break;
case "SMALLINT":
prop.type = "Int32";
break;
case "BIT":
prop.type = "Int32";
break;
case "VARCHAR":
prop.type = "string";
break;
default:
prop.type = "string";
break;
}
index++;
}
System.Xml.Serialization.XmlSerializer xmlSerializer =
new System.Xml.Serialization.XmlSerializer(typeof(hibernatemapping));
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
xmlSerializer.Serialize(sw, newMapping);
return sb.ToString();
}
public static string GetClientClass(SqlConnection Connection, string TableName)
{
CodeTypeReference codeTypeRef;
CodeMemberField memberField;
CodeMemberProperty memberPropPublic;
Columns cols = PopulateColumns(Connection, TableName, CommandType.TableDirect);
CodeNamespace codeNS = new CodeNamespace("ACME.Data");
CodeTypeDeclaration ct = new CodeTypeDeclaration(TableName);
ct.IsClass = true;
codeNS.Types.Add(ct);
CodeExpression markDirty = new CodeMethodInvokeExpression(
new CodeThisReferenceExpression(),
"MarkDirty",
new CodeExpression[] { });
foreach (Columns.ColumnRow cRow in cols.Column)
{
switch (cRow.Data_Type.ToUpper())
{
case "DECIMAL":
codeTypeRef = new CodeTypeReference("System.Decimal");
break;
case "INT":
codeTypeRef = new CodeTypeReference("System.Int32");
break;
case "SMALLINT":
codeTypeRef = new CodeTypeReference("System.Int32");
break;
case "BIT":
codeTypeRef = new CodeTypeReference("System.Int32");
break;
case "VARCHAR":
codeTypeRef = new CodeTypeReference("System.String");
break;
default:
codeTypeRef = new CodeTypeReference("System.String");
break;
}
memberField = new CodeMemberField(
codeTypeRef,
"_" + cRow.Column_Name.ToLower());
memberField.Attributes = MemberAttributes.Private;
ct.Members.Add(memberField);
memberPropPublic = new CodeMemberProperty();
memberPropPublic.Name = MakeFieldName(cRow.Column_Name);
memberPropPublic.Attributes = MemberAttributes.Public;
memberPropPublic.Type = codeTypeRef;
ct.Members.Add(memberPropPublic);
// Get Statement
CodeVariableReferenceExpression cvrexp =
new CodeVariableReferenceExpression("_" + cRow.Column_Name.ToLower());
CodeMethodReturnStatement cmrstmnt = new CodeMethodReturnStatement(cvrexp);
memberPropPublic.GetStatements.Add(cmrstmnt);
// Set Statement
memberPropPublic.SetStatements.Add(
new CodeAssignStatement(cvrexp,
new CodePropertySetValueReferenceExpression()));
memberPropPublic.SetStatements.Add(markDirty);
}
CSharpCodeProvider cSharpProvider = new CSharpCodeProvider();
StringBuilder sb = new StringBuilder();
System.IO.TextWriter tw = new System.IO.StringWriter(sb);
cSharpProvider.GenerateCodeFromNamespace(codeNS,
tw,
new System.CodeDom.Compiler.CodeGeneratorOptions());
return sb.ToString();
}
private static Columns PopulateColumns(SqlConnection Connection,
string TableName,
CommandType TableOrSproc)
{
Columns cols = new Columns();
Connection.Open();
SqlCommand sqlCmd = new SqlCommand();
if(TableOrSproc == CommandType.StoredProcedure)
{
// Get Parameters for Command.
sqlCmd = new SqlCommand(
"Select Parameter_Name as Column_Name,Data_Type, Character_maximum_length" +
" from Information_Schema.Parameters Where Specific_Name = '"
+ TableName +
"'",
Connection);
}
else
{
// Get Columns for the Table.
sqlCmd = new SqlCommand(
"Select Column_Name,Data_Type,Character_maximum_length" +
" from Information_Schema.Columns Where Table_Name = '"
+ TableName +
"'",
Connection);
}
sqlCmd.CommandType = CommandType.Text;
SqlDataReader dr = sqlCmd.ExecuteReader();
while (dr.Read())
{
Columns.ColumnRow row = cols.Column.NewColumnRow();
row.Column_Name = dr["Column_Name"].ToString();
row.Data_Type = dr["Data_Type"].ToString();
row.Character_maximum_length = dr["Character_maximum_length"].ToString();
cols.Column.AddColumnRow(row);
}
dr.Close();
Connection.Close();
return cols;
}