簡體   English   中英

如何在分層體系結構中使用溫莎城堡安裝依賴項

[英]how to install dependencies with castle windsor in a layered architecture

我在MVC項目中將Castle Windsor用於ioc。

我的架構基本上是Web-> Services-> Data

控制器將服務作為依賴項。 服務具有存儲庫作為數據層中的依賴項。

我的Web層沒有對數據層的引用。 我的問題是我試圖注冊我的存儲庫,這是我的服務的依賴項。 如果我在服務層中有一個單獨的容器來注冊存儲庫,我應該如何引導它?

否則我可能會以錯誤的方式進行操作。

如果將數據層功能類(AKA存儲庫)的接口放置在域層中,則Web層不必引用數據層。 然后,您可以輕松地依賴在Web層中初始化的單個容器。

該問題包含更多細節。

有關實現此功能的一些高級方法,請參考mvc擴展

這個怎么樣。 缺點是您需要對存儲庫dll的名稱進行硬編碼,但是您始終可以將其移動到web.config等,這樣會更加簡潔。

IWindsorContainer container = new WindsorContainer();

// Register repositories
_container.Register(
        AllTypes.Pick()
                .FromAssemblyNamed("MyDataLayerAssembly")
                .WithService
                .DefaultInterface());

// Register services
_container.Register(
        AllTypes.Pick()
                .FromAssemblyNamed(typeof(ISomeService).Assembly.GetName().Name)
                .WithService
                .DefaultInterface());

ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));

但是,您可能只需要精確調整Register()方法中傳遞的內容以適合您的需求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM