ASP.NET的BasePage回引用到具体实施 [英] ASP.NET BasePage back referencing to concrete implementation

查看:104
本文介绍了ASP.NET的BasePage回引用到具体实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,设置这样的

I have a page that is setup like this

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()方法与结果确实到底是什么。基本页面内我不能做。

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;

由于这实际上是 ViewBasePage&LT;吨presenter,TView方式&gt; ,因此它不能直接转换为只TView

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?

编辑:

我想的是写远确切的部分。

The exact part I'm trying to write away is

private static TView GetView()
{
    return (TView) HttpContext.Current.Handler;
}

因为我觉得这是相当总值(GDP)黑客以能够引用返回页面在这种情况下。

as I feel like this is fairly gross hack to be able to reference back to the page in this context.

推荐答案

我看不出(TView)本预期工作。 这个指的是这恰好是一个的类。你不能转换 IVIEW

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.

我缺少的东西吗?

编辑:现在,我理解你的处境好一点;关于有ViewBasePage从IVIEW继承(从你_Default页面移除)什么?

now that I understand your situation a little better; what about having ViewBasePage inherit from IView (And removing it from your _Default page)?

修改此外,如果你再想让_Default页面,必须实现接口中定义的功能,可以让ViewBasePage类实现接口的功能抽象。

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(){}
}

这篇关于ASP.NET的BasePage回引用到具体实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆