EPiServer7x: Hooking up to Quicknavigator

Alf Nilsson provided this great post about Hooking up to EPiServer Quicknavigator.

I our solution we use StructureMap.

It looks a little different then:

LogoutQuickNavigatorItemProvider.cs:

using System.Collections.Generic;
using EPiServer;
using EPiServer.Core;
using EPiServer.Web;
using EPiServer.Web.Routing;
    public class LogoutQuickNavigatorItemProvider : IQuickNavigatorItemProvider
    {
        private readonly IContentRepository contentLoader;

        public int SortOrder
        {
            get { return 10; }
        }

        public LogoutQuickNavigatorItemProvider(IContentRepository contentLoader)
        {
            this.contentLoader = contentLoader;
        }

        public IDictionary<string, QuickNavigatorMenuItem> GetMenuItems(ContentReference currentContent)
        {
            var urlBuilder = new UrlBuilder("/logout");
            if (this.IsPageData(currentContent))
            {
                var urlResolver = UrlResolver.Current;
                string url = urlResolver.GetUrl(currentContent);

                urlBuilder.QueryCollection.Add("ReturnUrl", url);
            }

            return new Dictionary<string, QuickNavigatorMenuItem> {
                {
                    "customlogout",
                    new QuickNavigatorMenuItem("/shell/cms/menu/logout", urlBuilder.ToString(), null, "true", null)
                }
            };
        }
        private bool IsPageData(ContentReference currentContentLink)
        {
            return this.contentLoader.Get<ContentData>(currentContentLink) is PageData;
        }

    }

Registry.cs:

using StructureMap;
using StructureMap.Configuration.DSL;

    public class ServiceRegistry : Registry
    {
        public ServiceRegistry()
        {
            //... some other registrations

            For<IQuickNavigatorItemProvider>().Use<LogoutQuickNavigatorItemProvider>();
        }

DependencyResolverInitialization.cs

using System.Web.Mvc;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ImageLibrary;
using EPiServer.ServiceLocation;
using EPiServer.Shell;

    [ModuleDependency(typeof (ShellInitialization))]
    [InitializableModule]
    public class DependencyResolverInitialization : IConfigurableModule
    {
        public void ConfigureContainer(ServiceConfigurationContext context)
        {
            context.Container.Configure(x =>
            {
                x.Scan(a =>
                {
                    a.AssembliesFromApplicationBaseDirectory();
                    a.LookForRegistries();
                });
                x.For<ISharedContextFactory>().Use<SharedContextFactory>();
                x.For<IImageService>().Use<ImageService>();
                x.SetAllProperties(s => s.OfType<ISharedContextFactory>());
            });

            DependencyResolver.SetResolver(new StructureMapDependencyResolver(context.Container));
        }

StructureMapDependencyResolver.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using StructureMap;

    public class StructureMapDependencyResolver : IDependencyResolver
    {
        private readonly IContainer _container;

        public StructureMapDependencyResolver(IContainer container)
        {
            _container = container;
        }

        public object GetService(Type serviceType)
        {
            if (serviceType.IsInterface || serviceType.IsAbstract)
            {
                return GetInterfaceService(serviceType);
            }
            return GetConcreteService(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _container.GetAllInstances(serviceType).Cast<object>();
        }

        private object GetConcreteService(Type serviceType)
        {
            try
            {
                // Can't use TryGetInstance here because it won’t create concrete types
                return _container.GetInstance(serviceType);
            }
            catch (StructureMapException)
            {
                return null;
            }
        }

        private object GetInterfaceService(Type serviceType)
        {
            return _container.TryGetInstance(serviceType);
        }
    }

The End.

Leave a Reply

Please log in using one of these methods to post your comment:

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: