I have been meaning to extract out this solution for quite a while but never got time to do this. Even now I would like to publish a lot more code but unfortunately that is going to take a while!
I pushed an update to GitHub (https://github.com/akshaysura/sitecoremvc) with the initial code. It currently does not have any unit tests but I will be pushing another update shortly.
First I would like to thank Mike Edwards and Nathanael Mann for helping me jump start my Glass knowledge. I was able to pick up Glass in no time.
The purpose of this solution is to give you a baseline solution on Sitecore MVC implementation. I have been using a flavor of this for a year now on various projects and found it to be very useful and setup in a way that lends itself to reuse and test-ability.
Now a note to the snuggle bunnies who just see the negative in the world: when a guy posts Sitecore tips you do not find what is wrong with it, you acknowledge that fact that this person is sharing it his/her knowledge and if you have a better way of doing it, say so! It does not cost you any more to be nice! If you know of a way to make this better, share it.
On another note, share your knowledge, no point in safeguarding it! The more you share the better the community gets.
Now back to the solution. It is no surprise that I love Simple Injector, its my favorite. You can read all about it in the links below:
I found the combination of SimpleInjector, Glass4 and Sitecore MVC to be very stable and fast. In comparison we ran tests of 1000 active users, hitting the site at the rate of 635 requests per second, and the load being taken by two load balanced servers. The servers did not flinch and stayed at 35% CPU or below. This is a great example of how this can help you. Of course having good caching wouldn’t hurt!
I also tried out Kam’s Unicorn with Rainbow, I have to say that its fast and easy to configure. Best of all it was seamless which is what attracted me to try it out!
I plan on adding a lot more functionality in the coming weeks. The concepts in this solution are nothing new but how all of it is brought together might be interesting. Typically code isn’t shared in the Sitecore community when it comes to the actual building of the solution, but if its a module of course people are willing to freely share.
I am also going to include several examples of how to decouple Sitecore from a solution with real world examples. You are encouraged to pick and choose the code as you wish.
One thing to point out is the way Simple Injector is being initialized. Typically this can be done via Global.asax, WebActivatorEx and/or a Pipeline (the Sitecore way). I chose to do it the Sitecore way and initialize it via config and a pipeline.
using SimpleInjector; using Sitecore.Pipelines; using SitecoreMVC.SCExtensions.DI; using System; using System.Linq; using System.Web.Mvc; namespace SitecoreMVC.SCExtensions.Pipelines.DI { public class InitializeControllerFactory { public virtual void Process(PipelineArgs args) { SetControllerFactory(args); } protected virtual void SetControllerFactory(PipelineArgs args) { var container = new Container(); PackageExtensions.RegisterPackages(container, AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith("SitecoreMVC."))); // you can customize this to what ever you need var simpleInjectorControllerFactory = new SimpleInjectorControllerFactory(container); var sitecoreControllerFactory = new Sitecore.Mvc.Controllers.SitecoreControllerFactory(simpleInjectorControllerFactory); ControllerBuilder.Current.SetControllerFactory(sitecoreControllerFactory); } } }
using SimpleInjector; using System; using System.Web.Mvc; using System.Web.Routing; namespace SitecoreMVC.SCExtensions.DI { ////// Inspiration: http://blog.istern.dk/2012/10/23/sitecore-mvc-new-ninject-controller-factory-clean-version/ /// public class SimpleInjectorControllerFactory : DefaultControllerFactory { private Container _container; public SimpleInjectorControllerFactory(Container container) { _container = container; } protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { return (IController)_container.GetInstance(controllerType); } } }
If you have any questions or comments please let me know.
I just updated Nat’s IoC Battle results to include DryIoc (https://bitbucket.org/dadhi/dryioc/overview).
Singleton resolve speed (1M iterations):
new(): 142.3155ms
Dry: 193.3746ms
Simple: 227.6696ms
Windsor: 1,070.3861ms
Transient resolve speed (1M iterations):
new(): 435.7380ms
Dry: 498.7880ms
Simple: 739.9103ms
Windsor: 133,559.9208ms
The vast majority of IoC work in Sitecore will be transients. It’s worth picking the right container.
Hi Akshay, I downloaded simpleinjector resource from the website https://github.com/akshaysura/sitecoremvc and using those as platform dlls in our local project. But getting the following error
“The constructor of type XXXXController contains the parameter of type YYYY with name ‘zzz’ that is not registered. Please ensure YYYY is registered, or change the constructor of XXXXController”
Please help me in getting out of this issue.
Thanks.
can you send me some code in the simpleinjector.cs?
Hi Akshay, Please find below the code in simpleinjector.cs:
using Glass.Mapper.Sc;
using SimpleInjector;
using SimpleInjector.Packaging;
using SitecoreMVC.Business.Caching;
using SitecoreMVC.Business.Common;
using SitecoreMVC.Business.Metadata;
using SitecoreMVC.Business.Scripts;
using SitecoreMVC.Business.Shared;
using SitecoreMVC.Business.SitecoreSettings;
using SitecoreMVC.Models.TemplateModels;
namespace SitecoreMVC.Web.App_Start
{
public class SimpleInjector : IPackage
{
public void RegisterServices(global::SimpleInjector.Container container)
{
container.Register(() => new SitecoreContext());
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
//singleton
container.Register(Lifestyle.Singleton);
}
}
}
Thanks in Advance.
I do not see the registrations for the interface, are you missing them in your copy paste?
Yes Akshay. Missed that Part.
container.Register();
Thanks
Yes. Not able to paste the lines as it is. I am trying to register NotificationBarRepository with INotificationBarLogic(Custom Interface & Class) similar to that of MetaDataLogic, ScriptsLogic..etc(already Existing Interfaces).
Thanks
if we setup a quick gotomeeting I can take a look.
Hi sir, SitecoreMVC solution is really good. If you add unit testcases, It would have been really great 🙂