0
我想做一些設置我的應用程序(初始化)。我在Global.asax.cs
中這樣做。如何向非控制器類注入依賴項?
我將需要依賴(可能是庫)來實現我的目標。
如何注入的IFooRepository
的實現?
public class MvcApplication : HttpApplication
{
private static IFooRepository _fooRepository;
protected void Application_Start()
{
// ...
IFoo foo = _fooRepository.Get(0);
foo.DoSomething();
}
}
我試過,但它失敗:
public class RepositoriesInstaller : IWindsorInstaller
{
void IWindsorInstaller.Install(Castle.Windsor.IWindsorContainer container,
Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
container.AddFacility<TypedFactoryFacility>();
container.Register(
Component.For<IFoo>()
.ImplementedBy<Foo>()
.LifestyleTransient(),
Component.For<IFooRepository>().AsFactory());
container.Register(Classes.FromThisAssembly()
.BasedOn<IFooRepository>()
.WithServiceDefaultInterfaces()
.LifestyleTransient());
}
}
如何依賴注入不構成東西(靜態類)?
我通過閱讀documentation for interface-based factories,但我不明白。
有什麼設施?他們用於什麼?
如果我的解決依賴(溫莎城堡)是在另一個項目,我沒有從我的UI訪問它? –
這是不太可能的情況。您的容器的配置必須從應用程序的啓動路徑(又名[Composition Root](http://blog.ploeh.dk/2011/07/28/CompositionRoot/))和MVC啓動路徑觸發你的'MvcApplication'。 – Steven
這就是我的情況:\我想要什麼? –