Skip to main content

SOLID Principles Saved My Angular App from Database Drama

Updated 2 min readMarkdown
Part of seriestoday i learned

Ever tried swapping LocalStorage for Dexie DB in your Angular app? That was me yesterday鈥攕urrounded by coffee cups, error messages, and regret.

My Angular service was tightly coupled to LocalStorage like peanut butter to jelly. Changing to Dexie meant rewriting EVERYTHING. Then I remembered the SOLID principles:

Single Responsibility: Each class should do one job (not my service juggling data AND storage)

Open/Closed: Classes open for extension, closed for modification (my code was welded shut)

Liskov Substitution: Subtypes should be substitutable (my storage wasn't)

Interface Segregation: Don't force clients to depend on methods they don't use

But the real hero? Dependency Inversion! By depending on abstractions instead of concrete implementations, I created a StorageService interface that both LocalStorage and Dexie could implement.

Suddenly, switching databases was just swapping implementations, not rebuilding Rome.

The "D" in SOLID (Dependency Inversion) became my hero when I created a storage interface that both implementations could satisfy. Now my components don't care where data lives; they just ask nicely for it.

// Instead of this nightmare 馃槺
class UserService {
  private localStorage = window.localStorage;

  saveUser(user: User) {
    this.localStorage.setItem('user', JSON.stringify(user));
  }
}

// I now have this beauty 馃拝
interface StorageProvider {
  save(key: string, data: any): Promise<void>;
  get(key: string): Promise<any>;
}

class UserService {
  constructor(private readonly storage: StorageProvider) {}

  saveUser(user: User): Promise<void> {
    return this.storage.save('user', user);
  }
}

// we will be talking about abstractions later on, don't throw a rock at me right now

Swapping implementations became as simple as changing underwear (but less embarrassing when done in public).

Moral of the story: When you SOLID-ify your code, future you will send present you a thank-you cake. Or at least fewer angry Slack messages.

About the author

  • Software developer documenting my journey through web and mobile development. I share what I learn, build, and struggle with鈥攆rom React to SwiftUI, architecture decisions to deployment challenges. Not an expert, just passionate about coding and learning in public. Join me as I navigate the tech landscape one commit at a time.

No comments yet. Be the first to comment.

today i learned

Part 1 of 6

Daily code discoveries from my developer journey. Each post delivers concise, actionable insights I've uncovered while building with React, Typescript, and more. Brief but impactful lessons for curious developers looking to grow incrementally.

Up nextFixing the Mysterious 404 When Deploying Angular 19 to VercelTL;DR When deploying Angular 19 apps to Vercel, override the output directory setting to dist/[your-app-name]/browserto avoid the dreaded 404 page if you use the @angular-devkit/build-angular:application builder The Backstory There I was, feeling acc...

More from this blog