ASP.NET BasePage 反向引用具体实现
我有一个像这样设置的页面
public partial class _Default : ViewBasePage<EmployeePresenter, IEmployeeView>,
IEmployeeView
{
...
}
在我的基本页面中
public abstract class ViewBasePage<TPresenter, TView> :
Page where TPresenter : Presenter<TView> where TView : IView
{
protected TPresenter _presenter;
public TPresenter Presenter
{
set
{
_presenter = value;
_presenter.View = GetView(); // <- Works
//_presenter.View = (TView)this; <- Doesn't work
}
}
/// <summary>
/// Gets the view. This will get the page during the ASP.NET
/// life cycle where the physical page inherits the view
/// </summary>
/// <returns></returns>
private static TView GetView()
{
return (TView) HttpContext.Current.Handler;
}
}
我需要做的实际上是强制转换 (TView)_Default,使用我的 GetView() 方法确实会以该结果结束。在基本页面内我无法执行此操作,
_presenter.View = (TView)this;
因为这实际上是 ViewBasePage
所以它不能直接转换为 TView。
所以我真正的问题是,有没有其他方法可以以一种感觉不那么老套的方式实现我的结果,如果这是主要选项,通过以这种方式处理我的页面,我真的需要担心什么吗?
编辑:
我试图写掉的确切部分是
private static TView GetView()
{
return (TView) HttpContext.Current.Handler;
}
因为我觉得这是一个相当严重的黑客行为,能够在这种情况下引用回页面。
I have a page that is setup like this
public partial class _Default : ViewBasePage<EmployeePresenter, IEmployeeView>,
IEmployeeView
{
...
}
Inside my base page
public abstract class ViewBasePage<TPresenter, TView> :
Page where TPresenter : Presenter<TView> where TView : IView
{
protected TPresenter _presenter;
public TPresenter Presenter
{
set
{
_presenter = value;
_presenter.View = GetView(); // <- Works
//_presenter.View = (TView)this; <- Doesn't work
}
}
/// <summary>
/// Gets the view. This will get the page during the ASP.NET
/// life cycle where the physical page inherits the view
/// </summary>
/// <returns></returns>
private static TView GetView()
{
return (TView) HttpContext.Current.Handler;
}
}
What I need to do is actually cast (TView)_Default, using my GetView() method does indeed end with that result. Inside the base page I can't do
_presenter.View = (TView)this;
Because this is actually ViewBasePage<TPresenter,TView>
so it can't directly cast to just TView.
So my actual question is there any alternative ways to achieve my result in a way that feels less hacky, if this is the primary option is there really anything I need to be concerned about by treating my page in this manner?
Edit:
The exact part I'm trying to write away is
private static TView GetView()
{
return (TView) HttpContext.Current.Handler;
}
as I feel like this is fairly gross hack to be able to reference back to the page in this context.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白
(TView)this
预计如何工作。this
指的是恰好是Page
的类。您无法将Page
转换为IView
。您当前的实现看起来并不那么糟糕。
我错过了什么吗?
编辑:现在我更好地了解了您的情况;让 ViewBasePage 从 IView 继承(并将其从您的 _Default 页面中删除)怎么样?
编辑 此外,如果您希望 _Default 页面必须实现接口中定义的功能,您可以让 ViewBasePage 类抽象地实现接口的功能。
I don't see how
(TView)this
is expected to work.this
is referring to the class which happens to be aPage
. You can't convert aPage
to anIView
.Your current implementation doesn't look at all hacky.
Am I missing something?
EDIT: now that I understand your situation a little better; what about having ViewBasePage inherit from IView (And removing it from your _Default page)?
EDIT Furthermore, if you then want the _Default page to have to implement the functions defined in the Interface, you can have the ViewBasePage class implement the interface's functions abstractly.