I am trying to add Autofac into my Webforms application (MVP pattern).
I have a problem with property injection. At the moment the Presenter property is instantiated with a brand new instance of the View class. That causes a null reference exception when 'view.Text' is set inside Presenter's constructor.
How can I configure autofac to instantiate Presenter with the instance of the parent View object?
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";
}
}
And the container configuration:
//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());