Windsor Spring

using Castle.MicroKernel.Registration;
using Castle.Windsor;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;

namespace DARSA.DI
{
    public partial class Spring
    { 
        private static IWindsorContainer container = null;
         
        private static IWindsorContainer GetContainer()
        {
            if (container == null)
            {
                container = new WindsorContainer();   
                container.Register(Classes.FromAssemblyNamed("DARSA.AMAZON.IBLL").Where(type => type.Name.EndsWith("Session")).WithServiceDefaultInterfaces().LifestyleTransient());
                container.Register(Classes.FromAssemblyNamed("DARSA.AMAZON.IDAL").Where(type => type.Name.EndsWith("SessionFactory")).WithServiceDefaultInterfaces().LifestyleTransient()); 
                container.Register(Classes.FromAssembly(GetAssemblyNamed("DARSA.AMAZON.BLL")).Where(type => type.Name.EndsWith("Session")).WithServiceDefaultInterfaces().LifestyleTransient()); 
                container.Register(Classes.FromAssembly(GetAssemblyNamed("DARSA.AMAZON.DAL")).Where(type => type.Name.EndsWith("SessionFactory")).WithServiceDefaultInterfaces().LifestyleTransient());
            }
            return container;
        }
        private static Assembly GetAssemblyNamed(string assemblyName)
        {
            if (string.IsNullOrWhiteSpace(assemblyName))
                throw new Exception("程序集名称不能为空!");
             
            try
            {
                Assembly assembly;  
                if (IsAssemblyFile(assemblyName))
                {
                    assembly = Assembly.Load(AssemblyLoadContext.GetAssemblyName(assemblyName));
                }
                else
                {
                    assembly = Assembly.LoadFile(string.Format(@"{0}{1}.dll", AppContext.BaseDirectory, assemblyName));
                }  
                return assembly;
            }
            catch (FileNotFoundException)
            {
                throw;
            }
            catch (FileLoadException)
            {
                throw;
            }
            catch (BadImageFormatException)
            {
                throw;
            }
            catch (Exception e)
            {
                // in theory there should be no other exception kind
                throw new Exception(string.Format("Could not load assembly {0}", assemblyName), e);
            }
        }
        private static bool IsDll(string extension)
        {
            return ".dll".Equals(extension, StringComparison.OrdinalIgnoreCase);
        } 
        private static bool IsExe(string extension)
        {
            return ".exe".Equals(extension, StringComparison.OrdinalIgnoreCase);
        }
        private static bool IsAssemblyFile(string filePath)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }

            string extension;
            try
            {
                extension = Path.GetExtension(filePath);
            }
            catch (ArgumentException)
            {
                // path contains invalid characters...
                return false;
            }
            return IsDll(extension) || IsExe(extension);
        }

        public static T GetObject<T>(string objName)  
        {
            if (container == null)
                GetContainer();
            return container.Resolve<T>();
        }
    }
}    
原文地址:https://www.cnblogs.com/valeb/p/14242395.html