Search code examples
c#asp.nethtmlmaster-pagessolid-principles

SOLID , MasterPages and ASCX?


I need to create a layout for a site. (I'm currently working on the master page).

However the logo (ascx) an be at different locations : ( location determined by query string value...).

enter image description here

  • Should the ascx know where to render itself by a query string

or

  • Should the master page provide it where to be rendered ?

FYI According to DIP ( Dependency Inversion) :

High-level classes should not depend on low-level classes. Both should depend on abstractions

So I guess that in thc ctor of the master page , I should provide to the ascx ctor where it should be rendered.

like in this first sample

  • Am I right ? or , there is a better way ?

(p.s. I already know that masterpage is also inherits usercontrol).


Solution

  • The master page would know how to render any surrounding div tags and/or set visibility to other regions of the html. It would be very awkward for the .ascx to know about its surrounding context.

    That being said, I would take things even further. I would let the master page fetch the query string value. But then I would have another class (LogotypeService) sport a method (GetPosition) that takes that value and make sense of it and return some enum (LogotypePosition). If the logo for some reason must know where it is, it could have this enum as a property being set by the master page.

    This way, neither your master nor your control contain the logic for figuring out why it should be rendered at a certain position. But they have enough information to put it in the correct position and even do some custom rendering if needed.