ASP.NET BasePage 反向引用具体实现

发布于 2024-08-03 23:13:00 字数 1470 浏览 9 评论 0原文

我有一个像这样设置的页面

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2024-08-10 23:13:00

我不明白 (TView)this 预计如何工作。 this 指的是恰好是 Page 的类。您无法将 Page 转换为 IView

您当前的实现看起来并不那么糟糕。

我错过了什么吗?

编辑:现在我更好地了解了您的情况;让 ViewBasePage 从 IView 继承(并将其从您的 _Default 页面中删除)怎么样?

编辑 此外,如果您希望 _Default 页面必须实现接口中定义的功能,您可以让 ViewBasePage 类抽象地实现接口的功能。

public class _Default : ViewBasePage<Presenter<IView>, IView>
{
    #region Overrides of classB
    public override void test()
    {
        //perform the test steps.
    }
    #endregion
}
public abstract class ViewBasePage<TPresenter, TView> :
    Page, IView
    where TPresenter : Presenter<TView>
    where TView : IView
{
    protected TPresenter _presenter;

    public TPresenter Presenter
    {
        set
        {
            _presenter = value;
            _presenter.View = (TView)((IView)this); //<- Now it does work
        }
    }
    #region Implementation of IView
    public abstract void test();
    #endregion
}
public interface IView
{
    void test();
}
public abstract class Presenter<TView> where TView : IView
{
    public TView View { get; set; }
    public virtual void OnViewInitialized(){}
    public virtual void OnViewLoaded(){}
}

I don't see how (TView)this is expected to work. this is referring to the class which happens to be a Page. You can't convert a Page to an IView.

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.

public class _Default : ViewBasePage<Presenter<IView>, IView>
{
    #region Overrides of classB
    public override void test()
    {
        //perform the test steps.
    }
    #endregion
}
public abstract class ViewBasePage<TPresenter, TView> :
    Page, IView
    where TPresenter : Presenter<TView>
    where TView : IView
{
    protected TPresenter _presenter;

    public TPresenter Presenter
    {
        set
        {
            _presenter = value;
            _presenter.View = (TView)((IView)this); //<- Now it does work
        }
    }
    #region Implementation of IView
    public abstract void test();
    #endregion
}
public interface IView
{
    void test();
}
public abstract class Presenter<TView> where TView : IView
{
    public TView View { get; set; }
    public virtual void OnViewInitialized(){}
    public virtual void OnViewLoaded(){}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文