如果我有这样的主持人 -
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/