使用视图注册演示者

时间:2012-10-25 23:04:44

标签: c# mvp autofac

如果我有这样的演示者 -

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>(); ????

1 个答案:

答案 0 :(得分:4)

为什么不在容器中注册视图,让Autofac工作!然后,您可以通过在演示者上使用构造函数注入和在视图上使用属性注入来自动挂接演示者和视图。您只需使用property-wiring注册视图:

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; }
}

如果您想先查看,那么您应该可以将其反转,以便主持人将视图视为属性。

相关问题