Richard Feich

Rich's Blog

Category: Umbraco

Dependency Injection and the Umbraco CMS Platform

On many projects I have used Ninject as my go-to dependency injection framework; it’s open source, flexible,  has good support, and a lot of extensions; but…it’s slow! Daniel Palme did some excellent work comparing the performance of various DI containers; see his results. Here is one of his conclusions supported by data:

Ninject is definitely the slowest container.

With this information I investigated using  the Simple Injector  DI framework. So far, I have used it on ASP.NET Web Forms, MVC, Web API, and console type applications. It’s a great simple super-fast DI framework, and its open source. I recommend trying it out [nuget  package].

Simple Injector is an easy-to-use Dependency Injection (DI) library for .NET 4+ that supports Silverlight, Windows Phone, Windows 8 including Universal apps and Mono. Simple Injector is easily integrated with frameworks such as Web API, MVC,  WCF, ASP.NET Core and many others. It’s easy to implement the dependency injection pattern with loosely coupled components using Simple Injector.”

I have been using Simple Injector in my custom Umbraco projects. Here is how it gets wired-up in the OnApplicationStarted() method:


...
using SimpleInjector;
using SimpleInjector.Integration.Web.Mvc;
using Umbraco.Core;
using Umbraco.Web.Mvc;

...

public class UmbracoEventHandler : IApplicationEventHandler
 {
        ...

        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var appName = ConfigurationManager.AppSettings["ApplicationName"];
            var websiteUrl= ConfigurationManager.AppSettings["WebsiteUrl"];
            var connectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;

            var memberRepoParms = new MembershipRepoParms
            {
                ApplicationName = appName,
                WebsiteUrl = websiteUrl,
                ConnectionString = connectionString,
                ApplicationContext = applicationContext
            };

            //Code for registering the repository class and DI
            // 1. Create a new Simple Injector container
            var container = new Container();

            // 2. Configure the container (register)
            container.Register<IMembershipRepository>(() => new MembershipRepository(memberRepoParms));
            container.Register<RenderMvcController>(() => new RenderMvcController());
            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

            // 3. Verify your configuration -- optional
            //container.Verify();

            // MVC Package - These two extension methods from integration package
            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // 4. Use the container examples -- this code go in your custom controllers.
            // var repository = container.GetInstance<IMembershipRepository>();
            // var repository = DependencyResolver.Current.GetService<IMembershipRepository>();

        }
 }

Umbraco CMS Platform

Umbraco is an open source content management system (CMS) platform written in C# built upon Microsoft’s .NET framework using ASP.NET MVC architectural model. Out-of -the-box, Umbraco is not a CMS per se, it’s really a platform for building a CMS; and an excellent one at that. I’ll been using this platform for 10+ years and I must say it has matured into a killer system for building websites and web-based applications. It has great architecture, super extensibility, and a fantastic development ecosystem (support, developers, plugins). Most importantly…the clients love the backoffice for managing content. The backoffice user-experience is fully customizable and components are implemented with AngularJS controllers and views.

To illustrate the power and extensibility of this system I am building a sample project and along the way I’ll explain various techniques and components. I am building an event management system, with grid calendars and event registration. More to come…

https://github.com/RichardFeich/UmbEventMgr

Features:

  • Hybrid Controllers (SurfaceRenderMvcController, RenderMvcController)
  • Dependency Injection using Simple Injector
  • Data access with Petapoco Micro-ORM and T4 templates
  • Strongly typed content access and views using Umbraco Models Builder
  • Responsive web design with Bootstrap
  • Google Maps integration
  • more…

© 2024 Richard Feich