c# - 使用 View 注册演示者

标签 c# mvp autofac

如果我有这样的主持人 -

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

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

考虑到依赖 View 不会被注册(但 IProductService 会),我如何向 Autofac 注册此 Presenter

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

最佳答案

为什么不在容器中也注册 View ,让 Autofac 发挥作用!然后,您可以通过在演示者上使用构造函数注入(inject)和在 View 上使用属性注入(inject)来自动连接演示者和 View 。您只需使用属性接线注册 View 即可:

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

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

关于c# - 使用 View 注册演示者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13078640/

相关文章:

design-patterns - 演示模型与被动 View

dependency-injection - Autofac.Core.DependencyResolutionException

c# - Entity Framework 创建复数表名,但 View 需要一个单数表名?

c# - 从 C# 启动 Excel 进入后台

vba - 使用 MVP 方法验证动态控件的用户表单输入

c# - Wcf 服务客户端不接收流消息

assemblies - 加载所有引用的程序集 .NET,即使未在代码中显式使用

c# - 通用 Windows 平台的 EF 迁移

c# - 如何在 ASP .NET 的 configureServices 函数中获取连接字符串

java - GWT 模型 View 演示器,重用演示器?或演示者 - 演示者沟通