Home » Blog

rounded header

Archive for April, 2010

Using T4 templates with LightSpeed

tag icon Tagged as LightSpeed

One powerful feature of the LightSpeed designer is the ability to use your own custom templates for code generation. However, some people don’t feel comfortable forking the templates. In addition, some people want to organise their files differently (the LightSpeed designer always generates everything into a single file), or want to use Microsoft’s T4 templating language instead of the NVelocity language.

So we’re pleased to announce that LightSpeed now supports T4 templates. You can create your own .tt files, hook them up to a LightSpeed model, and generate your own code files from that model. Here’s how.

First, you’ll need a bit of boilerplate. The following directives declare your template and pull in a couple of required LightSpeed designer DLLs:

<#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" #>
<#@ assembly name="Mindscape.LightSpeed.Generator.Model.dll" #>
<#@ assembly name="Mindscape.LightSpeed.Generator.Integration.Dsl.Mapping.dll" #>

You’ll need to help T4 find those DLLs. To do this, you can either specify a path in the directive, or you can add the folder to the project’s Reference Paths (Project > Properties > Reference Paths). [UPDATE: Visual Studio 2010 appears to ignore the project Reference Paths; specify the path in the directive instead.] Please note these are not redistributable DLLs and you should not add them to your project references! T4 needs them when it processes the template but you won’t need them at run time.

Now you need to tell T4 your inputs and outputs.

Our output directive tells T4 what extension to use for the generated file (its base name is the same as the .tt file). For this example, I’m going to just output a text file listing the entities in my model. In reality, of course, you’d be emitting C# or Visual Basic code, and you’d use an appropriate extension.

Our input is specified by a LightSpeedModel directive with two attributes: processor and requires. The processor directive is an incantation which hooks T4 up to the LightSpeed designer. The requires directive specifies the file name of the .lsmodel file you want the template to operate on.

So we need to add the following directives:

<#@ output extension=".txt" #>
<#@ LightSpeedModel processor="LightSpeedModelDirectiveProcessor" requires="fileName='Sample.lsmodel'" #>

where Sample.lsmodel is the name of our designer file — you’ll need to change this to the name of your model file.

That’s all the plumbing out of the way: now we can get on with writing the template itself. For our simple “list the entities” example, the skeleton is pretty obvious:

Entities in model:
<#
  foreach (/* what type? */ entity in /* what collection? */)
  {
#>
* <#= entity./* what property? */ #>
<#    
  }
#>

Now how do we fill in those gaps? Here, unfortunately, we have to improvise a bit. We don’t currently document the LightSpeed designer object model, so you’ll need to use a bit of ingenuity — or a tool such as the Visual Studio Object Browser. Here are some tips to get you started:

  • You can find the name of the type by looking in the drop-down box at the top of the Properties window. For example, if you select an entity, the drop-down box will say “Entity” next to the object name. That’s the type you’re looking for!
  • You can typically find the name of a property by removing the spaces from the display name in the Properties window. For example, the identity type of an entity is called “Identity Type” in the window, so the programmatic property name is “IdentityType”. This isn’t always the case, so you may need to whip out the Object Browser if you get errors.
  • Collections are typically (though not always) named as the plural of the kind of thing in the collection. For example, entity properties are represented by the EntityProperty type. So Entity has an EntityProperties collection. Again, Object Browser will help you out when this guideline fails.

Finally, how do we get started — how do we refer to the model itself? It turns out that T4 creates a member called Model which refers to the model loaded from the file specified in the LightSpeedModel directive. So we can just use that. Plugging all this into our skeleton, we get some proper T4 code:

Entities in model:
<#
  foreach (Entity entity in this.Model.Entities)
  {
#>
* <#= entity.Name #>
<#    
  }
#>

Save this updated .tt file, or run the transformation explicitly, and out pops the text file with its list of entities!

Want to create your own code off your LightSpeed models? Download the latest nightly build (retail version) and take it for a spin!

Silverlight Elements: Scheduler Control

One of the larger controls that you will find in Silverlight Elements is the scheduler. This control is based on the calendar seen in Microsoft Outlook and allows users to manage all of their appointments. It could be used by students to record their class timetable details, it could be used in the office to remind you of up comming meetings, or it could be used by anyone who just wants a personal scheduler. Today we take a tour of the main features that make the scheduler control easy to use.

View Navigation

The scheduler allows the user to view appointments that occur across a particular day, week, or month. The user can navigate between each of these views in a number of different ways. The most basic way is by using the labeled buttons at the top left of the control. These will navigate to a day, week or month that is close to what the user is currently viewing. Just below the labeled buttons is a pair of left / right navigational buttons. These allow the user to stay within the same view, but switch to the next / previous day, week or month.

Within month view you will also find buttons to the left of each week, and above each day which allow easy navigation to the appropriate views. Finally, if the user finds themself looking at a view that has no appointments to display, buttons appear on the control to navigate instantly to the next or previous appointment no matter when it occurs.

Creating appointments

There are a few different ways that a user can add an appointment to the schedule:

1. One way is to first make a selection across some number of days or time slots, and then press the “Add Appointment” button at the top right of the control.

2. Hovering the mouse over a time slot for a couple of seconds will result in a button appearing that can be used to add an appointment. After clicking the button, the user can give a name to the appointment, and then press enter or click some where else with the mouse to complete the operation.

3. Double clicking on a time slot can also be used to create appointments.

During all these different ways of creating an appointment, a dialog is displayed that allows the user to set its name, start/end time, or set up a recurrence pattern. Once an appointment has been added, the user can then change its start and end times by dragging the thumbs found at each of its ends.

Customization

Silverlight Elements comes with 5 standard styles for the scheduler control. Creating your own style allows you to customize the scheduler to meet the unique requirements of your application. You could remove features such as the “Click to add item” button, or add features such as new ways to navigate between views. We have also provided ways to control the scheduler programatically, and listen for when the user interacts with the scheduler making it easy to integrate it into your application. For example, adding an appointment can be done by going through the controls Schedule property and calling the AddItem method as follows.

Scheduler schedulerControl = new Scheduler();
schedulerControl.Schedule.AddItem(new ScheduleItem
{
  StartTime = new DateTime(2010, 4, 12, 9, 0, 0),
  EndTime = new DateTime(2010, 4, 12, 9, 30, 0),
  Name = "Kick start meeting"
});

You can also remove a particular schedule item, get a list of all the items, check the currently selected item and more. If you want to know how to make your own Scheduler style, or more information about how to use the Scheduler then you can contact us through our forums or put a comment on this post.

Download a free trial version of Silverlight Elements or try out the online demo (if you have Silverlight 3.0 installed).

Want more features in the scheduler or any of the other controls? We’d be happy to hear from you in our forums, or simply put a comment on this post.

Data Products Visual Controls Community Store
LightSpeed ORM
NHibernate Designer
SimpleDB Tools
SharePoint Tools
WPF Elements
WPF Diagrams
Silverlight Elements
Forums
Blog
Register
Login
Subscribe to newsletter
Buy Now
My Account
Volume Discounts
Purchase Orders
Contact Us