Blake Niemyjski

  • Upgrading from CSLA 3.8.4 to CSLA 4.0 using the CodeSmith CSLA templates version 3.0.0.


    We recently released a new major version of the CSLA templates that add support for many new features including Visual Studio 2010, CSLA 4.0 and Silverlight 4.0 support. They can be downloaded here. When upgrading to the templates and you are going from CSLA 3.8 to the latest CSLA 3.8.4 templates, there really aren’t any changes you need to make other than updating your CodeSmith Project File template location to point to the new version of the templates.  The easiest way to do this is to open up Visual Studio and double click on the CodeSmith Project File and do a replace all on that file path as this will update all file paths in all CodeSmith Project Files.

    If you are upgrading from CSLA 3.8 to CSLA 4.0 then additional work is required. The first step is to convert your solution to Visual Studio 2010 and set all the projects to use the .NET Framework v4.0.

    Secondly, you will need to do is go into your CodeSmith Project Settings and change the FrameworkVersion Property from v35 to v40. If you are planning on using Silverlight it is best to set the IncludeSilverlightSupport property to true to save you additional merging steps.

    The next step is to regenerate the files; you will notice that there are some build errors.  This is due to the entity partial classes are not regenerated and there are some required changes for Rules or for Silverlight. If you didn’t make any changes to the non-generated partial classes (E.G. Account.cs or Account.vb), then it is safe to delete the entire class and regenerate.

    Tip: if your Location property is set to a full path, I recommend setting it to “.\” without the quotes.

    If this is not the case then I recommend backing up your solution if it is not under source control (highly recommended). I would copy the CodeSmith Project File to a new folder and use a program like beyond compare to merge the partial classes together.  Since you have already regenerated, the generated partial classes will be in sync with CSLA 4.0 and won’t require a diff so your favorite diffing program will tell you that there is no work needing to be done.

    As you can see from the diff above, you will need to diff the changes. I’d recommend putting all of your Custom Code in region blocks as this makes merging much easier. This shouldn’t ever need to be merged again unless CSLA introduces some breaking changes.


  • CodeSmith supports Visual Studio 2010 RTM!

    We have been working with Microsoft over the past few months to ensure that CodeSmith works with Visual Studio 2010. The currently released version of CodeSmith 5.2.1 already supports Visual Studio 2010 Integration. Although the current support is titled as beta we have ran into very few Visual Studio 2010 bugs and will completely stand behind Visual Studio 2010.


    Microsoft and Microsoft logo's are trademarks of Microsoft Corporation.

  • Unit testing the CodeSmith CSLA Templates

    During the development of our CodeSmith CSLA templates we have spent a lot time unit testing the generated code to ensure everything was working correctly.

    In the future we would like to generate basic CRUD unit tests for any project. We decided to unit test the PetShop database to go along with our PetShop Sample application. We have a solution both in VB and C# that can be found in (Documents\CodeSmith\Samples\v5.2\Projects\Framework-Samples\Csla) when you install CodeSmith or grab the latest version from SVN. We have unit tests for all the Data Access implementations as well as collections. We test primarily against two tables, Category and LineItem.

    These NUnit tests will give you a good understanding of how different operations are performed in CSLA. Feel free to submit to CodeSmith support or forums any unit tests you think we don't have.  If you find a bug in the CSLA templates, its a huge help to the community and us if you submit a failing unit test for the PetShop sample application that can reproduce your bug. This ensures we have more testability and no regressions in future versions. However, any unit test will be appreciated along with the respected sample schema.

    When contributing new unit tests, please try to follow our common testing pattern for consistency that outputs a description and how long it took to run the test.

  • 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
            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.
  • How-to: CodeSmith and SQL Functions.


    CodeSmith now supports SQL Functions and SQL CLR Functions with the release of CodeSmith 5.2. I will quickly show you how to add SQL Function support to your template. 

    The first thing you need to do in order to use SQL Functions is to set the IncludeFunctions Property on any type that derives from SchemaObjectBase.  The following property types are capable of showing functions when you add the IncludeFunctions="True" setting to the property: CommandSchema, CommandSchemaCollection and DatabaseSchema.

    <%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Category="Required" Description="Database that contain the stored procedures." IncludeFunctions="True" %>

    Now your SQL Functions will show up in SchemaExplorer when you iterate over your commands.

    <% foreach (CommandSchema command in SourceDatabase.Commands) { %>

    Name: <%= command.Name %>

    <% } %>

    We have also added five extended properties to the CommandSchema. They are as follows:

    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.

    A normal stored procedure will always return an int value as the return value. However, a scalar function can return any data type.

    The attached template will show you how to get at this information.

  • How-to: Build a custom UITypeEditor

    Recently I built a CodeSmith sample UITypeEditor that allows a user to enter in custom data into a DropDownList. In the following article I’ll show you what you need to do in order to build your own UITypeEditor.


    First you need to create a public class that will hold the data of the drop down list. In this example I named my class DropDownListProperty.

    public class DropDownListProperty

    Next we will need to add the properties and the constructors.

        public class DropDownListProperty
            private List<string> _values = new List<string>();

            public DropDownListProperty()
                SelectedItem = "None";

            public DropDownListProperty(List<String> values)
                if(values.Count > 0)
                    SelectedItem = values[0];
                    SelectedItem = "None";

                Values = values;

            public List<string> Values
                    if (_values == null)
                        _values = new List<String>();

                    return _values;
                    if(value != null)
                        _values = value;

            public string SelectedItem { get; set; }

    You'll notice that we have a public property called SelectedItem. This property will hold the initial value which will be the selected value when a user selects a choice. By default we set this to "None" in the constructor. We also set an attribute on the property Browsable(false). This tells the PropertyGrid not to display this property.

    We now want to override the ToString() method to the DropDownListProperty so the dropdown displays the current selected value.

    /// <summary>
    /// The value that we return here will be shown in the property grid.
    /// </summary>
    /// <returns></returns>
    public override string ToString()
        return SelectedItem;

    Now it is time to implement the class that controls how my class is displayed in the property grid. We will want to create a class that inherits from UITypeEditor.

    /// <summary>
    /// Provides a user interface for selecting a state property.
    /// </summary>
    public class DropDownListPropertyEditor : UITypeEditor

    Next we will add a private member variable named _service. We will need to declare this member variable because we will want to tie into an event in a little bit. Now it is time to override the EditValue method.

    /// <summary>
    /// Displays a list of available values for the specified component than sets the value.
    /// </summary>
    /// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
    /// <param name="provider">A service provider object through which editing services may be obtained.</param>
    /// <param name="value">An instance of the value being edited.</param>
    /// <returns>The new value of the object. If the value of the object hasn't changed, this method should return the same object it was passed.</returns>
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        if (provider != null)
        // This service is in charge of popping our ListBox.
        _service = ((IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)));

            if (_service != null && value is DropDownListProperty)
                var property = (DropDownListProperty) value;

                var list = new ListBox();
                list.Click += ListBox_Click;
                foreach (string item in property.Values)

                // Drop the list control.

                if (list.SelectedItem != null && list.SelectedIndices.Count == 1)
                    property.SelectedItem = list.SelectedItem.ToString();
                    value =  property;

        return value;

    It is important not to be overwhelmed by the code above. The object value that is passed in is the DropDownListProperty class that holds our data. All we need to do is some safe type checking (value is DropDownListProperty) and then cast the value. The _service variable holds the property grid control that we are interacting with.

    We create a ListBox object as that will hold our list of data (Values property from the DropDownListProperty class). It also exposes a Click event that will allow us to know when someone clicks on the drop down list. We will add an event handler ListBox_Click  to the Click event so we can close the drop down list. If we skipped this step then the list would always be shown.

    The next few lines just adds all our data into the Listox and calls DropDownControl(Control).  This shows the populated ListBox control.

    Finally we will set the SelectedItem to the Item that the user selected.

    It is time to add the method that we wired up to the Click event.

    private void ListBox_Click(object sender, EventArgs e)
       if(_service != null)

    The last peice to this puzzle is to override the GetEditStyle method and return that we want to display a DropDown UITypeEditorEditStyle

    /// <summary>
    /// Gets the editing style of the <see cref="EditValue"/> method.
    /// </summary>
    /// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
    /// <returns>Returns the DropDown style, since this editor uses a drop down list.</returns>
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        // We're using a drop down style UITypeEditor.
        return UITypeEditorEditStyle.DropDown;

    Finally we will go back and add a Editor attribute to the DropDownListProperty class. This will tell the PropertyGrid that when this property type is loaded to use the new UITypeEditor class we created.

    [Editor(typeof(DropDownListPropertyEditor), typeof(System.Drawing.Design.UITypeEditor))]

    For more information please check out our Google Code repository. Attached is the source code for these two classes.

< Previous 1 2 3 4 5 Next > ... Last »;
Copyright © 2008 Windows Coding
Microsoft and Microsoft logo's are trademarks of Microsoft Corporation.