2012-10-25 29 views
1

如果我有這樣的主持人 -註冊一個演示了查看

public class LandingPresenter : ILandingPresenter 
{    
    private ILandingView _view { get; set; } 
    private IProductService _productService { get; set; } 

    public LandingPresenter(ILandingView view, IProductService) 
    { 
     .... 
    } 
} 

如何註冊這個演示與Autofac考慮到依賴視的,不予登記(但IProductService會)

builder.RegisterType<LandingPresenter>().As<ILandingPresenter>(); ???? 
+0

你是什麼意思的「視圖不會被註冊」?你想明確提供它或..? –

+0

對不起,在視圖中我會像這樣構造Presenter - Presenter = new LandingPresenter(this); (IProductService會使用Autofac工廠注入) – Paul

回答

3

爲什麼不在容器中註冊視圖,請將Autofac工作!然後,您可以通過在演示者上使用構造函數注入並在視圖上注入屬性來自動連接演示者和視圖。你只需要註冊財產接線的觀點:

builder.RegisterAssemblyTypes(ThisAssembly). 
    Where(x => x.Name.EndsWith("View")). 
    PropertiesAutowired(PropertyWiringFlags.AllowCircularDependencies). 
    AsImplementedInterfaces(); 

主持人:

public class LandingPresenter : ILandingPresenter 
{    
    private ILandingView _view; 
    private IProductService _productService { get; set; } 

    public LandingPresenter(ILandingView view, IProductService _productService) 
    { 
     .... 
    } 
} 

查看:

public class LandingView : UserControl, ILandingView 
{ 
    // Constructor 

    public LandingView(... other dependencies here ...) 
    { 
    } 

    // This property will be set by Autofac 
    public ILandingPresenter Presenter { get; set; } 
} 

如果你想要去的觀點,第一,那麼你應該能夠扭轉它,因此演示者將視圖作爲屬性來代替。

+0

嗨,Daniel,謝謝你的迴應!我把它連接起來,因爲你發佈但是我得到一個錯誤:(---無法解析參數'Views.Landing.ILandingView視圖'的構造函數'Void .ctor(Views.Landing.ILandingView,....它似乎Autofac仍然不知道如何處理參數ILandingView我已經雙重檢查Autofac正在註冊正確的程序集任何想法? – Paul

+0

這意味着LandingView(作爲ILandingView實現者)的視圖尚未註冊。如果使用構建器.RegisterAssemblyTypes當你創建你的Autofac並確保參數(在我的例子中是ThisAssembly)指向包含視圖的程序集時,它應該工作。 –

+0

我現在已經通過了這個錯誤,但現在當加載Landing.aspx頁面時該視圖的實例被注入到控制器中,因爲它不同於在頁面加載時創建的實例,因此這會導致對象未設置爲基本實例y視圖中的所有屬性... :( – Paul