c# - Autofac & WinForms Integration Issue -
i have simple winforms poc utilizing autofac , mvp pattern. in poc, opening child form parent form via autofac's resolve method. i'm having issues how child form stays open. in display()
method of child form, if call showdialog()
child form remains open until close it. if call show()
, child form flashes , instantly closes - not good.
i've done numerous searches integrating autofac winforms application, i've not found examples on autofac/winforms integration.
my questions are:
- what proper way display non-modal child form approach?
- is there better way utilize autofac in winforms application approach?
- how determine there no memory leaks , autofac cleaning model/view/presenter objects child form?
only relevant code shown below.
thanks,
kyle
public class mainpresenter : imainpresenter { ilifetimescope container = null; iview view = null; public mainpresenter(ilifetimescope container, imainview view) { this.container = container; this.view = view; view.addchild += new eventhandler(view_addchild); } void view_addchild(object sender, eventargs e) { //is correct way display form autofac? using(ilifetimescope scope = container.beginlifetimescope()) { scope.resolve<childpresenter>().displayview(); //display child form } } #region implementation of ipresenter public iview view { { return view; } } public void displayview() { view.display(); } #endregion } public class childpresenter : ipresenter { iview view = null; public childpresenter(iview view) { this.view = view; } #region implementation of ipresenter public iview view { { return view; } } public void displayview() { view.display(); } #endregion } public partial class childview : form, iview { public childview() { initializecomponent(); } #region implementation of iview public void display() { show(); //<== bug: child form flash instantly close. showdialog(); //child form display correctly, since call modal, parent form can't accessed } #endregion }
look @ code:
using(ilifetimescope scope = container.beginlifetimescope()) { scope.resolve<childpresenter>().displayview(); //display child form }
first, used child lifetime scope. if resolve out of top-level container, objects live long container (which whole lifetime of application).
however, there problem here. when displayview
calls form.show
, returns immediately. using
block ends, child scope disposed, , of objects (including view) disposed.
in case, not want using
statement. want tie child lifetime scope view when view closed, child scope disposed. see the formfactory
in 1 of other answers example. there other ways adapt idea architecture - in registration (containerbuilder) example.
Comments
Post a Comment