zoukankan      html  css  js  c++  java
  • 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>();
            }
        }
    }    
  • 相关阅读:
    Go视频教程
    Mysql常用
    同步Redux
    React跨组件通讯
    React组件通讯
    React
    git生成公钥和私钥
    常用经典算法---希尔排序
    string和c_str()使用时的坑
    腾讯云python网站开发环境搭建
  • 原文地址:https://www.cnblogs.com/valeb/p/14242395.html
Copyright © 2011-2022 走看看