The Case for Using Use Cases In Code

The Case for Using Use Cases In Code

I was in a quandary. I hadn't come across a similar situation before. What was the best way of handling this?

About 20 years ago, I was working as a developer, and I came across an unusual situation. It became for me the seed of a beautiful learning experience.

OK, here is what happened. The firm I was working for at the time had a collection of web services. One of those web services performed background processing when called with a simple HTTP GET call. A timed scheduler program would make the call to the web service every hour to kick off the processing.

Now, the decision had been made to move this web code into a Windows Service where it could be scheduled to run at regular intervals without the need for the HTTP request. In those days, when Windows ruled supreme, it was the norm to employ a Windows Service for regular processing of background tasks.

The task fell to me to implement this change; move the code from the web service to the Windows Service. No problem. I had experience developing both web and Windows services. This change appeared a simple one. Innocently enough, I estimated it would take me only take 2-3 days.

As soon as I had copied and pasted the code from the web service into the Windows Service, there were numerous compilation errors. Way too many - Hundreds. 

Silly me. I had thought the code would compile and work almost right away. 

I had not considered that in a Windows Service, there was nothing 'web'. For example, the usual web constructs I was familiar with like HttpRequest, HttpResponse and HttpContext did not exist at all!

Cutting a long story short, I remember that it took me nearly a fortnight to get the code working again. This time in a non-web context. 

Once all the dust had settled, a question as forming in my head: 

Was it possible to structure code in a such a way that it could be picked up, without altering it, and moved to another delivery mechanism and it would work?

In other words, code that would build and run whether it was in a web service, Windows Forms application, console app, or anything else.

No alt text provided for this image

Unfortunately, I didn't dwell on the question and go looking for a solution. At the time, I might have thought it an impossible pipedream.

Fast forward to 2012. I learned about Robert C. Martin, aka Uncle Bob. 

In one of his videos, Uncle Bob espouses the notion of encapsulating high-level business logic in something called an 'Interactor'. 

These Interactors were to be the entry point to the Business Logic code. 

OK, what does this mean?

______________________________________________________

Business Logic Primer

The Business Logic of a system is WHAT needs to be done to achieve the desired business objective. 

Example: Loan Application Process

An overly simplified process to apply for a personal loan with a bank might run along the following lines: 

  1. Receive loan application form
  2. Check form for completeness
  3. Check customer's creditworthiness
  4. Check customer collateral
  5. Register customer
  6. Register customer loan on loan book
  7. Pay loan money to the customer  

Notice that the process described above would also work if done with paper, rather than computer code, the way lenders did it a century or more ago. 

This step-by-step process describes WHAT needs to happen, not HOW it is to happen. Notice how there is no mention of Web - Web could be one delivery mechanism. There are others; paper, mobile, clay tablets. 

The point is that at the highest level of WHAT a system does the delivery mechanism is unimportant.

______________________________________________________

OK, Let's return to Uncle Bob's Interactors, the high-level entry points to Business Logic when expressed in computer source code. 

Maybe a more intuitive name for Interactors could be Use Cases

Some readers might remember Use Cases from Unified Modelling Language (UML). In the 1990s and a bit after, UML was a popular tool to model software systems.

Hosted Business Logic encapsulated in Use Cases.

For example, in object-oriented source code, we should express the above loan application process as a Use Case class. We would then be able to host this class in a Windows Service, a Web app, a Windows Forms application, or any other delivery mechanism. 

Very powerful. Something to ponder on.

Conclusion

Use Cases are not only a UML construct. Their use in code creates an abstraction that allows the migration of high-level business logic from one delivery mechanism to another with relative ease. The first step in the ability to create pluggable systems. 

Next Time

We'll delve deeper into Use Cases. We'll examine the code for a Use Case class. We'll discover how Use Cases delegate action to other, lower-level components. We'll examine how Use Cases classes provide independence from particular database technologies and services. 

Robert Haegens

Principal Engineer at Empower

5 å¹´

Nice article, take a look at Uncle Bob's Clean Architecture which is a bit more of the HOW part.? I use a similar approach to his called the Onion Architecture but it all follows a similar path of outside in to the domain. This helps keep the environment you are running in mostly isolated from the use cases and domain logic.? I also like the concept of CQRS in it's purest form (without event sourcing) to help drive the concept of use cases and where/how to handle such a concept.? ? ? ?https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html

赞
回复

Very well explained, good stuff!

赞
回复

要查看或添加评论,请登录

Olaf Thielke的更多文章

  • The Road to Clean Software Architecture

    The Road to Clean Software Architecture

    What is the secret to good software architecture? What purpose does software serve? It allows developers to fashion…

  • Interfaces are Connectors

    Interfaces are Connectors

    For a long time, I didn't get interfaces. I am referring to interfaces as programming constructs in static languages…

  • In a Nutshell: Object-Oriented Programming Explained

    In a Nutshell: Object-Oriented Programming Explained

    In this article, we'll take a look at the purpose of Object-Oriented Programming using an easy to understand example…

  • Using Use Cases in Code

    Using Use Cases in Code

    Want to blow the minds of your fellow developers with a powerful technique in software development that almost nobody…

  • Watch Out for this Software Company Killer

    Watch Out for this Software Company Killer

    I worked for a start-up many years ago that was brought down by this One Thing. The company didn't go out of business…

    9 条评论
  • Handle Exceptions like a Professional (Part 2)

    Handle Exceptions like a Professional (Part 2)

    In Part 1, we looked at how runtime exceptions behave and why this behaviour makes them so useful in the right…

  • Handle Exceptions like a Professional (Part 1)

    Handle Exceptions like a Professional (Part 1)

    I still remember struggling with exceptions as a young developer. I didn't understand when and how to use them.

  • A Simple Way to Develop Self-Healing Systems

    A Simple Way to Develop Self-Healing Systems

    Ouch! 193 HTTP Internal Server 500 errors. 193 requests from our customers failed on the live site.

  • What the International Space Station Taught me about How to Design Software Systems

    What the International Space Station Taught me about How to Design Software Systems

    An Alternative International Space Station The International Space Station (ISS) is one of humanity’s greatest…

    2 条评论

社区洞察

其他会员也浏览了