views:

82

answers:

4

Hi,

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?

+1  A: 

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...

Dan Dumitru
+4  A: 

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.
VinayC
@VinayC: Your approach seems to me the best one. Thanks.
MainMa
+1  A: 

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()
Andrea Parodi
+1  A: 

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.

eglasius