Running the CodeSmith generation tool using the NetTiers templates is a piece of cake if we're going to talk about generating code for database tables. However, when it comes to custom objects being generated by CodeSmith, it gets a bit rocky but it is still doable J
In order to generate code that would encapsulate a stored procedure that you created, you need to follow the steps below:
- Your procedure name must be discoverable, meaning that you should set certain settings in CodeSmith so that it could find it during generation. Follow the couple of points below to set the configurations in CodeSmith
- IncludeCustoms=true
- ProcedurePrefix=<EnterYourProcedurePrefix>
- CustomProcedureStartsWith=<EnterYourPattern>
This pattern uses two placeholders: {0} for the TableName and {1} for the ProcedurePrefix (we've just talked about). So if we need to match the procedure name "myCusProc_myTableName_MethodNameToGenerate", we'll have to set {1} to be "myCusProc_" and appended to it would be the <TableName>. Therefore, the final template we're going to use for this property, CustomProcedureStartsWith, would look like that: {1}{0}_
- CustomNonMatchingReturnType=IDataReader|DataSet. You'll have to choose one of these datatypes to return your data in. Usually, I prefer IDataReader.
- If you want to generate code for your custom procedures only (i.e: You don't need to re-generate for all your database), then you should set the property ExecuteSQL=false, and the property SourceTables=<TheTablesThatWillContainTheNewlyGeneratedProceduresFunctions>.
- Finally, you can verify that CodeSmith generated code for your procedures by checking the Data layer in the base directory for the TableNameProviderBase.generatedCore.cs.
- IncludeCustoms=true
Enjoy generating your code J