Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To set up an enumerated property, you need to define a type that only allows the values you want. You can do this by creating an enumeration. Start by moving to the end of the template and choosing Edit > Insert Content > Script Block. Then create the new type:

Code Block
languagevb
<script runat="template">

...


Public Enum

...

 StateEnum
    STARTED
    STOPPED
    DISABLED
End Enum
</script>

Now you can use a CodeSmith Property directive to define a property that makes use of the new type:

Code Block
languagehtml/xml
<%@ Property Name="InitialState" Type="StateEnum" Category="Options" Default="STARTED" Description="The initial state of the Web service." %>

There's one more piece that you need to add to make it all work, though. Under the covers, .NET treats enumerations as integers, but you want to insert literal strings in the generated code. To make the translation, you'll also need to add a helper function inside of the script block:

Code Block
languagevb
Public Function GetState (ByVal State As StateEnum) As

...

 String
    Select Case State
        Case StateEnum.STARTED
            GetState = "STARTED"
        Case StateEnum.STOPPED
            GetState = "STOPPED"
        Case StateEnum.DISABLED
            GetState = "DISABLED"
    End Select
End Function

Having done this, you can get the string corresponding to the user's choice of InitialState property by inserting <%= GetState(InitialState) %> anywhere in the template. After adding enumerations, properties, and helper functions for the authentication and port properties, here's the current state of our template:

Code Block
languagevb
<%@ CodeTemplate Language="VB" TargetLanguage="T-SQL" Description="Create an HTTP Endpoint." %>

...


<%@ Property Name="InitialState" Type="StateEnum" Category="Options" Default="STARTED" Description="The initial state of the Web service." %>

...


<%@ Property Name="Authentication" Type="AuthenticationEnum" Category="Options" Default="INTEGRATED" Description="Authentication method." %>

...


<%@ Property Name="Port" Type="PortsEnum" Category="Options" Default="CLEAR" Description="Port to use." %>

...


CREATE PROC dbo.PersonAddressTypeProc

...


AS
    SELECT 
    AddressTypeID, 
    Name, 
    rowguid, 
    ModifiedDate
    FROM
    Person.AddressType
GO
CREATE ENDPOINT GetAddressType
    STATE = <%= GetState(InitialState) %>
AS HTTP
(
    PATH = '/AddressType',
    AUTHENTICATION = (<%= GetAuthentication(Authentication) %>),
    PORTS = (<%= GetPort(Port) %>),
    SITE = 'localhost'
)
FOR SOAP
(
    WEBMETHOD 'AddressTypeList'
        (NAME='AdventureWorks.dbo.PersonAddressTypeProc'),

...


    BATCHES = DISABLED,
    WSDL = DEFAULT,
    DATABASE = 'AdventureWorks',
    NAMESPACE = 'http://AdventureWorks/AddressType'

...


)

...


GO

...


<script runat="template">

...


Public Enum

...

Public Enum AuthenticationEnum
    BASIC
    DIGEST
    NTLM
    KERBEROS
    INTEGRATED
End Enum 

Public Enum PortsEnum
    CLEAR
    SSL
End Enum 

Public Function GetState (ByVal State As StateEnum) As String
    Select Case State
        Case StateEnum.STARTED
            GetState = "STARTED"
        Case StateEnum.STOPPED
            GetState = "STOPPED"
        Case StateEnum.DISABLED
            GetState = "DISABLED"
    End Select
End Function 

Public Function GetAuthentication (ByVal Authentication As AuthenticationEnum) As String
    Select Case Authentication
        Case AuthenticationEnum.BASIC
            GetAuthentication = "BASIC"
        Case AuthenticationEnum.DIGEST
            GetAuthentication = "DIGEST"
        Case AuthenticationEnum.NTLM
            GetAuthentication = "NTLM"
        Case AuthenticationEnum.KERBEROS
            GetAuthentication = "KERBEROS"
        Case AuthenticationEnum.INTEGRATED
            GetAuthentication = "INTEGRATED"
    End Select
End Function 

...

 StateEnum
    STARTED
    STOPPED
    DISABLED
End Enum 

Public Enum AuthenticationEnum
    BASIC
    DIGEST
    NTLM
    KERBEROS
    INTEGRATED
End Enum 

Public Enum PortsEnum
    CLEAR
    SSL
End Enum 

Public Function GetState (ByVal State As StateEnum) As String
    Select Case State
        Case StateEnum.STARTED
            GetState = "STARTED"
        Case StateEnum.STOPPED
            GetState = "STOPPED"
        Case StateEnum.DISABLED
            GetState = "DISABLED"
    End Select
End Function 

Public Function GetAuthentication (ByVal Authentication As AuthenticationEnum) As String
    Select Case Authentication
        Case AuthenticationEnum.BASIC
            GetAuthentication = "BASIC"
        Case AuthenticationEnum.DIGEST
            GetAuthentication = "DIGEST"
        Case AuthenticationEnum.NTLM
            GetAuthentication = "NTLM"
        Case AuthenticationEnum.KERBEROS
            GetAuthentication = "KERBEROS"
        Case AuthenticationEnum.INTEGRATED
            GetAuthentication = "INTEGRATED"
    End Select
End Function 

Public Function GetPort (ByVal Port as PortsEnum) As String
    Select Case Port
        Case PortsEnum.CLEAR
            GetPort = "CLEAR"
        Case PortsEnum.SSL
            GetPort = "SSL"
    End Select
End Function
</script>

So far, so good. But there's still one thing missing: a connection to the database. We'll tackle that next.

...