Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
But the classes that use the cookieController would be related to it via inheritance or via a member class of type CookieController? –  David Jiménez Martínez Mar 28 at 11:39
@DavidJiménezMartínez they would only be related via the member field of type CookieController –  ratchet freak Mar 28 at 11:54
Thank you for your answer. Just curious, would this approach be called "composition"? –  David Jiménez Martínez Mar 28 at 12:06
@DavidJiménezMartínez yes putting utility objects as a field is composition, –  ratchet freak Mar 28 at 12:11
Thank you for your answer @ratchetfreak –  David Jiménez Martínez Mar 28 at 12:17

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.

share|improve this answer
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. –  Fuhrmanator Mar 28 at 17:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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