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();

/...

  

你可能感兴趣的:(unity)