4

Currently in a debate at work and I cannot fathom why someone would intentionally avoid Inheritance with Master Pages.

For reference here is the project setup:

  • BaseProject
    • MainMasterPage
      • SomeEvent
  • SiteProject
    • SiteMasterPage
      • nested MainMasterPage
  • OtherSiteProject
    • MainMasterPage (from BaseProject)

The debate came up because some code in BaseProject needs to know about "SomeEvent". With the setup above, the code in BaseProject needs to call this.Master.Master. That same code in BaseProject also applies to OtherSiteProject which is just accessed as this.Master.

SiteMasterPage has no code differences, only HTML differences.
If SiteMasterPage Inherits MainMasterPage rather than Nests it, then all code is valid as this.Master.

Can anyone think of a reason why to use a Nested Master Page here instead of an Inherited one?

3
  • Check out codeproject.com/Articles/18641/… Mar 27, 2012 at 20:37
  • @Nick : Yeah, already ran across that. So far the only reason is some problem someone had in 2007 that no one ever bothered to figure out? :P Mar 27, 2012 at 20:56
  • I am going to guess that those HTML differences are why it was separated out. Since the whole idea of MP inheritance is to support Visual Inheritance, then I'm guessing that there either were actual or imagined (either concurrent or future) issues with the layout. Mar 30, 2012 at 19:29

1 Answer 1

3

No.

First, I'd recommend using the MVP design pattern for webforms. Separate the logic that manipulates the view from the logic that manipulates the data model (View Logic, Business Logic). Put all your logic in classes instead of in the code-behind. Also, decide which works best for you: Passive View or Supervising Controller.

SiteMasterPage has no code differences, only HTML differences.

Extract all the logic into a separate class: [Description]MasterPage such as class DefaultMasterPage. Each MasterPage would inherit this class. This way you're not duplicating code. The only differences will be with the html in the MasterPages.

If the logic begins to diverge you now have the option to Inherit and Override functionality (still in separate classes, not in the code-behind).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.