如果我有这样的主持人——
public class LandingPresenter : ILandingPresenter
{
private ILandingView _view { get; set; }
private IProductService _productService { get; set; }
public LandingPresenter(ILandingView view, IProductService)
{
....
}
}
Run Code Online (Sandbox Code Playgroud)
考虑到依赖视图不会被注册,我如何向 Autofac 注册这个 Presenter(但 IProductService 会)
builder.RegisterType<LandingPresenter>().As<ILandingPresenter>(); ????
Run Code Online (Sandbox Code Playgroud)
为什么不在容器中注册视图,让 Autofac 工作!然后,您可以通过在演示者上使用构造函数注入和在视图上使用属性注入来自动连接演示者和视图。您只需要使用 property-wiring 注册视图:
builder.RegisterAssemblyTypes(ThisAssembly).
Where(x => x.Name.EndsWith("View")).
PropertiesAutowired(PropertyWiringFlags.AllowCircularDependencies).
AsImplementedInterfaces();
Run Code Online (Sandbox Code Playgroud)
主持人:
public class LandingPresenter : ILandingPresenter
{
private ILandingView _view;
private IProductService _productService { get; set; }
public LandingPresenter(ILandingView view, IProductService _productService)
{
....
}
}
Run Code Online (Sandbox Code Playgroud)
看法:
public class LandingView : UserControl, ILandingView
{
// Constructor
public LandingView(... other dependencies here ...)
{
}
// This property will be set by Autofac
public ILandingPresenter Presenter { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果您想先查看,那么您应该能够反转它,以便演示者将视图作为属性。
| 归档时间: |
|
| 查看次数: |
830 次 |
| 最近记录: |