Escape From the Framework. Or as Robert C. Martin says: “Don’t… | by Başar Özöğüt | Feb, 2022

Or as Robert C. Martin says: “Don’t marry the framework”

Başar Özöğüt
Photo by geraldo stanislas on Unsplash
The way tutorials teach you is the most basic and non-abstract way of coding. You access the database directly from the presentation layer (controller layer in this example).
Photo by Gala Rodriguez on Unsplash
The service interface and implementation for book creation and queries.
This is the way you might want your controller code to look like.
When you have the abstractions in place, you can call your business logic from any kind of application.
Injected dependencies.
BookService broken into another process as a REST api. Our MVC app is not affected by this change. We can just swap the implementation for the IBookService in a line of code if we are using a dependency injection library.

Food For Thought

  • We only abstracted away one layer of logic. This layer is still accessing the database directly through the entity framework, so it’s still tightly coupled to a framework. What will happen if you need to store data in a file, or in a database that the entity framework does not support? How can you overcome this problem?
  • What will happen when a BusinessException is thrown? Do you need to handle it, and where should you handle it?
  • Is leaking web constructs like HttpRequest or ViewBag directly into your service layer a good idea? What will happen if you want to use this business service in a Console app instead of a web app when you leak the web constructs?

This was a very general approach to a very broad topic. I highly recommend reading books like Clean Architecture: A Craftsman’s Guide to Software Structure and Design by Robert C. Martin to dive deeper into the subject. As Robert C. Martin says in the book: “Don’t marry the framework!”

Leave a Comment