• 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>();
            }
        }
    }    
  • 相关阅读:
    文字超出省略号表示的几种方法
    Sqlserver数据库死锁
    Session丢失原因与解决方案
    CLR Profiler 性能分析工具 (转)
    微软HoloLens虚拟现实可以开发了。
    数据表分区
    SQLSERVER内核架构剖析 (转)
    理解SQL SERVER中的分区表(转)
    SQL Server Profile:使用方法和指标说明
    监视剪贴板(转)
  • 原文地址:https://www.cnblogs.com/valeb/p/14242395.html
Copyright © 2020-2023  润新知