I’ve really been struggling with how to improve creating templates for NBusiness 3 so that it is much easier than it is now. I have been delayed by the prospect of actually creating any more templates with the CodeDom. It is just too painful and ugly.

On one hand I really want templates to be classes because this allows me to search for them easily, they are either in the same assembly or in a referenced assembly. It also allows me to easily gather meta-data about templates (attributes) and actually run the templates. It also allows for the template developer to generate code however they way, it's very powerful. There also does not need to be some special path that you store templates in and there does not need to be a relative path specified in the entity. It’s a very light-weight and powerful way to do templates.
Unfortunately the problem with this approach is that your templates are probably going to end up being done with the CodeDom, which as you probably know is a royal pain in the butt. If you try to use a template engine such as NVelocity then you’re stuck taking care of the path issues yourself, which is ok but is also sort of annoying.
Well last night I finally figured out a great solution to this problem. I have decided to include an NBusinessTemplateCodeGenerator Custom Tool to the visual studio integration project. This custom tool will generate for you a template class, based on a template file it is applied to in your solution. Then your entities can reference that template and will use NVelocity to do the generation. I plan on making it extensible somehow so other template tools (such as codesmith?) could be used. Here are some screenshots of a working prototype.
So in this example my simple NVelocity template has the following code:

using System;
 
namespace $entity.Namespace
{
      public partial class $entity.Name
      {
#foreach($field in $entity.Fields)
            public $field.TypeName $field.Name { get; set; }
#end
      }
}

The .nbt (NBusiness Template) file will create a Template class such as this:

[NBusiness.Templates.CodeTemplateAttribute()]
public class TestTemplate : NBusiness.Templates.TemplateBase {
    protected override NBusiness.CodeDom.Compiler.EntityFile Compile(NBusiness.CodeDom.Entity entity, NBusiness.CodeDom.Compiler.EntityCompileParameters parameters) {
        NBusiness.Templates.ICodeGeneratorEngine engine = NBusiness.Templates.CodeGeneratorEngine.GetEngine();
        string template = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(@"IyoNCiAgICBTaW1wbGUgRW50aXR5SW5mbyBDb...you get the idea"));
        byte[] generated = engine.Generate(entity, template, parameters);
        return new NBusiness.CodeDom.Compiler.EntityFile(entity.Name, generated);
    }
}

 …Which is ugly, which is why this tool was created. It will convert your template code into a base64 string which will be converted back into code before getting passed into the NVelocity engine and merged with the incoming entity. This class will now be accessible by entities in the same project or referencing projects.
Your entity file might look like this:

using ClassLibrary1;
 
family Test
{
      entity A as TestTemplate
      {
            field auto id int aid;
            field nullable string data;
            field nullable double value;
      }
}

Which when run through the TestTemplate will produce code that looks like this:

using System;
 
namespace Test
{
      public partial class A
      {
            public int aid { get; set; }
            public string data { get; set; }
            public System.Nullable<double> value { get; set; }
      }
}

So all you have to do to get NVelocity working with NBusiness entities and models will be to add a .nbt file to your project which (if done through the Add New Item dialog) will have the Custom Tool NBusinessTemplateCodeGenerator applied to it. Then author away!
I will be using this system to author the core templates that come with NBusiness from now on.

The twin cities developers guild has recently formed as a way to help bring together and organize other special interest groups.

Magenic is currently sponsoring an XNA User Group as well as a Languages User Group and now the Developers Guild is being formed. I'm happy to say that these groups are open for everyone and will encompass any relevant topic. Check out the respecitve websites and come to the meetings when you can!

A new user group has formed in the Twin Cities with a special focus on programming languages.

http://www.twincitieslanguagesusergroup.com

The first session will be held by Charles Nutter, who works for Sun Microsystems developing JRuby. He works right here in the Twin Cities and I've met him personally at the Lang.NET symposium this past January and found him to be very knowledgable and exciting to talk to.

Getting a chance to talk with someone who is actively working on a professional programming language should be quite interesting. If you have any interest at all in programming languages you should definitely go to the link above and find out the logistics.

1 2 3 4 5               

About Me

Me

My name is Justin Chase and I am a software professional in Minneapolis, Minnesota. I work for a software consulting company called Magenic Technologies and I am the lead developer on an open source project called NBusiness.
More ...

Topics