CodeSmith Community
Your Code. Your Way. Faster!

Unique project Guid values without hard-coding

Latest post 10-31-2007 7:33 PM by jamezw. 0 replies.
  • 10-31-2007 7:33 PM

    • jamezw
    • Not Ranked
    • Joined on 10-31-2007
    • Posts 2
    • Points 35

    Unique project Guid values without hard-coding

    The NetTiers.cst uses hard coded Guid values when generating the Visual Studio projects, like so:

    string bllGuid = "20E43088-4618-4F4A-B8AD-FC31B50D94CD";
    string dalGuid = "041C1BBE-0BFB-4D45-8125-9AB0BBC09A92";
    string DALSqlGuid = "8996A7B4-57D3-440B-A545-A701844B8C4A";
    string DALWSGuid = "061C1BBE-0BFB-4D45-8125-9AB0BBC09A92";
    string DALGenericGuid = "6788B7D9-57D3-440B-A545-A701844B8C4A";
    string wsGuid = "5E3CA58E-216A-4F53-BD23-5A48A6C44924";
    string utGuid = "031D5BAE-0BFB-4D45-8125-9AB0BBC09A92";
    string webLibGuid = "47FE3BE8-3E46-47CA-8494-473755867DD0";
    string websiteGuid = "1DCAB031-308A-4581-AFA8-BD29F45A1357";
    string componentGuid = "794327CE-DC0A-4381-89EE-4C00AFB08D5A";
    string winLibGuid = "43FE3BE8-3E46-47CA-8494-473315867DD0";

    What if you want unique project Guid values? Using Guid.NewGuid() is no good because it will generate a different Guid on each re-gen. Instead, you can use a hash of the project namespaces to create a different Guid value for all your projects without having to generate a different Guid each time and without having to hard code the Guid values.

    First, you want to import the System.Security.Cryptography namespace in the NetTiers.cst file like so:

    <%@ Import NameSpace="System.Security.Cryptography" %>

    Second, you want to add a method which will accept a string, hash it, and then generate a unique Guid value from the hash. Place this method just below the GetTemplateBase method in the NetTiers.cst file:

    public Guid GetGuidFromHashedString(String preHashText)
    {
    Encoder encoder = Encoding.UTF8.GetEncoder();

    Byte[] preHashBytes = new Byte[preHashText.Length * 2];
    encoder.GetBytes(preHashText.ToCharArray(), 0, preHashText.Length, preHashBytes, 0, true);

    MD5 md5 = new MD5CryptoServiceProvider();
    Byte[] hashedBytes = md5.ComputeHash(preHashBytes);

    return new Guid(hashedBytes);
    }

    Finally, you want to use this method in place of the hard code Guid values in the NetTiers.cst file, like so:

    string bllGuid = GetGuidFromHashedString(BLLNameSpace).ToString().ToUpper();
    string dalGuid = GetGuidFromHashedString(DALNameSpace).ToString().ToUpper();
    string DALSqlGuid = GetGuidFromHashedString(DALSqlNameSpace).ToString().ToUpper();
    string DALWSGuid = GetGuidFromHashedString(DALWSNameSpace).ToString().ToUpper();
    string DALGenericGuid = GetGuidFromHashedString(DALGenericNameSpace).ToString().ToUpper();
    string wsGuid = GetGuidFromHashedString(WSNameSpace).ToString().ToUpper();
    string utGuid = GetGuidFromHashedString(UTNameSpace).ToString().ToUpper();
    string webLibGuid = GetGuidFromHashedString(WebLibNameSpace).ToString().ToUpper();
    string websiteGuid = GetGuidFromHashedString(WebsiteNameSpace).ToString().ToUpper();
    string componentGuid = GetGuidFromHashedString(ComponentsNameSpace).ToString().ToUpper();
    string winLibGuid = GetGuidFromHashedString(WinLibNameSpace).ToString().ToUpper();
    • Post Points: 30
Page 1 of 1 (1 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems