Development Framework for Sitecore MVC

Source code for this framework is available on GitHub

It’s been 3 year I have been working on Sitecore Mvc (6.6 & 7.2) and had seen some of the bad and best enterprise framework laid out for Sitecore development. With Sitecore it is really important to structure the solution correctly to facilitate development team to design a scalable solution and code without worrying about another part of the application. It is also important to develop individual components without any dependency on the functional or non functional part of the code.

This tempted me to write an abstract framework or layout the basic building block of the application which are required for any Sitecore Mvc development. I have found other frameworks available e.g Sitecore Mvc Contrib or Sample Sitecore Mvc but I haven’t used them so far.

It is not possible to cover all the feature in this post and it may require a series of post to cover it. Following are the feature included so far. I will turn them in links once I have the relevant post out.

  1. Pluggable Mvc Areas Implementation
  2. Form Post in Siteocre MVC
  3. Exception Handling
  4. Dependency Injection – Container agnostic
  5. Asynchronous Logging – Framework agnostic
  6. Integration with Claims Identity
  7. Glass Mapper Implementation
  8. Unit Testing

Architecture
This Project is designed using Sitecore 7.2 and Mvc 5.1 and here is how it looks like from solution alignment point of view.

Sitecore Solution Architecture

The key consideration here is to un-touch the Sitecore Host project and code outside of it using pluggable module, on build just deploy the required files in host project’s respective directories and run the application. This open up the idea of deploying individual component as separate module in Sitecore via xcopy, nuget package or any other deployment mechanism. It helps in environment where multiple teams are working independently.

Exception handling is designed to handle the application error via HttpModule and Mvc specific one with Sitecore’s Exception Processor. The important part of it is that it avoids the redirect and render the error page content along with correct error status code. Framework keeps the error handling simple but it doesn’t stop if there is a need for richer experience. I wrote about it long back here , this can be still applied to present more customized solution.

Dependency Injection is achieved via a Framework IoC class but it internally rely on a container which could be any container available, either Unity, Windsor or any other. This allows less dependency on the DI container and build application specific code. The current application uses Unity as sample implementation.

Logging implementation is asynchronous and it uses BackgroundWorker class to process log to consuming framework. The current implementation uses a simple file to write the log. It can be enhanced to use the new .net based async/await support or AsyncCollection for cleaner implementation.

The other major feature it supports is ClaimsIdentity over the existing IIdentity implementation for Claims based access. It allows access to User attributes in the runtime for more fine grain control over the user properties and access. This can further be extended to support Federation authentication within the application.

I haven’t added the full glass mapper example and unit testing yet but that’s in-progress.

How to setup

This solution is consist of 3 main module, 1) Framework 2) Modules (Mvc Areas) 3)Host Project

Framework is build with 4 projects

  1. Infrastructure- Infrastructure project contains dependency injection and Logging functionality.
  2. Sitecore Extension- This project contains extensions for Sitecore areas & error handling pipelines, support for ClaimsIdentity and some helper classes.
  3. Web Extension- Currently it has helper and extension classes to support Form Post.
  4. Setup- This project contains unity based DI implementation, file logger and application bootstrap to wire-up all project together without making any modification in Host project.

Module is nothing but collection of Mvc projects with area registration classes to support pluggable application development and it can be deployed to Host project as package.

Host is a Sitecore project and it works are integration point for Framework and pluggable Mvc modules.

Steps

  1. Download the source code for this article from GitHub and open the solution in Visual Studio 2013, you will notice it has all the projects available but host project doesn’t have much except, App_Config/Include/Framework for config file required by Framework, empty Areas folder, script and content folder for jquery and bootstrap css files.
  2. Download the Sitecore 7.2 from SDN.
  3. Copy below files/folder from Sitecore7.2 downloaded zip file to Host project directory.
    1. App_Config folder
    2. bin folder
    3. Layouts folder
    4. Sitecore folder
    5. Sitecore Modules folder
    6. Sitecore_files folder
    7. Temp folder
    8. Data folder with license file
    9. Upload folder
    10. xsl folder
    11. Default.aspx
    12. default.css
    13. default.js
    14. global.asax
    15. Web.config
    16. webedit.css

      Once everything is ready, build the solution and it should generate the required assembly inside the Host project’s bin folder to run the application.

      Disclaimer
      This solution is still in development and not been used on any production environment. I am trying my best to test out all the aspect of framework but if you are planning to use this framework, let me know and I will try to help you set it up, just shoot me an email.

      Any feedback, drop in the comment section or email me.

      Advertisement

About cprakash

A developer, passionate about .Net, Architecture and Security.
This entry was posted in Framework, MVC, Security, Sitecore and tagged , , , , . Bookmark the permalink.

9 Responses to Development Framework for Sitecore MVC

  1. patrickhuber says:

    Hey Chandra! Great post. I was just talking to Rapp about this and he sent me to this link:
    http://www.awareweb.com/awareblog/10-24-14-sitecoremvc

    I remember one of the big challenges we had in the past was due to application isolation when trying to host a multi tenant architecture. With .NET 4.5, microsoft release the Managed AddIn Framework. This may give you a bit more isolation than MVC areas and supports discoverability, versions and security . http://msdn.microsoft.com/en-us/library/bb384200(v=vs.110).aspx.

    Check it out and keep up the great posts!

    • cprakash says:

      Thanks Patrick, its really great to have your feedback. For 1st one, the article referenced applies only on controller resolved using fully qualified type name, but we are injecting controller name, area name and namespace in the route data and SitecoreControllerFactory will hand over it to MVC Controller Factory to create the instance which looks for IDependencyResolver first for resolution and if not available then it will go for Mvc’s IControllerFactory implementation.

      Controller Resolution by IDependencyResolver

      For 2nd one, I will look into it, but my first thought is that AreaRegistration class works as plugin manager and at runtime it discover all the AreaRegistration implementation by scanning assemblies in bin folder (even ones which are not referenced) which avoid the hard dependency on Host application assembly.

  2. Pingback: Form Post in Sitecore MVC | cprakash

  3. Pingback: Sitecore 8: Fix MVC area execution and registration | cprakash

  4. Pingback: Exception Handling in Sitecore MVC | cprakash

  5. Paritosh Tripathi says:

    Hey,
    I tried your framework but i get error while at Line 32: string value = controllerRunner.Execute(); in AreaControllerRenderer as Execute method calls the CreateController Method which while creating the Controller throws error as correct controller name is not getting passed. I’ve been struggling with this issue for quite a long time. Can you help. Below is the error –
    The request for ‘Home’ has found the following matching controllers:
    SitecoreMVC.Areas.Site1.Controllers.HomeController
    SitecoreMVC.Areas.Site2.Controllers.HomeController

    I have Two Areas namely Site1 and Site2 and in setting I’ve configured as below :

    I want to access the sites as dev.site1.com and dev.site2.com.
    Also If i override the initializeroute pipeline and register my areas i’m able to access my sites using dev.mvcsite.com/site1 and dev.mvcsite.com/site2 but I want to access these sites with direct hostnames not subdomain names.

    • cprakash says:

      Hi Paritosh,
      Thanks for reaching out, you need to do following.

      1. Define ‘Namespace’ field in AreaController Rendering, see Sitecore Template section in – https://cprakash.com/2014/10/27/sitecore-and-mvc-areas-as-pluggable-module/
      2. In Area Registration, along with route registration fill in the namespace parameter to resolve the correct controller

        routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new[] { "AppName.Controllers" }
        );

      For multiple domain name, if you define ‘host’ name parameter for each of the site in Sites section with configuration, it should work. See – https://sdn.sitecore.net/Articles/Administration/Configuring%20Multiple%20Sites.aspx

      Let me know, how it goes. Need any help feel free to reachout.

      • Paritosh says:

        Hi Prakash,

        I’ve added everything as per your blog. I have a Area Controller Rendering already with Area and Namespace and have set the GUID in the config for the template to fetch the data from sitecore. In the AreaControllerRenderer all the values are available and i set it to the RequestContext as well but when the ControllerRunner.Execute() method is fired it internally calls the CreateController method where the Area or something is missing due to which it fails to create the correct controller . I used .net reflector check all this but not sure where i’m making mistake. I assume it is something very small which i’m overlooking. Can you try to create two Areas and have HomeController and Index action in both Areas and inside the sites configuration add two host names 1 for Site1 and another for Site2 pointing to respenctive areas and in Sitecore add two nodes Site1 and Site2 having presentation details set with Home (Area Controller Rendering) respective rendering for Site1 and Site2

        Let me know if you are on skype maybe i can connect there and show you where i’m stuck.
        Regards,
        Paritosh

      • cprakash says:

        I am using the same setup at work and it works so its best to look at your code, you can reach out to me on Skype via my outlook id, it’s on About page. Alternatively, you can upload the relevant source code anywhere and I can take a look.

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 )

Connecting to %s