Volver al ranking

bespoyasov/frontend-clean-architecture

TypeScriptbespoyasov.me/blog/clean-architecture-on-frontend/

React + TypeScript app built using the clean architecture principles in a more functional way.

clean-architectureonion-architecturedomainapplicationadapterslayered-architecturefunctional
Crecimiento de estrellas
Estrellas
2.6k
Forks
290
Crecimiento semanal
Issues
2
1k2k
jul 2021mar 2023nov 2024jul 2026
Artefactosnpmnpm install frontend-clean-architecture
README

Other languages: Russian.

Frontend Clean Architecture

A React + TypeScript example app built using the clean architecture in a functional(-ish) way.

Things to Consider

There are a few compromises and simplifications in the code that are worth to be mentioned.

Shared Kernel

Shared Kernel is the code and data on which any modules can depend, but only if this dependency would not increase coupling. More details about the limitations and application are well described in the article "DDD, Hexagonal, Onion, Clean, CQRS, ... How I put it all together".

In this application, the shared kernel includes global type annotations that can be accessed anywhere in the app and by any module. Such types are collected in shared-kernel.d.ts.

Dependency in the Domain

The createOrder function uses the library-like function currentDatetime to specify the order creation date. This is not quite correct, because the domain should not depend on anything.

Ideally, the implementation of the Order type should accept all the necessary data, including the date, from outside. The creation of this entity would be in the application layer in orderProducts:

async function orderProducts(user: User, { products }: Cart) {
  const datetime = currentDatetime();
  const order = new Order(user, products, datetime);

  // ...
}

Use Case Testability

The order creation function orderProduct itself is framework-dependent right now and cannot be used or tested in isolation outside of React. The hook wrapper though is only used to provide the use case to components and to inject services into the use case itself.

In a canonical implementation, the function of the use case would be extracted outside the hook, and the services would be passed to the use case via a last argument or a DI:

type Dependencies = {
  notifier?: NotificationService;
  payment?: PaymentService;
  orderStorage?: OrderStorageService;
};

async function orderProducts(
  user: User,
  cart: Cart,
  dependencies: Dependencies = defaultDependencies
) {
  const { notifier, payment, orderStorage } = dependencies;

  // ...
}

Hook would then become an adapter:

function useOrderProducts() {
  const notifier = useNotifier();
  const payment = usePayment();
  const orderStorage = useOrdersStorage();

  return (user: User, cart: Cart) =>
    orderProducts(user, cart, {
      notifier,
      payment,
      orderStorage,
    });
}

In the sources, I thought it was unnecessary, as it would distract from the essence.

Crooked DI

In the application layer we inject services by hand:

export function useAuthenticate() {
  const storage: UserStorageService = useUserStorage();
  const auth: AuthenticationService = useAuth();

  // ...
}

In a good way, this should be automated and done through the dependency injection. But in the case of React and hooks, we can use them as a “container” that returns an implementation of the specified interface.

In this particular application, it didn't make much sense to set up the DI because it would distract from the main topic.

Repositorios relacionados
ryanmcdermott/clean-code-javascript

Clean Code concepts adapted for JavaScript

JavaScriptnpmMIT Licensejavascriptprinciples
94.7k12.5k
jasontaylordev/CleanArchitecture

Clean Architecture Solution Template for ASP.NET Core

C#MIT Licenseclean-architecturetemplate
cleanarchitecture.jasontaylor.dev
20.3k4.2k
ardalis/CleanArchitecture

Clean Architecture Solution Template: A proven Clean Architecture Template for ASP.NET Core 10

C#MIT Licenseclean-architecturearchitecture
18.3k3.1k
Sairyss/domain-driven-hexagon

Learn Domain-Driven Design, software architecture, design patterns, best practices. Code examples included

TypeScriptnpmMIT Licensedomain-driven-designhexagonal-architecture
14.8k1.6k
kgrzybek/modular-monolith-with-ddd

Full Modular Monolith application with Domain-Driven Design approach.

C#MIT Licensedddddd-architecture
13.9k2.2k
mehdihadeli/awesome-software-architecture

📚 A curated list of awesome articles, videos, and other resources to learn and practice software architecture, patterns, and principles.

Creative Commons Zero v1.0 Universalarchitectureddd
awesome-architecture.com
11.4k992
dotnet-architecture/eShopOnWeb

Sample ASP.NET Core 8.0 reference application, now community supported: https://github.com/NimblePros/eShopOnWeb

C#MIT Licenseasp-net-coreasp-net-core-mvc
10.7k6k
bxcodec/go-clean-arch

Go (Golang) Clean Architecture based on Reading Uncle Bob's Clean Architecture

GoGo ModulesMIT Licensearchitecturegolang
10.1k1.3k
labs42io/clean-code-typescript

Clean Code concepts adapted for TypeScript

TypeScriptnpmMIT Licenseclean-codetypescript
labs42io.github.io/clean-code-typescript
9.8k1.2k
thangchung/clean-code-dotnet

:bathtub: Clean Code concepts and tools adapted for .NET

C#MIT Licenseawesomeaspnet
7.7k1.1k
evrone/go-clean-template

Clean Architecture template for Golang services

GoGo ModulesMIT Licenseclean-architecturego
7.6k658
igorwojda/android-showcase

💎 Android application following best practices: Kotlin, Coroutines, JetPack, Clean Architecture, Feature Modules, Tests, MVVM, DI, Static Analysis...

KotlinMIT Licensekotlinandroid
6.8k908