如何在页面和母版页之间共享代码而无需多重继承/代码重复?
我已阅读问题/答案解释不存在多重继承在 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 writebool 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用:
添加一个基本页面和一个基本母版页,而不是停在那里。两者都使用共享类,并保留特定页面/母版页代码的间接性。
Use:
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.
我建议您使用第一个选项。如果你(可以理解)
对增加间接级别感到不舒服,您可以在独立类上创建新方法,例如:
并且从您的页面中调用
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:
and the from your pages just call
我不认为你的第二个选择是令人讨厌的。
我认为您的意思是创建一个基类,例如从
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 fromSystem.Web.UI.MasterPage
, and creating an empty MasterPage for your homepage, that will inherit from thisMasterPageBase
.If done right, it shouldn't slow things down...
MasterPage 是一个公正的控件(嵌入到实际页面中),因此您不能采用后面的方法。然而,创建另一个辅助类的第一种方法是相当可行的。
我们通常使用的另一种方法是使用
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
myBasePage = (BasePage)this.Page;
. This way master page may access common functionality from base page class.