ProMesh.NET Quick Walkthrough - Part 8 - Setup/Teardown methods


The framework can call methods on your controller object before and after your "main" action method automatically. This can be useful to specify an alternate view for some users, to perform authentication, or whatever crosses your mind.

You can do that by adding the [Setup] or [Teardown] attributes to methods of your controller class. The [Setup] attribute tells ProMesh.NET to execute the method before the action method. Obviously, the [Teardown] attribute will cause the method to be called after the action method.

The best way to use this feature is by creating base classes for your controller pages. For example, if you have a set of pages that are only available for users that are already authenticated (logged in), you can create two base classes with setup methods defined:

public class PublicPageController : PageController
{
   [Setup]
   private void SetLayout()
   {
       ChangeLayout("PublicLayout");
   }
}

public class AuthenticatedPageController : PageController
{
   [Setup]
   private void Authenticate()
   {
       if (!(bool)Session["Authenticated"])
          Redirect("~/login.ashx");
   }
}


You can then use these classes as the base class for your controllers:

// Note: we didn't specify a view because by default the view is the same name as the class
public class AboutUs : PublicPageController
{
   // ...
}

public class MyAccount : AuthenticatedPageController
{
   // ...
}


Next >> Unit Testing

Last edited Jul 30, 2007 at 4:33 PM by activa, version 2

Comments

No comments yet.