13.4. The Code Module

This section generates the code module, adds it to the project, and opens the file in the design-time environment:

//Check if the user is creating a data or business class.
switch (CustomParams[0].ToString())
{
    case "Data":
        code = CreateDataClass(tableName, defaultNamespace, schema);
        fileName = tableName + "Data.cs";
        break;
    case "Business":
        code = CreateBusinessClass(tableName, defaultNamespace, schema);
        fileName = tableName + "EO.cs";
        break;
    case "StoredProcedures":
        code = CreateStoredProcedures(tableName, schema);
        fileName = tableName + "SP.sql";
        break;
}

//Write the file to disk.
StreamWriter sw = new StreamWriter(newItemDir + fileName);
sw.Write(code);
sw.Close();

//Add the file to the project.
ProjectItem newfile = projectItems.AddFromFile(newItemDir + fileName);

// Open file so that the user can start editing it
dbSelector.Designer.ExecuteCommand("File.OpenFile", "\"" + newItemDir + fileName +
                                    "\"");

The CustomParams are passed in via the .vsz file that you will be creating. Three .vsz files are created: one for the stored procedures, one for the data class, and one for the business class. Each .vsz file will create this same object but it will pass in a different parameter to signal which file should be created. The switch statement is used to determine which type of file should be created and then it calls another method to generate the string that should be saved in the file. After the code has been written to the string, it is persisted to the disk ...

Get ASP.NET 3.5 Enterprise Application Development with Visual Studio® 2008: Problem - Design - Solution now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.