Creating entity Templates

Topics: Developer Forum
Oct 10, 2007 at 8:47 PM
To create a new template you need to do a few things:

  1. Create a class that implements NBusiness.Templates.ITemplate, or inherit from a class that implements ITemplate (NBusiness.Templates.TemplateBase for example)
  2. Add a reference to the assembly that contains this class. For now you cannot put entity templates into the entity project itself (hopefully in the future this will be available).
  3. Implement the generate method by returning one or more resource files. These files have a byte[] that represents the data for the file, for code files such as C#/VB you can take a string object and convert it into a byte[].
  4. On your entity definition file (es) add a using that matches the namespace of your entity template. For example if the class is Example.Templates.MyExampleTemplate then add "using Example.Templates;" to your entity definition. This will cause your entity to show up with intellisense (hit ctrl + j to force intellisense).
  5. Add your template to the entity for example: "entity ExampleEntity as EntityBase, MyExample" Note: that the postfix 'Template' is missing on the name of the entity template, it is entirely optional

If you attach an instance of Visual Studio to the instance with the entity project when you build your project it should allow you to step into your custom template during build time. A few things you should know:

  • Your template will only be called once during build time, all entities defined by this template will be passed in as an argument. This allows you to create multiple files for each entity or 1 file for multiple entities.
  • The template specifies the names of the classes and files it will be generating. Try to stick with fairly unique naming standards.
  • Generated files (for now) are placed into the $(ProjectDirectory)\obj\generated\ folder. So if you name your file "MyExample\\" + entity.Name + ".cs" then the file $(ProjectDirectory)\obj\generated\MyExample\ExampleEntity.cs" will be created.

There is a work item related to creating project files for generated files, which should be available for version 3.0 but for now you are able to simply manually navigate to this folder to access the generated files.