Thursday, June 23, 2011

The GAX Property Directive

One of the things that really bother me about T4 templates is the lack of direct parameter support (at least in the Visual Studio 2008/.NET 3 timeframe).

In an earlier post, I passed parameter by writing the parameter data to a file outside of the template and reading the data from within. I used a common assembly linked to both sides of the divide. This works for me, but it is really hacky.

GAX Template Host

During my preparation for my Portland Code Camp presentation, I came across Guidance Automation Toolkit for Visual Studio 2008. It has its own TemplateHost that includes a Dictionary of parameters.

Parameters are exposed to the Template through a custom directive. On the template, the parameter is declared using a directive like this:

<#@ property processor="PropertyProcessor" name="MyName" type="String" #>
The directive contains the following parts:
processor Should be "PropertyProcessor" (there may be a way to write your own processor, I haven’t gone down that rat hole yet).
name The Variable name that can appear in your code. It works just like a variable, however, tangable T4 Editor doesn’t provide Intenesence (I wouldn’t have expected it).
type Any .NET Type that the template knows about. You should use the .NET type name (as opposed to C# or VB.NET type).
In C# the parameters are created like this:
var arguments = new Dictionary();
arguments.Add("MyProperty", new PropertyData("String", typeof(string)));
To pass parameters from your code you need to create a Dictionary<string, PropertyData>, add your parameters and pass them as the second argument into the TemplateHost constructor (the first can be any string as far as I can tell).

Simple Sample

The Template

<#@ template language="C#" debug="True" #>
<#@ output extension="txt" #>
<#@ assembly name="System.dll" #>
<#@ import namespace="System" #>
<#@ property processor="PropertyProcessor" name="MyProperty" type="Nullable<Int32>"#>
<#@ property processor="PropertyProcessor" name="MyName" type="String"#>
My Property Test
<# if (MyProperty.HasValue) {#>
Property Value: <#= MyProperty.Value #>!
<#}#>
Name: <#= MyName #>

C# code

static void Main(string[] args)
{
    // Prepare template parameters
    var arguments = new Dictionary<string, PropertyData>();
    arguments.Add("MyProperty", new PropertyData(42, typeof(int?)));
    arguments.Add("MyName", new PropertyData("Jack Stephens", typeof(string)));

    // Initialize GAX template host
    // The Template Host is from GAX, not the default host
    var host = new TemplateHost("Random String", arguments);
    host.TemplateFile = Path.Combine(Directory.GetCurrentDirectory(), 
        "PropertyTest.tt");

    // Transform template
    string template = File.ReadAllText(host.TemplateFile);

    ITextTemplatingEngine engine = new Engine();
    string output = engine.ProcessTemplate(template, host);

    Console.Write(output);
    Console.ReadKey();
}

References

Monday, June 13, 2011

Portland Code Camp 2011 Presentation Notes

Again, I'm late getting this up. I will give usual excuses of being busy. In the future I will not promise anything before the next weekend after the code camp.

Since I developed this talk on this blog, this entry will essentially be a link list. I put a copy of the slides in Google doc and you can reference it here

This presentation is similar, but not the same as a presentation I gave at the Boise Code Camp in February. In the interest of being DRY (Don’t Repeat Yourself) I will reference the notes for that presentation.

Tools

For this presentation, I used the Tangible T4 Editory and the T4 Toolbox.

Template Parts

I presented a slide that showed the basic template parts in a small mock template:

<#@ template language="C#" #>
<#@ directive property=“value” #>
<# var item = "Statement Block"; #>
Text Block 
<#= "Expression Block" #>
<#+ 
    string ClassFeatureBlock(string thingy)
    {
        return thingy;
    } 
#>

Hello World Monty

I showed some of the elementary features of T4 templates by refactoring a basic template that would create a text file that contained the infamous words “Hello World”. A script of that exercise is here

Basic Code Generation Demonstration

To demonstrate Code Generation I set out to generate a simple T-SQL Select Statement using Sql Management Objects (SMO).

Here I will refer to the Boise Code Camp Notes.

Generating Multiple Files using T4 Toolbox

I used the result of the Basic Code Generation Demonstration to attempt to generate select scripts for all the tables in a given database. This demonstration was based on a series of blog posts by Oleg Synch (Part 1, Part 2, Part 3 & Part 4).

I do intend to give my take on this process. Perhaps that is why I'm over a week late on these notes. I will add a link to these notes later.

GAX Property Directive

I discussed using Guidance Automation Extensions's property directive to overcome T4's lack of native support for passing parameters into templates. This part was based on another Oleg Synch article Understanding T4: <#@ property #> directive.

Again, I will add links to future blogs post(s) on this subject here.

Generating a Nullable ADO.NET DataSet Wrapper

This is when I went to Show and Tell Mode. I demonstrate a Nullable DataSet Wrapper that I wrote in these blog entries. I wrote the templates in VB.NET to generate code in C#.

Part 1, Part 2, Part 3 & Part 4

Last Word

This presentation reached too far and, like Icarus, I failed to hit the mark. The number of forward links is a sign of that. I intend to back-blog in the areas the missing material and add links when I am done.

Tuesday, May 31, 2011

Writing Kindle Presentation Notes with MobiPocket Creator

Next weekend I will be presenting on Code Generation with T4 Templates at the Portland Code Camp and I bought a Kindle a week or so ago. So, how do I play with my new toy without getting behind on preparing for my presentation?

Mobipocket Creator Publisher Edition

I am creating small ebooks with Word and Mobipocket Creator Publisher Edition. I work though a demo or other presentation block and write it in Word. Then I use the Mobipocket Creator to create an ebook. I put it on my Kindle and review it before I go to bed.

Is this really a net gain or just a way to justify playing with my new toy instead of doing real work? I will find out come Saturday in Portland

Tuesday, May 17, 2011

Hanselminutes #266 and Alt.Net Seattle’s "Open Source in Business"

I just finished listening to Hanselminutes Open Source vs. Making Money vs. Freaking Lasers - Are we all Evil? With Chris Sells and it reminded me of the Open Source in Business session that I attended at Alt.Net Seattle. This post is attempting to tie these two things together.

Open Source vs. Commercial Software

Should we share our non-core software? Scott and Chris suggest that open sourcing your software is a cost. If you open source a project to increase good will among the programmer community; it is a good thing but not necessarily profitable.

In Alt.Net Seattle’s Open Source in Business, a couple of participants claim that their companies benefited from "free" programming and used the contributor’s list to fill positions.

Scott and Chris talk about the Portland open source culture and how making money isn’t necessarily the biggest thing in Portland. Some of the open source that way to keep more of what they make.

Companies Being Evil

Apple and Google have done evil things in the last few years, but Microsoft is still evil. Scott and Chris suggested that Microsoft isn’t well organized enough to be evil. No company of any size is beloved by everyone.

Is evil associated with making money over customers? In any publicly traded company, stock holder value is king over everything else. Microsoft may feel more evil because they charge us directly. People may like them better if pricing was less complicated (the pricing discussion goes on from there, but I won’t go there in this post).

Since Google and Facebook make money from advertising and don't charge us anything, they feel less evil (Facebook privacy policy makes them feel more evil to me).

Originality of .NET Open Source

At Alt.NET we also discussed the relative lack of originality in the .NET ecosystem. Many of the major open source projects (NHibernate, NUnit, etc.) are copies of Java open source projects. Perhaps many .NET developers are shy about starting ambitious open source project fearing that Microsoft will somehow co-opt it. Perhaps Microsoft is too disorganized to get out of the way of some cool open source project.

This may be the way that Microsoft is evil. It also could be that the companies that use Microsoft software are more conservative and value safety over innovation. I've worked for a few companies that view open source as evil -- to these companies, Microsoft is a crusader fighting against the chaos.

Sunday, May 08, 2011

Thoughts durring Alt .NET Seattle: Explaining my lame laptop.

Confession: I cam to Alt .Net Seattle with a HP Pavilion Entertainment PC Laptop with a HUGE screen. The thing is a monster. And it is totally uncool. I am not going to replace it for another year: I am not made of money.

I wonder if there are things I can to to acknowledge my folly in a light self deprecating way. Perhaps a sticker that says something like "I'm a slack jawed yokel from Spokane" or somehow making light of the fact that it isn't an Apple (it's 40% Apple here), but that seams too heavy to me. A sticker would be nice but what should is say? etc.

Monday, May 02, 2011

Thoughts before Alt .NET Seattle: Part II: Using jrcs3.com

I am going to Alt.Net 2011 Seattle Conference next weekend. Since I am not on the bleeding edge of technology at work, I’m looking into modern methods and technologies.

Using my domain

Several years ago, I bought the domain jrcs3.com; it the initials for my legal name John Robert Chilton Stephens III.

Since I will be hanging out with the cool kid and in technology, all the cool kids have vanity domain, I need to make sure that my domain is visible on my cards, etc. So this weekend I:

  • Moved this blog from jrcs3.blogspot.com to brochure.jrcs3.com (it is “brochure” because someone said that it isn’t a blog until it gets comments).
  • Created a jrcs3.com event email address for the Alt.Net Seattle 2011. You will have to attend the event to see what it is.

When I get my next paycheck (Monday or Tuesday) I will:
  • Buy a year of hosting for jrcs3.com
  • Write a simple home page for it in Razor (linking to brochure.jrcs3.com). I have already created a WebMatrix project.
  • Print out a couple of sheets of business cards to hand out.

Friday, April 29, 2011

Thoughts before Alt .NET Seattle: Part I: TDD

I am going to Alt.Net 2011 Seattle Conference next weekend. Since I am not on the bleeding edge of technology at work, I’m looking into modern methods and technologies.

TDD

On my last couple of gigs, when I bring up Test Driven Development, I get a lecture about how it is overly academic and would never work "here" (one place even banned NUnit altogether). Yes, it may take more time to code, but at these places, we spend a lot of time fixing things after the fact.

I agree that it can be taken too far. There are times that it doesn’t make sense to write test before writing trivial code. UI is infamously un-testable. Be pragmatic, it is just as extreme to reject automated testing altogether

I like the way TDD appears to make you think. You know, this function needs to do these 5 things, and what about this edge case, etc. And I have some tests that I can use later, for less effort than if wrote the test later.

I have snuck some tests into the projects that I have more control over. Most of the tests are too large (cover more one thing) and break other TDD rules. Since I can’t get approval to use an IOC container, they are not unit test. Even with these lower quality tests, I have found bugs and have been able to safely re-factor.