如何在页面和母版页之间共享代码而无需多重继承/代码重复?

发布于 2024-09-19 13:40:09 字数 867 浏览 5 评论 0原文

我已阅读问题/答案解释不存在多重继承在 C# 中,我们甚至不需要它,而且它会导致太多问题。

现在,我正在开发一个项目,我不太明白如何在没有多重继承、不重复代码的情况下做事。

情况是这样的。有一个网站,其主页和其他页面继承自母版页(主页不继承自)。页面和母版页都在执行一些操作:自定义登录、统计、加载用户设置以进行自定义等。目前,该解决方案很糟糕,因为这些任务的源代码只是复制了两次。

主页类继承自Page。另一方面,母版页继承自母版页。从逻辑上讲,继承一个公共类也很好,但它是多重继承,所以这是不可能的。

那么该怎么办呢?

我考虑了几种方法,但不喜欢它们:

  • 创建一个将从页面/母版页类调用的独立类。例如,我不会编写 bool isDisplayingTips = this.CurrentUser.IsDisplayingTips,而是编写 bool isDisplayingTips = this.SharedObjects.CurrentUser.IsDisplayingTips。我不喜欢它,因为写起来比较长。

  • 创建一个“真实的”、空的、通用母版页,并从中继承主页和母版页。它不仅需要编写更多代码来访问母版页参数,而且还会减慢速度,每个请求都需要一个额外的母版页。

有什么想法吗?

I've read the questions/answers explaining that there is no multiple inheritance in C#, that we don't even need it, and that it causes too much problems.

Now, I'm working on a project where I don't really understand how can I do things without multiple inheritance, without duplicating code.

Here's the situation. There is a website with a home page and other pages inheriting from a masterpage (the home page does not inherit from). Both the page and the masterpage are performing some stuff: custom login, statistics, loading of users settings for customization, etc. For the moment, the solution is crappy, since the source code for those tasks is just copied twice.

The home page class inherits from Page. The masterpage, on the other hand, inherits from Masterpage. Logically, it would be great to inherit from a common class too, but it's multiple inheritance, so it's impossible.

So what to do instead?

I thought about several ways, but dislike them:

  • Create a standalone class which will be called from the page/masterpage class. So for example instead of writing bool isDisplayingTips = this.CurrentUser.IsDisplayingTips, I would write bool isDisplayingTips = this.SharedObjects.CurrentUser.IsDisplayingTips. I don't like it, since it's longer to write.

  • Create a "real", empty, common masterpage, and inherit both the home page and the masterpage from it. Not only it will require to write more code to access masterpage parameters, but it will also slow the things down, requiring an additional masterpage on each request.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

心碎无痕… 2024-09-26 13:42:20

使用:

将被调用的独立类
来自页面/母版页类

添加一个基本页面和一个基本母版页,而不是停在那里。两者都使用共享类,并保留特定页面/母版页代码的间接性。

Use:

standalone class which will be called
from the page/masterpage class

but instead of stopping there, add a base page and a base master page. Both use the shared class, and keep the specific pages/master pages code from the indirection.

捶死心动 2024-09-26 13:42:06

我建议您使用第一个选项。如果你(可以理解)
对增加间接级别感到不舒服,您可以在独立类上创建新方法,例如:

public bool IsDisplayingTips(){
    return CurrentUser.IsDisplayingTips;
}

并且从您的页面中调用

bool isDisplayingTips = this.SharedObjects.IsDisplayingTips()

I suggest you to use the first of your option. If you (understandably)
don't feel comfortable with increased level of indirection, you could just create new methods on your standalone classe, e.g:

public bool IsDisplayingTips(){
    return CurrentUser.IsDisplayingTips;
}

and the from your pages just call

bool isDisplayingTips = this.SharedObjects.IsDisplayingTips()
温柔女人霸气范 2024-09-26 13:41:52

我不认为你的第二个选择是令人讨厌的。

我认为您的意思是创建一个基类,例如从System.Web.UI.MasterPage派生的MasterPageBase,并为您的主页创建一个空的MasterPage,它将继承于此MasterPageBase

如果做得好,它不应该减慢速度......

I don't find your 2nd option that dislikable.

I presume you mean creating a base class, e.g. MasterPageBase, derived from System.Web.UI.MasterPage, and creating an empty MasterPage for your homepage, that will inherit from this MasterPageBase.

If done right, it shouldn't slow things down...

前事休说 2024-09-26 13:41:35

MasterPage 是一个公正的控件(嵌入到实际页面中),因此您不能采用后面的方法。然而,创建另一个辅助类的第一种方法是相当可行的。

我们通常使用的另一种方法是使用

  1. 公共基页面类 - 所有页面都将从公共基页面继承。
  2. 将通用功能放入基页类中
  3. 从母版页,可以通过转换引用基页 - 例如,myBasePage = (BasePage)this.Page;。这样母版页就可以从基页类访问通用功能。

MasterPage is a just control (that get embedded into the actual page) so you can not have the later approach. However, first approach of creating another helper class is quite feasible.

Yet another approach that we typically use is to have

  1. Common base page class - all pages will inherit from the common base page.
  2. Put common functionality in base page class
  3. From master page, the base page can be referred by casting - for example, myBasePage = (BasePage)this.Page;. This way master page may access common functionality from base page class.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文