简体   繁体   English

如何模拟ASP.NET基础页的多重继承?

[英]How to simulate multiple inheritance for ASP.NET base pages?

My project has been using a base page to hold common page functionality. 我的项目一直在使用基本页面来保存常见页面功能。 Its worked until the base page grew to be huge, so I tried to factor the base page into several classes. 它一直有效,直到基础页面变得巨大为止,因此我尝试将基础页面分为几个类。 Then I realized that I can't write 然后我意识到我不会写

public MyPage : SecurePage, PageWithGridViewSorting, PageWithSessionWarning

given something like: 给出类似的东西:

public class SecurePage : RadAjaxPage
{
    // Add session id to ViewStateUserKey
}

public class PageWithGridViewSorting : RadAjaxPage
{
    // add sorting to all grids on page
}

public class PageWithSessionWarning : RadAjaxPage
{
    // inject javascript to warn user of impending session end
}

Which way am I supposed to take this? 我应该采取哪种方式? And I think the fact that this is the ASP.NET page is relevant because it is a class that gets constructed by the framework, not me. 而且我认为这是ASP.NET页这一事实很重要,因为它是由框架而不是由我构造的类。 My first thought was to make a override for the constructor and pass in the features and that wont work with System.Web.UI.Page. 我的第一个念头是对构造函数进行覆盖,并传入功能,并且无法与System.Web.UI.Page一起使用。

Do the functions provided by SecurePage , PageWithGridViewSorting and PageWithSessionWarning actually need to be overloaded or overridden by their derived classes? SecurePagePageWithGridViewSortingPageWithSessionWarning提供的功能实际上是否需要由其派生类重载或覆盖?

If all you're doing is using inheritance to group functions, you should consider using a different method of organizing functions into classes. 如果您所做的只是使用继承对函数进行分组,则应考虑使用将函数组织到类中的另一种方法。 Like, for example, the Strategy pattern . 例如,例如战略模式 A page with sorting is a page, with sorting. 具有排序的页面就是具有排序的页面。 It's a page that has sorting. 这是一个具有排序的页面。 Using inheritance isn't the right way to approach this problem. 使用继承不是解决此问题的正确方法。

If they do need to differ in the derived classes, you might want to approach the problem with interfaces instead of inheritance. 如果它们确实需要在派生类中有所不同,则可能需要使用接口而不是继承来解决问题。 Since you'll be redefining the functions anyway this shouldn't increase complexity or make you repeat yourself. 因为无论如何您都将重新定义功能,所以这不会增加复杂性或使您重复自己。

EDIT: Given the specifics of your problem, it really sounds to me like you should investigate aspect-oriented programming and how you can fit it into your language . 编辑:鉴于您问题的具体情况,对我来说,确实听起来像您应该研究面向方面的编程以及如何使其适合您的语言

I would try to move over some of the content from BasePage to UserControls. 我会尝试将某些内容从BasePage移到UserControls。 Lets say PageWithSessionWarning. 可以说PageWithSessionWarning。 This could be a control that you put on the masterpage. 这可能是您放在母版页上的控件。

I think ASP.NET is an environment where you should prefer composition to inheritance. 我认为ASP.NET是一个您应该更喜欢继承而不是继承的环境。 You should consider restructuring your code to "inject" the necessary functionality by composing your page from helper classes that hook into the Page events and provide the functionality. 您应该考虑重组代码,以通过挂钩到Page事件并提供功能的帮助程序类组成页面来“注入”必要的功能。 into a shared structure that other pages can use. 成其他页面可以使用的共享结构。

This can be a tricky thing to do well in ASP.NET because the page lifecycle is complex and can often "get in your way" when trying to write generic code. 在ASP.NET中,要做到这一点可能是一件棘手的事情,因为页面生命周期很复杂,并且在尝试编写通用代码时常常会“妨碍您”。

You could also try to move functionality into custom or user controls, if the functionality is UI-centric. 如果功能是以UI为中心的,您也可以尝试将功能移至自定义控件或用户控件中。

Also, master pages are an effective way to factoring common functionality out of a page and 此外,母版页是从页面中分解出常用功能的有效方法,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM