Home » Blog

rounded header

Customising LightSpeed entity templates

tag icon Tagged as LightSpeed

LightSpeed provides two ways of defining your entity model: writing the entity classes out in code, for maximum flexibility, or using the visual designer, to avoid spending time on boilerplate. Behind the scenes, the visual designer is just generating code according to conventional LightSpeed coding patterns. This is similar to many other visual designers — for example, the LINQ to SQL designer works in exactly the same way.

One thing that’s unusual about the LightSpeed designer, though, is that we provide access to the code generation templates, so if you want to generate code that’s different to the default, you can do so by editing the templates. These are NVelocity templates written in VTL and can be found under the installation directory, under Tools > Designer > Templates.

However, editing the templates in place has a couple of problems. First, whenever you upgrade LightSpeed, it will reinstall its templates over the top of yours. Second, if you’re working with other developers, or across multiple machines, you need to make sure the custom templates get copied to each machine.

To solve these problems, LightSpeed allows you to keep your templates with the project. By doing this, you eliminate the risk of the templates being overwritten during install, and you can treat them as part of the project rather than a machine setting — for example putting them in source control so that when another developer gets the project sources they get the current templates as well.

To do this, copy the template files from the installation location to a suitable project-specific location. Then go into Solution Explorer, select the project node and look at the Properties grid. If the project contains a LightSpeed model (a .lsmodel file), you’ll see an extra entry here called LightSpeed Template File. Edit this to point to your copy of the “main” template (typically Base.vm):

LightSpeed designer custom template path

Next time you edit your model, the code will be regenerated using your template copies. You can now edit these copies to your heart’s content!

Here are a couple of tips and tricks for working with custom templates:

1. Visual Studio won’t automatically regenerate code when you change the template. Changes to the templates will only take effect next time you edit your model. If you don’t actually need to do anything to the model, just move something and move it back again — that will be enough to trigger regeneration.

2. We occasionally ship updates to the default templates, to reflect new features or options, or to fix bugs. If these updates are relevant to you, you’ll want to fold them into your custom templates. It’s therefore a good idea to keep a copy of the “Mindscape version our custom templates are based on” around. That way, when we update the templates, you can use a diff and merge tool to find the changes between the Mindscape versions and merge them into your files (or to merge your diffs from the baseline onto the new baseline). Changes to the templates are usually fairly minor, so this shouldn’t usually be too painful!

Most users won’t need to customise the templates — the default templates generate simple code that will be acceptable in most environments, especially when combined with the per-property custom coding settings — but if you have established conventions or standards that don’t match with ours then it can be a handy escape hatch. If that sounds like you, grab a copy and give it a try!

3 Responses to “Customising LightSpeed entity templates”

  1. [...] 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 [...]

  2. Dude, that is so cool!

    I only had to modify 3 files to output a DTO project: http://www.mindscape.co.nz/forums/Thread.aspx?ThreadID=3536

    Cheers,

  3. [...] of you with legacy databases, automatic generation of WCF data contracts, and the option to use custom code generation templates. Check out the lsgen documentation for more [...]

Leave a Reply

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