FubuMVC Behaviours II

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:

Policies.WrapBehaviorChainsWith<MasterPageBehavior>(); 

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.

Advertisements
Explore posts in the same categories: Fubu, MVC

Tags:

You can comment below, or link to this permanent URL from your own site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: