CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Raymond Lewallen

Framework Design, Agile Coach, President Oklahoma City Developers Group, Microsoft MVP C#, TDD, Continuous Integration, Patterns and Practices, Domain Driven Design, Speaker, VB.Net, C# and Sql Server

My very first CodeSmith template

I've always been a fairly manual kinda guy, not using a lot of code generators, and using them conservatively when I do use them. With the release of CodeSmith 3.0, which I like quite a bit so far, I think I'm about to take a big plunge into generating a lot of code from now on using code gen tools.

Here is my very first template for CodeSmith 3.0. It is a very, very simple template that creates a class that inherits from CollectionBase and implements IEnumerable, which is something I use quite a bit of. Of course, the advent of generics will render this almost useless in a few months :)

My very first CodeSmith Template

--

Name: EnumerableCollection

Author: Raymond Lewallen

Description:

--%>

Language="VB" TargetLanguage="VB" Description="Generates a strongly-typed collection based on a CollectionBase." %>

Property Name="Author" Type="System.String" Category="Header" Description="Your name." Default="Raymond Lewallen" %>

Property Name="Copyright" Type="System.String" Category="Header" Description="Copyright Information." Default="Copyright 2005 Raymond Lewallen. All rights reserved." %>

Property Name="Project" Type="System.String" Category="Header" Description="The project this type belongs to." %>

Property Name="ItemType" Type="System.String" Category="Context" Description="The type to use as an item in the collection." %>

Property Name="ItemTypeAssembly" Type="System.String" Category="Context" Description="The assembly where the type to use as an item in the collection is located." %>

Property Name="ItemTypeNamespace" Type="System.String" Category="Context" Description="The namespace where the type to use as an item in the collection is located." %>

Property Name="CollectionNamespace" Type="System.String" Category="Context" Description="The namespace where this collection is located." Default="Collections" %>

Property Name="IsSealedType" Type="System.Boolean" Category="Options" Description="Indicates if this collection is to be sealed." Default="true" %>

Property Name="Accessibility" Type="AccessibilityEnum" Category="Options" Description="The accessibility of the class to be generated." %>

'By        :          

'Date     :          

'Description:      Collection for ..

 

Imports System.Collections

Imports .

Namespace

 

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Project:     

    ''' Class:        .Collection

    '''

    ''' Collection of objects.

    '''

    '''

    '''

    ''' --------------------------------------------------------------------------------

            if (IsSealedType) Then %>

    Notinheritable Class Collection

            else %>

    Class Collection

            end if %>

    Inherits CollectionBase

    Implements IEnumerator

 

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Class.Method:         .Collection.Add

    '''

    '''

    '''

    '''

    '''       object.

    '''       Value Type: ." />       (..)

    '''

    '''            (System.Int32)

    '''

    '''

    ''' --------------------------------------------------------------------------------

        Public Function Add(ByVal item As ) As Integer

        Return List.Add(item)

    End Function

 

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Class.Method:         .Collection.Insert

    '''

    '''

    '''

    '''

    '''       [description goes here].

    '''       Value Type: (System.Int32)

    '''

    '''

    '''       object.

    '''       Value Type: ." />       (..)

    '''

    '''

    '''

    ''' --------------------------------------------------------------------------------

        Public Sub Insert(ByVal index As Integer, ByVal item As )

        List.Insert(index, item)

    End Sub

 

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Class.Method:         .Collection.Remove

    '''

    '''

    '''

    '''

    '''       object.

    '''       Value Type: ." />       (..)

    '''

    '''

    '''

    ''' --------------------------------------------------------------------------------

        Public Sub Remove(ByVal item As )

        List.Remove(item)

    End Sub

 

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Class.Method:         .Collection.Contains

    '''

    '''

    '''

    '''

    '''       object.

    '''       Value Type: ." />       (..)

    '''

    '''       (System.Boolean)

    '''

    '''

    ''' --------------------------------------------------------------------------------

        Public Function Contains(ByVal item As ) As Boolean

        Return List.Contains(item)

    End Function

 

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Class.Method:         .Collection.IndexOf

    '''

    '''

    '''

    '''

    '''       object.

    '''       Value Type: ." />       (..)

    '''

    '''            (System.Int32)

    '''

    '''

    ''' --------------------------------------------------------------------------------

        Public Function IndexOf(ByVal item As ) As Integer

        Return List.IndexOf(item)

    End Function

 

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Class.Method:         .Collection.CopyTo

    '''

    '''

    '''

    '''

    '''       Array of type .

    '''       Value Type: ." />       (..)

    '''

    '''

    '''       [description goes here].

    '''       Value Type: (System.Int32)

    '''

    '''

    '''

    ''' --------------------------------------------------------------------------------

        Public Sub CopyTo(ByVal array As (), ByVal index As Integer)

        List.CopyTo(array, index)

    End Sub

 

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Class.Method:         .Collection.Item.Get

    '''

    '''

    '''

    '''

    '''       [description goes here].

    '''       Value Type: (System.Int32)

    '''

    ''' ." />     (..)

    '''

    '''

    ''' --------------------------------------------------------------------------------

        Default Public Property Item(ByVal index As Integer) As

        Get

                Return CType(List(index), )

        End Get

            Set(ByVal Value As )

            List(index) = Value

        End Set

    End Property

 

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Class.Method:         .Collection.OnValidate

    '''

    '''

    '''

    '''

    '''       Object to be validated.

    '''       Value Type:           (System.Object)

    '''

    '''

    ''' Checks to see if given value is of type .

    '''

    ''' --------------------------------------------------------------------------------

    Protected Overrides Sub OnValidate(ByVal value As Object)

        MyBase.OnValidate(value)

            If Not (TypeOf value Is ) Then

            Throw New ArgumentException("Collection only supports objects.")

        End If

    End Sub

 

#Region "IEnumerator overrides"

 

    Private pos As Integer = -1

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Class.Method:         .Collection.MoveNext

    '''

    '''

    '''

    '''       (System.Boolean)

    '''

    '''

    ''' --------------------------------------------------------------------------------

    Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext

        System.Math.Min(System.Threading.Interlocked.Increment(pos), pos - 1)

        If pos Then

            Return True

        Else

            Return False

        End If

    End Function

 

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Class.Method:         .Collection.Reset

    '''

    '''

    '''

    '''

    '''

    ''' --------------------------------------------------------------------------------

    Public Sub Reset() Implements System.Collections.IEnumerator.Reset

        pos = -1

    End Sub

 

    ''' *******************************************************************************

    '''      

    ''' *******************************************************************************

    ''' Class.Method:         .Collection.Current.Get

    '''

    '''

    '''

    '''         (System.Object)

    '''

    '''

    ''' --------------------------------------------------------------------------------

    Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current

        Get

            Return List(pos)

        End Get

    End Property

 

#End Region

 

    End Class

 

End Namespace

 

' My functions here.

Public Enum AccessibilityEnum

    [Public]

    [Protected]

    [Friend]

    [ProtectedFriend]

    [Private]

End Enum

 

Public Function GetAccessModifier(ByVal accessibility As AccessibilityEnum) As String

    Select Case accessibility

        Case AccessibilityEnum.Public

            GetAccessModifier = "Public"

        Case AccessibilityEnum.Protected

            GetAccessModifier = "Protected"

        Case AccessibilityEnum.Friend

            GetAccessModifier = "Friend"

        Case AccessibilityEnum.ProtectedFriend

            GetAccessModifier = "Protected Friend"

        Case AccessibilityEnum.Private

            GetAccessModifier = "Private"

        Case Else

            GetAccessModifier = "Public"

    End Select

End Function

 



Comments

Jason Bunting said:

You should post this on the CodeSmith Template Share forum on the CodeSmith website:

http://forum.codesmithtools.com/default.aspx?f=9
# June 2, 2005 8:09 AM

Raymond Lewallen said:

Excellent. Didn't know there was one. Like I said, have just really started delving into CodeSmith for everyday use.
# June 2, 2005 8:29 AM

Jake Good said:

once you get into the code behind aspect of CodeSmith, you'll never go back
# June 2, 2005 9:22 AM

Jules said:

Ray: The crown jewel of CodeSmith is its ability to inspect a SQL stored procedure and return the input parameters and outputs of the SELECT statement -- as variables that you can work with. You can really save a ton of time with that. For example, I'm working on an application where there are many stored procedures that take input, do a query, and return output. I can point CodeSmith to a specific stored proc and it generates the SQLDataReader code, the collection classes that I use, and the item class that represents one row of returned data. Saved me a ton of typing.
# June 7, 2005 1:27 PM

Raymond Lewallen said:

Jules, I've looked at some of those templates an even generated some code based on them. Sounds like you do the same thing I do - have a structure that represents a row of data, and store the structures in a collection. Those are exactly the type of templates I need.
# June 7, 2005 1:58 PM

Jules said:

Always willing to help out a fellow hard-working coder. I'll send you my standard SQLDataReader template through your contact page.
# June 7, 2005 8:15 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Raymond Lewallen

Working primarily in the public sector during his career, Raymond has designed and built several high profile enterprise level applications for all levels of the government. Raymond now works as a solutions architect for EMC. Raymond is an agile coach, Microsoft MVP C# and also president of the Oklahoma City Developers Group and Oklahoma Agile Developers Group. Raymond spends a lot of his time learning and teaching such things as Test Driven Development, Domain Driven Design, Design Patterns and Extreme Programming practices and principles, to name a few. Raymond is also an advocate of Alt.Net. Raymond is primarily a framework guy, so don't ask him anything about UI :) Check out Devlicio.us!

Our Sponsors

Free Tech Publications