Who Depends On Whom?

 

Starting today, we’ll dive deep into dependency management. In my last article, I made a closing remark stating some types should be unaware of other types. Why? Because it would adversely affect the structure of our program; it would be harder for us to make changes. That is the correct answer. But it leaves out much detail. We’ll explore that detail today and over the next few days.

Let’s take a step back. Imagine we have some business logic code (BL) and some presentation logic code (PL). The presentation logic may be a User Interface (UI) or an API exposed over the HTTP. The business logic code encapsulates particular business rules and workflows. 

We realise that we can lump together the business logic code, maybe in a layer or a component. And we can do the same with the presentation logic code. In the figure below, the vertical line separates the business logic and presentation logic:

These separate layers of business logic and presentation logic must interact somehow. A dependency of exists between them. Either

  1. The Business Logic depends on the Presentation Logic, or
  2. The Presentation Logic depends on the Business Logic, or
  3. They depend on one another.

It stands to reason that the Presentation Logic must know about Business Logic. If it did not, how could a UI button click invoke a BL workflow? 

What about the inverse? Must Business Logic depend on Presentation Logic?

We have reached a critical point in our analysis. 

Indeed, we could make it so that the Business Logic is aware of the Presentation Logic. In that case, BL and PL know about each other and are dependent on one another. It would be as if we hadn’t separated the BL and PL code.

Let’s remember, though:

In programming, dependencies are something we want to minimise

We must have a dependency from PL to BL. But do we also have to have a dependency from BL to PL? 

It would be advantageous if BL did not require to know about Presentation logic. 

  • Should our business rules care whether they are exposed via a web API or a mobile or desktop app?
  • Should our business rules care whether a button on the UI is blue or grey?

No, our business rules should be unaware of these presentation details. If we had coupled our BL to a particular presentation logic implementation, say, desktop app, we would not be able to embed the same BL in a Web API. 

Keeping the business logic independent of presentation logic concerns makes our software more flexible. We will be able to move our BL between different presentation logic implementations with ease. 

OK, we have reached the conclusion that Presentation Logic must depend on Business Logic the reverse is best avoided.

Next time we will look into how types are affected by this one-way dependency between BL and PL. 

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply