我正在尝试将Autofac添加到我的Webforms应用程序(MVP模式)中。
我对属性注入有疑问。目前,Presenter属性使用View类的全新实例进行实例化。在Presenter的构造函数中设置“ view.Text”时,将导致空引用异常。
如何配置autofac以实例化Presenter的实例 父视图对象?
public interface IView
{
string Text { set; }
}
public partial class View : System.Web.UI.UserControl, IView
{
public Presenter Presenter { get; set; }
public string Text
{
set
{
ltText.Text = value;
}
}
}
public class Presenter
{
public Presenter(IView view)
{
view.Text = "Hello World";
}
}
以及容器配置:
//Global.ascx.cs...
var builder = new ContainerBuilder();
builder.RegisterType<Simple.Core.Views.View>().As<Simple.Core.Views.IView>();
builder.RegisterType<Simple.Core.Views.Presenter>().AsSelf().InstancePerRequest();
_containerProvider = new ContainerProvider(builder.Build());