Source

RazorLib / src / RazorLib.Infrastructure / Site / StructureMapContainer.cs

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

namespace RazorLib.Infrastructure.Site
{
    public class StructureMapContainer : IDependencyResolver
    {
        static IContainer _container;

        public StructureMapContainer(IContainer container)
        {
            _container = container;

            _container.Configure(x => x.Scan(y =>
            {
                y.AssembliesFromApplicationBaseDirectory();
                y.WithDefaultConventions();
                y.LookForRegistries();
            }));
        }

        public object GetService(Type serviceType)
        {
            return _container.TryGetInstance(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _container.GetAllInstances<object>()
                .Where(s => s.GetType() == serviceType);
        }
    }
}