January 2010 - Blake Niemyjski

  • How-to: Upgrade your PLINQO or CSLA templates

    This article will help you upgrade your solutions templates safely and easily to the latest major or minor version.  The latest templates can be found on our Google code project and the latest nightly build can be found here. The first thing to do is to download the template framework you wish to update.

    After the templates have been downloaded and extracted. Remove or Replace the old templates with the new ones keeping the same folder structure or simply update your CodeSmith Project File(s) to point to the updated templates. If you have custom made changes, this is where you merge in your changes. 

    I recommend using our templates via SVN. This will ensure easy upgradability while keeping any custom changes. To update all you need to do is right click and select update.

    The next step is to remove any template dependent references from your Visual Studio Project so the new references can be updated. These references would include the CodeSmith.Data assembly if you are using PLINQO or the Csla assembly. Both of these assemblies can be found in the Common folder in the root of the template directory. This can be done by clicking on your project references and clicking remove. The references will be automatically added when you run the templates. Please note that if the paths are the same then you can skip this step, but I recommend completing this step just to be on the safe side.

    Lastly, if you have any build breaks fix them by compiling. You shouldn’t have any build breaks but this can happen if you are migrating to the next major version (E.G. 4.0).

    If you encounter any bugs after upgrading please let us know by contacting support.

     

  • How-to: Use SQL CLR Functions and Functions in your CodeSmith templates.

    In a previous post I had mentioned that we added SQL CLR Stored procedures as well as SQL Functions in the release of CodeSmith 5.2. Since then there has been a few questions since then on how to add this to your existing templates. The great news is, you can add the functionality in a few easy changes.

    To enable SQL function support you need to set IncludeFunctions="True" on any types inheriting from SchemaObjectBase (E.G. CommandSchema, CommandSchemaCollection, DatabaseSchema...).

    Once this has been done you will see SQL functions be added to your User Interface Command pickers as well as show up in the API like DatabaseSchema.Commands.

    How do I check to see what type of SQL Function it a command is?

     

    1. CS_IsCLR: Returns true if the command is a CLR Procedure.
    2. CS_IsScalarFunction: Returns true if the command is a Scalar Function.
    3. CS_IsTableValuedFunction: Returns true if the command is a Table-Valued Function.
    4. CS_IsInlineTableValuedFunction: Returns true if the command is a Inline Table-Valued Function.
    5. CS_IsMultiStatementTableValuedFunction: Returns true if the command is a Multi-Statement Table-Valued Function.
    It is also easy to take different actions in your template logic based on the type of function.
    <% if(!IsTableValuedFunction) { %>
                    cmd.CommandText = "[<%= SourceCommand.Owner %>].[<%= SourceCommand.Name %>]";
                    cmd.CommandType = CommandType.StoredProcedure;
    <% } else {%>
                    cmd.CommandText = "SELECT * FROM [<%= SourceCommand.Owner %>].[<%= SourceCommand.Name %>](<%=BuildArgumentList()%>)";
                    cmd.CommandType = CommandType.Text;
    <%}%>
    public bool IsTableValuedFunction
    {
        get
        {
            if (SourceCommand == null || !SourceCommand.ExtendedProperties.Contains("CS_IsTableValuedFunction"))
                return false;
            bool temp;
            bool.TryParse(SourceCommand.ExtendedProperties["CS_IsTableValuedFunction"].Value.ToString(), out temp);
            return temp;
        }
    }
    <%}%>
    What templates can I observe these changes in?
    We have completely updated the Command wrapper templates to fully support SQL Functions. Please download the latest build of CodeSmith 5.2 and give them a try.
windowscoding.com;
Copyright © 2008 Windows Coding
Microsoft and Microsoft logo's are trademarks of Microsoft Corporation.