Snowing Code

Personal notes on software development

FubuMVC Behaviours II

(Publish date: 09/05/2010)

In an older post I wrote about Behaviours in FubuMVC and tried to show how fubu can handle different concerns of your website. Now, while the post still is a good example of what we can do with fubu, especially regarding scenarios where mutual concerns of several pages (across the application) can be handled through one sole class (thus encouraging SRP), the solution I suggested in the post to the problem I’ve discussed was far too complex and inefficient compared to what can be achieved by the following way simpler solution.

Let’s work our way from bottom to top. First we’ll have a view model for our Site.master page, a class that will contain all that we want to display on each and every page having this master page:

public class SiteMasterModel
           public IList<Post> History { get; set; }

In our Site.master page we’ll have access to the model by having such a class the from which our master view will inherit:

public class SiteMasterView : FubuMasterPage<SiteMasterModel>

What’s left to do now is to create the Behavior that will take care of populating our blog posts History property of the SiteMasterModel, or for that matter any other master page concern.

public class MasterPageBehavior : BasicBehavior
           private readonly IFubuRequest _request;
           private readonly IPostRepository _repository;
           public MasterPageBehavior(IFubuRequest request, IPostRepository repository)
           : base(PartialBehavior.Executes)
           _request = request;
           _repository = repository;
           protected override DoNext performInvoke()
           _request.Set(new SiteMasterModel { History = _repository.GetHistory() });
           return base.performInvoke();

StructureMap would take care of injecting the IFubuRequest and IPostRepository (or IPostService or whatever you want to have) interfaces implementations you’ve got. How simple and elegant is that? What’s left after that would be to simply register our Behavior:


Not only the behaviour registration procedure is less complex, with this solution the display would be handled from within the master page, rather than from each and every view.

Tags: fubu fubumvc mvc
blog comments powered by Disqus