in

CodeSmith Community

Your Code. Your Way. Faster!

External

October 2007 - Posts

  • Datasets Vs. Custom Objects

    A few days ago, we got a question in the discussions for the NuSoft Framework asking, " Why not just use a strongly-typed DataSet? " Honestly, I was a little taken aback by the question. I haven't used datasets or typed datasets in probably 4 or 5...
  • CSTiers is born

    A new CodeSmith Project enters the world and its called CSTiers, named after the excellent .netTiers project. While .netTiers creates a complete website, CSTiers will create a DBVT Community Server Visual Studio customization project framework.

    Almost every Community Server customization project I do involves five areas: DBVTConfiguration, DBVTContext, DBVTUrls, a Data Provider and original Chameleon Controls.  The objective of CSTiers is to create the structure for each of these areas when I start a new CS project.

    I created CodeSmith templates to generate Data Providers for both SQL Server and Access some time ago, and because I'll be creating a number of Chameleon Controls over the next weeks I added a CodeSmith template to generate the control classes: Data, List, PropertyValueComparison, PropertyComparison, ControlUtility, Query and ThreadQuery.  All seven classes may not be used for every Chameleon Control, but why not create them anyway?  I included the option for generating a ControlUtility class, and since this is an early version of the template I'm sure I will add more generation options going forward.

    Below is the execution template for CSTiers.Controls using my recent DBVT ITunes mod as an example.  A master CodeSmith template would be used for the project, equivalent to the netTiers.cst template and would replace the individual templates.

     

     

    The output of CSTiers.Controls is shown below.  And no, the classes aren't 100% complete, as there is still a bit more coding to do, but 80% complete is better than 0%.


     

    The CodeSmith project output window so far looks like the following.  When complete it will contain a single template generating not only Controls and the Data Provider as shown, but the Configuration, CustomUrls and Context folders and files as well.

     

  • NetTiers: Expose custom stored procedures as componentlayer methods

    Sometimes is needed a custom functionality that goes far that the CRUD functionality generated by NetTiers. To acomplish this task we can use a custom stored procedure to get the data and expose it as a method of the componentlayer (services layer).
    Follow the next steps to expose a custom stored procedure:

    1) Configure Codesmith properties
    CustomProceduresStartsWith (example: {1}{0}_)
    ProcedurePrefix (example: cust_sp_ )

    2) Add a Stored Procedure
    Add a SP with a name that follows the convention defined in CustomProceduresStartsWith where {1} is the placeholder for
    ProcedurePrefix and {0} is the placeholder for the TableName, for example cust_sp_Contact_GetBySalesOrderCountBetween (using AdventureWorks db)


    CREATE PROCEDURE [dbo].[cust_sp_Contact_GetBySalesOrderCountBetween]

    @min int,
    @max int
    AS
    BEGIN
    SET NOCOUNT ON;

    SELECT * FROM Person.Contact C
    WHERE
    (SELECT COUNT(*) FROM Sales.SalesOrderHeader SOH
    WHERE SOH.ContactId = C.ContactId) BETWEEN @min and @max
    END

    3) Run the template

    4) Use the method
    Now in the componentlayer for the entity we have our custom stored procedure exposed as a method

    ContactService contactService = new ContactService();
    gvContacts.DataSource = contactService.GetBySalesOrderCountBetween(25, 30);
    gvContacts.DataBind();


    Note: the return type of the custom stored procedure method can be a TList or the defined in the CustomNonMatchingReturnType property (DataSet/IDataReader).
    To determine what type will be return a method compares every column order and type returned from the Sp with every column order and type of the Entity table.
    Debugging a bit the template, I have reviewed the method that compares the stored procedure column type with the table column type. This method returns false if some column type returned has no equivalent (File: \TemplateLib\CommonSqlCode.cs Method: IsMatching)

    if (!SqlTypesAreEquivalent(command.CommandResults[0].ColumnsIdea.NativeType, table.ColumnsIdea.NativeType))
    return false;


    If you have UDT (User-Defined Data Types) fields in your table this method will be return false and CustomNonMatchingReturnType will be setted as Method Return type.

    Usually is preferible that have an TList as method return than a Dataset or a IDataReader, because in this way you have all the NetTiers functionality to manipulate the data (deepload the TList returned for example). To force return a TList when have
    UDT fields you can edit the file \TemplateLib\CommonSqlCode.cs and change in the method IsMatching the above line for this:


    if (!SqlTypesAreEquivalent(command.CommandResults[0].ColumnsIdea.DataType.ToString(), table.ColumnsIdea.DataType.ToString()))

  • CodeSmith for Blog Posts

    I wanted to add a few Stylin posts to my new Stylin Mirrored Blog tonight, but didn't want to deal with entering the posts from scratch.  They each have the same format after all: some text to describe the page, a thumbnail that links to a full-size screenpic, the date I originally captured the screen image, and a link to the site.  A simple pattern.  I put the blog online last night after having the idea to do it yesterday afternoon, and tonight I was thinking about creating a Community Server Module to render the post details for me.  The module's snippet would have looked something like:

    [stylin: image1022.jpg,9/15/2007,http://thesiteurl.com]

    The opening original comments would complete the post.

    I didn't really like that idea, so since we're talking about a template, why not use CodeSmith?  About five minutes to crank out the template and I have something I like better than a CSModule.


  • CodeSmith for Creating SQL Tables

    I was creating a SQL Server 2005 table today in SQL Server Management Studio and went to the bottom pane to create my Identity field like I always do. I scrolled down, scrolled some more, and selected "yes" from the dropdown to confirm it was indeed...
  • CodeSmith for Code Changes

    One of the greatest benefits of using a code generation tool like CodeSmith comes not from generating methods and objects the first time, but when you need to change that code.  Using CodeSmith to regenerate template-based blocks of code is potentially so much more efficient than editing code by hand.  Below is an example of responding to field name changes of a SQL table with CodeSmith and the time savings that results.

    In the example I am migrating data from Access tables to SQL Server using a Community Server background task.  I needed to add a "VendorNo" field to the Company Table and since I was adding a field I decided to change several field names of the company tables as well.  CompanyAddress became Address, CompanyPhone became Phone, and so on.  Normally that would require some ugly code editing, but making the changes shown below and elsewhere in the app required literally only minutes with CodeSmith.


     

    Above is the method that imports company data into SQL Server.  The Business Object CompanyImport that was generated from the updated table no longer contains CompanyAddress, CompanyPhone, etc.  Time to execute my "Add SQL Update-Insert Parameters.cst" CodeSmith Template.


     


    The resulting cmd.Parameters.Add() statements were generated from the above template in seconds, which compiled clean and contained all of the field name changes as well as the new "VendorNo" field.

     

  • .netTiers Code Generation

    A tip for code generation. When reading through various patterns based development books, working on my current gig, and just trying to make code more readable without adding too much headache, I've stumbled on a few tips. Name those entities! Instead of "Address" or "User" you would then have AddressEntity or UserEntity. This is very helpful when you are using domain objects at a higher layer than your entity objects. It is also helpful in the case of a WCF Facade layer...(read more)[IMG]
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems