1

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 Answers2

6

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.

ratchet freak
  • 25,706
  • 2
  • 62
  • 97
0

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.

Franchesca
  • 109
  • 3
  • 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 '14 at 17:55