简体   繁体   English

MVP视图/演示者注册

[英]MVP view/presenter registration

I'm working on an MVP based GUI architecture for a WinForms application and would like to use Autofac to keep track of the different parts. 我正在为WinForms应用程序开发基于MVP的GUI体系结构,并希望使用Autofac来跟踪不同的部分。 I keep running into circular component dependencies and would appreciate a gentle push in the right direction. 我一直遇到循环组件依赖关系,并希望朝着正确的方向轻轻地推动。

The architecture is based on this post where the View is as passive as i gets. 该架构基于这篇文章,在文章中,View与我一样被动。 The View holds no reference to the Presenter. 视图不引用演示者。 The View is passed to the Presenter on construction. 视图将在构造时传递给Presenter。 So in the non-DI world you would start your program with: 因此,在非DI世界中,您将使用以下代码启动程序:

var MainView = new MainView();
var mainPresenter = new MainPresenter(mainView, new DataRepository());
Application.Run(mainView);

Ok, so the Presenter needs to know about the View instance to do its job. 好的,因此Presenter需要了解View实例才能完成其工作。 How can I express that in the registration code? 如何在注册码中表达出来? This is what I've tried: 这是我尝试过的:

builder.RegisterType<MainPresenter>().PropertiesAutowired().SingleInstance();
builder.RegisterType<MainView>().As<IMainView>().PropertiesAutowired().SingleInstance();

And then in Program.cs: 然后在Program.cs中:

var mainPresenter = Container.Resolve<MainPresenter>();
Application.Run(Container.Resolve<IMainView>() as MainView);

But this way I need to remember to create the Presenter instance. 但是通过这种方式,我需要记住创建Presenter实例。 However I would like to express in the registration that if I request a IMainView instance the MainPresenter should be kicked into action. 但是,我想在注册中表示,如果我请求IMainView实例,则应将MainPresenter付诸行动。 But how.... 但是...

Any hints, mockery or derisive laughter are welcome 任何暗示,嘲弄或嘲笑的欢迎

I think you should be able to solve it this way: Register the presenter and view without property injection since you say the view needs no reference to the presenter, and constructor injection is considered best practice in Autofac: 我认为您应该可以这样解决:注册演示者和视图而无需注入属性,因为您说视图不需要引用演示者,并且构造函数注入被认为是Autofac的最佳实践:

builder.RegisterType<MainPresenter>().SingleInstance();
builder.RegisterType<MainView>().As<IMainView>();

Inject the view into the presenter through constructor and publish it as a readonly property: 通过构造函数将视图注入到presenter中,并将其发布为只读属性:

public class MainPresenter
{
  // Private variables

  private readonly IMainView _view;

  // Constructor

  public MainPresenter(IMainView view)
  {
    _view = view;
  }

  // Properties

  public IMainView View
  {
    get { return _view; }
  }
}

Then you fire up the application through a single resolve: 然后,您通过一个解决方案启动应用程序:

var mainPresenter = Container.Resolve<MainPresenter>();
Application.Run(mainPresenter.View as Form);

Finally, if you find later on that you need a reference from the view to the presenter, I think you would have to use property-injection on the view to avoid circular reference exceptions. 最后,如果以后发现您需要从视图到演示者的引用,我认为您必须在视图上使用属性注入以避免循环引用异常。 Then you can register the view like this: 然后,您可以像这样注册视图:

builder.RegisterType<MainView>().As<IMainView>().PropertiesAutowired(PropertyWiringFlags.AllowCircularDependencies);

and supply the view with a read/write property that will be set by Autofac 并为视图提供由Autofac设置的读/写属性

public MainPresenter Presenter { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM