2

如果我有这样的主持人——

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

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

考虑到不会注册依赖视图(但 IProductService 会),我如何使用 Autofac 注册此 Presenter

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

1 回答 1

5

为什么不在容器中注册视图,让 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; }
}

如果你想先查看,那么你应该能够反转它,以便演示者将视图作为属性。

于 2012-10-27T23:07:03.110 回答