Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

A prime example is working from data types from an RDBMS which don't match those of the programming language you are writing for. Typically, what happens is the template writer would have to create a long switch case statement for every lookup.
Example:
Old way you would manage a lookup:
public string GetSqlDbType(DataObjectBase column)

{
    switch (column.NativeType)
    {
        case "bigint": return "BigInt";
        case "binary": return "Binary";
        case "bit": return "Bit";
        case "char": return "Char";
        case "datetime": return "DateTime";
        case "decimal": return "Decimal";
        case "float": return "Float";
        case "image": return "Image";
        case "int": return "Int";
        case "money": return "Money";
        case "nchar": return "NChar";
        case "ntext": return "NText";
        case "numeric": return "Decimal";
        case "nvarchar": return "NVarChar";
        case "real": return "Real";
        case "smalldatetime": return "SmallDateTime";
        case "smallint": return "SmallInt";
        case "smallmoney": return "SmallMoney";
        case "sql_variant": return "Variant";
        case "sysname": return "NChar";
        case "text": return "Text";
        case "timestamp": return "Timestamp";
        case "tinyint": return "TinyInt";
        case "uniqueidentifier": return "UniqueIdentifier";
        case "varbinary": return "VarBinary";
        case "varchar": return "VarChar";
        default: return "_UNKNOWN_" + column.NativeType;
    }
}
Developing using a CodeSmith Map

Using a map is quite easy and very flexible. You simply register the map using the Map Directive (see highlighted green image below). Once the map has been registered, you simply reference the map and pass it the value similar to using another Collection class.

Example:

<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Map %>

Directive Attributes
Name: The reference name of the map specified to use in code.(required)
Src: The file-name reference to the map file. Adding the extension name is not required.(required)
Description: A Description field for the Map Register directive.
Reverse: When you require to translate the lookup back to the key from the value, you can reverse the map. You simply Load the Collection using the reverse overloaded method.
Default: The default value to return the key is not found.

API Access:

You can interface with a map from code simply by loading the map by name.

Common API Usage:

This mimics the usage of the mapping in the image above which uses a declarative model.

MapCollection list = MapCollection.Load("System-CSharpAlias.csmap");
list.ReturnKeyWhenNotFound = true;
Response.Write(list[column.SystemType.FullName]);

Reverse Map:
Call the overloaded Load method when requiring to load the map with the key value pairs swapped.

MapCollection list = MapCollection.Load(string mapName, bool reverseMap);
Debug.Assert(list[myValue]  == myKey);

 

 

See the CodeSmith API help for more API Coverage.

 

  • No labels