0
我遇到了使用Unity應用程序塊的問題,我創建了一個名爲Composition的基類。Unity應用程序塊,繼承注入
每個組合包含一個IUnityContainer,當我創建頂級對象UniversalComposition時,我想用UnityContainer初始化它。
從UniversalComposition創建的任何對象我想使用IUnityContainer.CreateChildContainer方法將一個子IUnityContainer注入到它。
class Program
{
static void Main(string[] args)
{
UniversalComposition universe = new UniversalComposition();
}
}
public class UniversalComposition : Composition
{
// Everything that gets resolved in this container should contain a child container created by the this container
public UniversalComposition()
{
this.Container.RegisterType<IService, Service>();
}
protected override IUnityContainer CreateContainer()
{
return new UnityContainer();
}
}
public abstract class Composition
{
protected IUnityContainer Container {get; private set;}
public Composition()
{
this.Container = this.CreateContainer();
}
public TInstance Resolve<TInstance>()
{
return this.Container.Resolve<TInstance>();
}
protected abstract IUnityContainer CreateContainer();
}
public class Service : Composition, IService
{
public Service(/* I want to inject a child Unity Container in here container.CreateChildContainer() */)
{
}
}
public interface IService { }
謝謝,我怎麼能自動注入父和孩子到DerivedClass?我不能簡單地將它標記爲'[InjectionConstructor]' – 2008-12-07 23:03:34