向视图注册演示者 [英] Register a Presenter with a View

查看:76
本文介绍了向视图注册演示者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的演示者-

If I have a presenter like this -

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

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

考虑到依赖视图将不会被注册(但IProductService将被注册),我如何在Autofac中注册此Presenter

How do I register this Presenter with Autofac considering the dependent view will not be registered (but IProductService will)

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

推荐答案

为什么也不在容器中注册视图,也可以使用Autofac!然后,您可以通过在演示者上使用构造函数注入并在视图上使用属性注入来自动挂钩演示者和视图.您只需要通过属性接线来注册视图:

Why not register the views in the container as well, put Autofac to work! Then you can hook up presenters and views automagically by using constructor injection on the presenters and property injection on the views. You just have to register the views with 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; }
}

如果您想先查看,那么您应该可以将其反转,以便演示者将视图作为属性.

And if you want to go view-first then you should be able to reverse it so the presenters take the view as property instead.

这篇关于向视图注册演示者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆