caloggins
11/9/2013 - 1:25 PM

Lets Windsor inject connection strings as primitives. Lifted from http://blog.ploeh.dk/2012/07/02/PrimitiveDependencies/.

Lets Windsor inject connection strings as primitives. Lifted from http://blog.ploeh.dk/2012/07/02/PrimitiveDependencies/.

namespace NAMESPACE
{
    using System.Configuration;

    using Castle.Core;
    using Castle.MicroKernel;
    using Castle.MicroKernel.Context;

    public class ConnectionStringConvention : ISubDependencyResolver
    {
        public bool CanResolve(
            CreationContext context,
            ISubDependencyResolver contextHandlerResolver,
            ComponentModel model,
            DependencyModel dependency)
        {
            return dependency.TargetType == typeof(string)
                && dependency.DependencyKey.EndsWith("ConnectionString");
        }

        public object Resolve(
            CreationContext context,
            ISubDependencyResolver contextHandlerResolver,
            ComponentModel model,
            DependencyModel dependency)
        {
            var name = dependency.DependencyKey.Replace("ConnectionString", "");
            return ConfigurationManager.ConnectionStrings[name].ConnectionString;
        }
    }
}