zoukankan      html  css  js  c++  java
  • Unity 容器常见使用声景

    /*****************************************************/
    //Create Class With Interface Dependencies
    #region
    //需要某实现某接口的具体实例
    UnityContainer container=new UnityContainer();
    container.RegisterType(ILogger,NullLogger>();
    Database datavase = container.Resolve<CustomDatabase>Database();
    
    #endregion
    CustomDatabase的构造函数包含ILogger参数
    CustomDatabase(ILogger log){}
    
    /*****************************************************/
    //Create TypeMapping-Interface
    UnityContainer container=new UnityContainer();
    container.RegisterType(ILogger,NullLogger>();
    ILogger logger = container.Resolve<ILogger>();
    
    /*****************************************************/
    //Register Singleton
    UnityContainer container=new UnityContaier();
    container.RegisterType<Database,SqlDatabase>
    	(new ContainerControlledLifetimeManager());
    /*****************************************************/
    //Registering Named Instances
    UnityContainer container=new UnityContaier();
    container.RegisterType<Database,SqlDatabase>("SQL");
    container.RegisterType<Database,OracleDatabase>("ORACLE");
    
    IEnumerable<Database> databases = container.ResolveAll<Database>(); //Return All named instances
    Database database = container.Resolve<Database>("SQL");
    /*****************************************************/
    //Register Existing Intance 
    //Registering Named Instances
    UnityContainer container=new UnityContaier();
    container.RegisterInstance<Database>(new SqlDatabase());	//注册instance,Unity认为它是Singleton
    container.RegisterInstance<Database>("ORACLE",new OracleDatabase());
    
    Database database = container.Resolve<Database>("ORACLE");
    
    /*****************************************************/
    //Buildup Existing Instace
    UnityContainer container=new UnityContainer();
    container.RegisterType(ILogger,NullLogger>();
    
    DB2Database existingDatabase = new DB2Database();
    
    container.BuildUp(existingDatabase);		//创建引用关系
    container.RegisterInstance<Database>(existingDatabase);
    
    Database database = container.Resolve<Database>();
    
    //-----
    public class DB2Database:Database{
    	[Dependency]
    	public ILogger Logger{get;set}
    }
    /*****************************************************/
    // Configure Container via Config File
    UnityContainer _contaier=new UnityContaier();
     UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
    section.Configure(_container); // Unnamed <container> element
    
    ICustomerDataSource datasource=_container.Resolve<ICustomerDataSource>();
    
    /*****************************************************/
    container.Configure<InjectedMembers>()
    	.ConfigureInjectionFor<GenericDatabase>(
    		new ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
    		new InjectionProperty("Logger")
    	);
    Database database=container.Resolve<Database>();
    /*****************************************************/
    //NestedContainers 父子容器,适用于,有公共容器。树型结构的容器o
    UnityContainer parentContainer  = new UnityContaier();
    IUnityContaier childContainer1 = parentContaier.CreateChildContainer();
    IUnityContaier childContainer2 = parentContaier.CreateChildContainer();
    /...
    

      

  • 相关阅读:
    javascript时间戳和日期字符串相互转换
    jquery两稳定版本比较~~
    原生的强大DOM选择器querySelector
    分享一个自定义的 console 类,让你不再纠结JS中的调试代码的兼容
    基于Mesos运行Spark
    chrome插件 postman 可以调用restful服务
    cassandra优秀博客集
    Cassandra监控
    Cassandra
    SecureCRT中文显示乱码的解决方法
  • 原文地址:https://www.cnblogs.com/wucg/p/2229728.html
Copyright © 2011-2022 走看看