34

In the coming months we're going to begin a project where we take a system we've built for a client (v1) and rebuild it from scratch. Our goal with v2 is to make it modular, so that this specific client will have their own set of modules they use, then another client may use a different set of modules altogether. The trick here is that Company A might have a series of checkout and user modules that change how that system works. Company B might stick with the standard checkout procedure but customize how products are browsed.

What are some good approaches to application architecture when you're building an application from scratch that you want to have a Core that's shared among all clients while still maintaining the flexability for anything to be modified specifically for a client?

I've seen CodeIgniter's hooks and don't think that's a good solution as we could end up with 250 hooks and it's still not flexible enough. What are some other solutions? Ideally we won't need to draw a line in the sand.

Ben
  • 625
  • 1
  • 5
  • 10

3 Answers3

26

To achieve highly organized and decoupled modularity, you can follow the Hierarchical MVC architectural pattern, sometimes known as Presentation–abstraction–control (although they aren't strictly the same pattern). Kohana, Alloy, Fluency and FuelPHP support HMVC natively* and Kohana's HMVC approach is discussed in Scaling Web Applications with HMVC and Optimising HMVC Web Applications for Performance, by Sam de Freyssinet.

Unfortunately CodeIgniter doesn't support HMVC natively. I've built my own libraries to provide some sort of HMVC support on CodeIgniter, taking some inspiration from wiredesignz's codeigniter-modular-extensions-hmvc. There's a very nice introduction to HMVC article at nettus+, that discusses CodeIgniter and wiredesignz's extension. The following image and quote are from that tutorial:

enter image description here

Each triad functions independently from one another. A triad can request access to another triad via their controllers. Both of these points allow the application to be distributed over multiple locations, if needed. In addition, the layering of MVC triads allows for a more in depth and robust application development. This leads to several advantages which brings us to our next point.

Finally, you are on the right track with hooks, even if you adopt an HMVC architecture, there is going to be some problems you'll still need to solve with hooks, depending on your implementation and the level of automation that you are looking for. A good use of hooks would be a pre_controller hook that would make sure all dependencies for installed modules are present, for example.

* There may be others I don't know of.

yannis
  • 39,547
  • 40
  • 183
  • 216
  • 1
    Does ASP.NET MVC have support for this? – Robert Harvey Nov 21 '11 at 18:51
  • @RobertHarvey I have no idea... – yannis Nov 21 '11 at 18:57
  • @Robert Harvey To me this seems like more of a pattern than a framework. Maybe I'm missing something but I can't see why I can't do this with inheritance in ASP.NET MVC. – Jeremy Nov 21 '11 at 19:17
  • 3
    @Jeremy: It gets tricky in the views, especially if you want to re-use parts of views. It looks intriguing, but I'm finding it hard to visualize at the nuts and bolts level. – Robert Harvey Nov 21 '11 at 19:20
  • In the diagram above, it looks like the upper-level views are being composed from smaller views located lower in the hierarchy. That can already be done to a certain extent with `RenderAction()` – Robert Harvey Nov 21 '11 at 22:02
  • @RobertHarvey [This answer](http://programmers.stackexchange.com/questions/84532/multi-mvc-processing-vs-single-mvc-process/84682#84682) to a very similar question discusses HMVC on ASP.Net/ – yannis Nov 22 '11 at 00:02
  • Brilliant answer. – kta Aug 21 '18 at 02:59
5

I'm not a PHP guy, but on the java-script side if you are going to have lots of modules that you would like to keep as independent as possible there are several systems for this, such as RequireJS, that have java-script modules that declare dependencies on other modules, and the framework makes sure they are loaded when needed.

The various frameworks out there vary in how dependencies are declared, how much dependency handling is done as a build step on the server, on page load, or dynamically upon request, and in how much you have to change the way you write your java-script.

These frameworks don't apply as much (or perhaps at all) if your server side framework is handling this or generating your java-script for you, but you could possibly want your java-script to be independent of your server side framework.

If you don't have too much java-script but you want some modularity, you can always stick to anonymous self calling functions that handle dependencies by ordering the script tags with dependencies first, and referencing each other by registering themselves with a single object in the global namespace. That at least makes them nearly modular and doesn't cost you much to implement.

psr
  • 12,846
  • 5
  • 39
  • 67
0

Here are some instructions on how to do modular design: https://class.coursera.org/saas/lecture/preview/9

also this: "SOA can be seen in a continuum, from older concepts of distributed computing and modular programming, through SOA, and on to current practices of mashups, SaaS,..."

HMVC code organization by itself, does not necessarily lead to modularity.

Goce Ribeski
  • 111
  • 2