The current time is

Tuesday, November 17, 2009

CodeSmith & NetTiers Custom Stored Procedures

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:

  1. 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
    1. IncludeCustoms=true
    2. ProcedurePrefix=<EnterYourProcedurePrefix>
    3. 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}_

    4. CustomNonMatchingReturnType=IDataReader|DataSet. You'll have to choose one of these datatypes to return your data in. Usually, I prefer IDataReader.
    5. 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>.
    6. 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.


 

Enjoy generating your code J

9 comments:

  1. I've used CodeSmith and NetTiers in my Bachelor Project (",)
    it's nice :)

    ReplyDelete
  2. Cooool... Keep it up then and let me know if you need any help... :)

    ReplyDelete
  3. Hey,
    i used code smith couple of times with the same template you mention, but i would share with you the new era of .NET framework 3.5 which is linq!!
    It's more more convenient and reliable than code smith...check it out!

    ReplyDelete
  4. Tareq, thanks for your comment. Any links that you wanna share here?

    ReplyDelete
  5. I had to migrate my DEV database to a new server. Our DBA restored the PROD version of the database to the new server. Seems rather harmless, right? For some reason, Code Smith 4.1 has stopped recognizing all of my custom stored procedures. I verified that they are in fact in the database. Has anyone encountered this problem?

    ReplyDelete
  6. Hi fcatacutan, I hope my suggestion would benefit you... Are you using the same exact template settings you used to generate with, because maybe the custom procedures pattern is not set correctly. Of course, the connection string is correct, right? Let me know your updates then...

    ReplyDelete
  7. How is the custom stored procedure configured that is not tied to a table?

    ReplyDelete
  8. Altaf, netTiers would eventually generate a generic method (that returns an IDataReader) so that when the procedure is called, you can do whatever you want using "that" datareader... This method will be generated in the class for the table that YOU specify in the procedure name... Let me know if I'm not clear enough...

    ReplyDelete