I'm working on an ASP.NET Webforms project, using the MVP Pattern (which the presenters as you may know use a Page Controller pattern). I'm given a rather complex inheritance chain for my controllers. Below I depict a small example of the structure I'm facing (it is way bigger structure in my app):

enter image description here

A lot of code among these controllers is duplicated (i.e. creating cookies in RegisterPageController and LoginPageController, access Customer data in ProfilePage and BuyProductPage...), so I decided to systematically apply Template pattern and create an inheritance structure that would allow me to reuse as much of the code as possible (i.e. Creating an AuthenticatedPageController that checks that the user is authenticated and then BuyProductPage and ProfilePageController inherits from this class, reusing the authentication part of the code).

However, as I progress refactoring my code and apply more Template pattern, the inheritance chain starts to become more and more complex, and sometimes trying to create a new hierarchy among all my other classes becomes too tedious.

I was wondering if there was a better approach to tackle my issues. I've been reading about composition (I read this post Alternative to "inheritance versus composition?") but I'm not sure about how to implement it nor if this would be the best approach for my case.

Can anyone enlighten me?

Thank you.

2 Answers 2


If several controllers need cookie control then let them hold a CookieController that will handle that.

There is no need for the BuyProductController to also be a AccountController just because it needs the billing address.

  • But the classes that use the cookieController would be related to it via inheritance or via a member class of type CookieController? Commented Mar 28, 2014 at 11:39
  • 1
    @DavidJiménezMartínez they would only be related via the member field of type CookieController Commented Mar 28, 2014 at 11:54
  • Thank you for your answer. Just curious, would this approach be called "composition"? Commented Mar 28, 2014 at 12:06
  • 1
    @DavidJiménezMartínez yes putting utility objects as a field is composition, Commented Mar 28, 2014 at 12:11
  • Favor composition over inheritance is a design principle. Commented Mar 28, 2014 at 17:50

You might also want to take a look at the C# Decorator Pattern, which may be useful for sharing functionality between classes without complex inheritance.

  • This would make sense if there is some client method in all the controllers that is similar to operation() in the Decorator pattern. My experience with controllers is limited, but I am not sure this exists. Commented Mar 28, 2014 at 17:55

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.