Several of the pieces of information that we want to collect from the user have a limited number of acceptable choices. For example, the state of the endpoint can only be STARTED, STOPPED, or DISABLED; anything else will lead to a T-SQL error. Rather than prompting the user for freeform input (and running the risk of having them type an unacceptable value), it's much more sensible to offer a list of just the acceptable choices. Fortunately, you can do this by defining an enumerated property.
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 start typing <script which will show you an IntelliPrompt and allow you to autocomplete the script block.
After the script block has been created, lets create the new enumeration type:
<script runat="template"> Public Enum StateEnum STARTED STOPPED DISABLED End Enum </script>
Now you can use a CodeSmith Generator Property directive to define a property that makes use of the new type:
<%@ 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:
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:
<%@ 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 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.