Applications as Frameworks
Tags: cleancode, frameworks, designpatterns, antipatterns
Published: 2019-03-04 07:04:25

---
title: Applications as Frameworks
published: true
description: What are the difference between applications and frameworks, how can applications be used as frameworks and how can common problems be avoided?
tags: CleanCode, Frameworks, DesignPatterns, AntiPatterns
---

What are the differences between building on top of a framework and building on top of an application? How does using an application as a framework cause problems, and how can these problems be avoided? That is what this post is all about.
<h3>Decoupled web application</h3>
In your typical web application, the code handles a request and returns a response. Let's assume we are using a web framework to handle common tasks such as routing. Let's also assume that we think framework binding has a high cost and decouple our application from it. The flow of control would look like this:

![Diagram](https://thepracticaldev.s3.amazonaws.com/i/wka6x0trqajlay5uwry0.png)

Execution starts with the framework. For PHP frameworks this will be in a file like `public/index.php`. The framework then bootstraps itself and does a bunch of stuff. It's safe to assume this stuff will include routing, and often it also includes things like dependency construction and error handling.

After the framework did the tasks you want it to do, it hands control over to your application. Your application does a bunch of application and domain logic and interacts with persistence. It likely uses a number of libraries, especially for infrastructure tasks like logging and database access. Even so, control stays with the application. (The key difference between frameworks and libraries is that you control/call libraries while frameworks control/call you.) Your application might also be calling the framework and use it as a library. Again, control stays with the application.

Finally when the application is done, it hands some kinda of result back to the framework. The framework then does another bunch of stuff, like template rendering and translations. In case of a web framework it then spits out a HTTP response and execution ends.

An application like this keeps you in control of what happens, making it easier to change things. This style also makes it easy to decouple from the framework. There are only two points where you need to decouple.

![Diagram](https://thepracticaldev.s3.amazonaws.com/i/3q6jqztksjbzj40wkx40.png)

My post <a href="https://www.entropywins.wtf/blog/2016/11/24/implementing-the-clean-architecture/">Implementing The Clean Architecture</a> outlines one architectural approach that leads to this kind of application.
<h3>Frameworks vs Applications</h3>
Let's compare how frameworks and applications differ when they are used as a foundation for an/another application.

Frameworks don't do stuff on their own. There is no application or domain logic. There is no set of existing web pages or API endpoints with their own structure and behavior. This is all defined by your application when using a framework. When building on top of an application that acts as a framework, you'll need to deal with existing structure and behavior. You'll need to insert your own stuff, change existing behavior in certain situations and prevent default behavior altogether in others.

I know that there are "frameworks" that do provide their own stuff out of the box. (Example: web shop framework.) While they might not be a full application on their own, for the purpose of this blog post they are the same as an application that gets used as a framework.
<h3>Plugins and Extensions</h3>
There is nothing inherently bad about building things on top of an application. Plugins and extensions are a very useful pattern. A plugin that interacts with a single plugin point can decouple itself when appropriate and is in control over itself. And for extensions that use many extension points of the application yet are shallow/small, framework decoupling might not make sense.

This post is about using applications as framework foundation for sizable sets of code which are applications in their own right.
<h3>Applications as Frameworks</h3>
Let's imagine we have an application that is used on some site for some use case. We'll call this application FrameworkApp, since we'll use it as framework for another application that powers another site.

When building our application on top of FrameworkApp, we'll need to register new behavior and modify existing behavior. To make this possible, FrameworkApp needs to provide the appropriate extension points. Often these take the form of abstract classes or even systems, though the exact nature of the extension points is not important for our purposes.

This leads to a very different flow of control. Rather than calling us once, the FrameworkApp calls each extension point our application handles.

![Diagram](https://thepracticaldev.s3.amazonaws.com/i/hrnw8nio8t9wqqmirzx3.png)

The diagram is showing just 6 extension points, though there can be 100s.

When visualized like this, it becomes easy to see how decoupling from the framework becomes next to impossible. Even if you manage to avoid coupling to framework <em>code</em> in your application, its whole structure is still defined by the framework. This means you are very limited in what you can do in your application and need to understand the framework to effectively develop the application. Framework coupling causes more issues than that, though a comprehensive overview of those is out of scope for this post.
<h3>An OOP Solution</h3>
<blockquote>Favor composition over inheritance

<small>-- OOP principle</small></blockquote>
Using an application as a framework is very similar to using inheritance for code reuse.

Just like with the application that is build on top of the app that acts as framework, the subclass might not be in control and be invoked many times from the base class. This is especially the case when using the Template Method Pattern and when having a deep inheritance hierarchy. The flow of control can bounce all over the place and decoupling the subclass from the classes up the hierarchy becomes all but impossible.

You can avoid this classical inheritance mess by using composition. Which suggests one way to move away from using an application as a framework or avoid doing so altogether: stop treating the framework as a base class. If there is code to share, use composition. This way you stay in control, can decouple easier and avoid <a href="https://www.entropywins.wtf/blog/2017/09/06/the-fallacy-of-dry/">The Fallacy of DRY</a>.

Just like with class hierarchies you can always slap on an extra level.

<img src="https://thepracticaldev.s3.amazonaws.com/i/l7qnupqy6xisktau3q3a.png" alt="" width="400" /> <img src="https://thepracticaldev.s3.amazonaws.com/i/g5rd2d20gjavwa7l15tg.jpg" alt="" width="200" />

Thanks to <a href="https://somehowimanage.blog/">Raz Shuty</a> for proofreading and making some suggestions.

<a href="https://www.entropywins.wtf/blog/2019/02/28/applications-as-frameworks/">Applications as Frameworks</a> was originally posted on <a href="https://www.entropywins.wtf/blog">my blog</a>, where you can find many similar posts.