Table of contents
Referencing your assemblies and automatically importing namespaces
Referencing Assemblies
Consider the following this code:
<% for user in users: output "<p>${user.Name} - ${user.Email}</p>" end %>
Looks simple, right? The problem is that Brail doesn't really know what user is, so it uses reflection to get the values of the Name and Email properties. This is, of course, quite expensive in performance terms. What is the solution? You need to tell Brail to add a reference to the assembly where User is defined. You can do that by adding this line to your web.config file (see the full configuration section below for more details).
<Brail> <reference assembly="assembly.with.user.object" /> </Brail>
And then, in your view code, you write:
<% import My.Models %> <!-- lots of html code -> <% for user as User in users: output "<p>${user.Name} - ${user.Email}</p>" end %>
You can also use this out side of loops, in order to get strong typing (and the assoicated performance benefits):
<% import My.Models %> <!-- lots of html code -> <% # define a parameter called user of type User user as User = GetParameter("user") # now it uses strong typing, instead of reflection output "<p>${user.Name} - ${user.Email}</p>" %>
With this simple change, you've completely eliminated the use of reflection and probably increased by a fair margin your application performance. However, because it significantly increases the complexity of developing the views, it is not really the recommended approach. If you run into a situation where the cost of reflection in the views is a significant one, there are other options, which involve improving dynamic dispatch inside of Brail, bringing you the benefits without the cost. This is not implemented currently, because so far we have not run into a situation where this was a problem that warranted the additional complexity.
Beyond making the view code more complex, this can affect the ability to use some of the nicer abilities on Brail, such as ignoring null references using the IgnorNull proxy.
Auto Imports
As you can imagine, it can get tiresome to specify the default imports all over the place, Brail supports automatic imports from the configuration. All you need to do is specify the following in the web.config file:
<Brail> <import namespace="My.Models"/> </Brail>
And it will be added for you by Brail.