解决方案#1a
使用httpModule拦截context.PreRequestHandlerExecute来调用ObjectFactory.BuildUp(httpContext.Current.Handler)
public partial class _Default : Page,IEmployeeVIEw{ private EmployeePresenter _presenter; private EmployeePresenter Presenter { set { _presenter = value; _presenter.VIEw = this; } }}
解决方案#1b
在页面加载中调用buildup而不是使用httpModule
public partial class _Default : Page,IEmployeeVIEw{ private EmployeePresenter _presenter; private EmployeePresenter Presenter { set { _presenter = value; _presenter.VIEw = this; } } protected voID Page_Load(object sender,EventArgs e) { ObjectFactory.BuildUp(this); }}
解决方案#1c
如果需要,通过Property访问presenter允许Getter to BuildUp.
public partial class _Default : Page,IEmployeeVIEw{ private EmployeePresenter _presenter; public EmployeePresenter Presenter { get { if (_presenter == null) { ObjectFactory.BuildUp(this); } return _presenter; } set { _presenter = value; _presenter.VIEw = this; } }}
解决方案#2
public partial class _Default : Page,IEmployeeVIEw{ private EmployeePresenter _presenter; private EmployeePresenter Presenter { get { if (_presenter == null) { _presenter = ObjectFactory.GetInstance<EmployeePresenter>(); _presenter.VIEw = this; } return _presenter; } }}
解决方案#2b
public partial class _Default : Page,IEmployeeVIEw{ private EmployeePresenter _presenter; private EmployeePresenter Presenter { get { if (_presenter == null) { Presenter = ObjectFactory.GetInstance<EmployeePresenter>(); } return _presenter; } set { _presenter = value; _presenter.VIEw = this; } }}
编辑:添加解决方案1c,2b
解决方法 我使用解决方案#1b,并为所有页面创建一个 layer supertype,以便更多地干掉演示者初始化.像这样:页码:
public partial class _Default : AbstractPage,IEmployeeVIEw{ private EmployeePresenter presenter; private EmployeePresenter Presenter { set { presenter = value; presenter.VIEw = this; } } protected overrIDe voID Do_Load(object sender,EventArgs args) { //do "on load" stuff }}
摘要页码:
public abstract class AbstractPage : Page{ protected voID Page_Load(object sender,EventArgs e) { ObjectFactory.BuildUp(this); this.Do_Load(sender,e); //template method,to enable subclasses to mimic "Page_load" event } //Default Implementation (do nothing) protected virtual voID Do_Load(object sender,EventArgs e){}}
使用此解决方案,您只能在一个类中进行演示者初始化(由ObjectFactory创建),如果您以后需要对其进行修改,则可以轻松完成.
编辑:
Do_Load应该是抽象的还是虚拟的?
Template Method最初声明该方法应该是Abstract,以强制子类实现它,遵守超类契约. (参见维基百科“垄断”的例子<“游戏”). 另一方面,在这种特殊情况下,我们不希望强制用户类重新定义我们的方法,但给它机会这样做.如果你声明它是抽象的,许多类将被迫重新定义方法只是为了让它为空(这显然是代码味道).所以我们提供合理的默认值(什么都不做)并使方法成为虚拟的.
总结以上是内存溢出为你收集整理的c# – 带有StructureMap的Model-View-Presenter模式中的Presenter注入全部内容,希望文章能够帮你解决c# – 带有StructureMap的Model-View-Presenter模式中的Presenter注入所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)