A more detailed example of architecture in angular


To illustrate a more detailed example of applying Uncle Bob's Clean Architecture to an Angular application, let's consider a simple user management system with functionalities for user registration, login, and fetching user profiles. This example will demonstrate how to structure the application into distinct layers, adhering to Clean Architecture principles.

Project Structure Overview

The project will be structured into four main directories:

  • src/domain: Contains the business logic and entities.
  • src/application: Contains use cases and interfaces for repositories.
  • src/infrastructure: Contains implementations of repositories and external services.
  • src/presentation: Contains Angular components, services, and modules.

Step-by-Step Implementation

1. Setting Up the Domain Layer

First, define the entities within the domain directory. For instance, a User entity might look like this:

// src/domain/entities/user.entity.ts export class User { id: string; username: string; password: string; email: string; }

2. Defining Use Cases in the Application Layer

Next, define use cases in the application directory. These use cases encapsulate the business rules. For example, a RegisterUser use case might be defined as follows:

// src/application/use-cases/register-user.use-case.ts import { User } from '../../domain/entities/user.entity'; export class RegisterUserUseCase { constructor(private userRepository: UserRepository) {} execute(user: User): Promise<void> { return this.userRepository.register(user); } }

3. Implementing Repositories in the Infrastructure Layer

Implement the UserRepository interface in the infrastructure directory. This interface defines methods for interacting with users, such as registering a new user:

// src/infrastructure/repositories/user.repository.interface.ts export interface UserRepository { register(user: User): Promise<void>; }

And here's a basic implementation using a hypothetical user service:

// src/infrastructure/repositories/user.repository.implementation.ts import { UserRepository } from './user.repository.interface'; import { User } from '../../../domain/entities/user.entity'; export class UserRepositoryImplementation implements UserRepository { async register(user: User): Promise<void> { // Implementation details, e.g., calling an API or database } }

4. Creating Services and Components in the Presentation Layer

Finally, in the presentation layer, create Angular services and components to interact with the use cases. For example, a registration service might look like this:

// src/presentation/services/register.service.ts import { Injectable } from '@angular/core'; import { RegisterUserUseCase } from '../application/use-cases/register-user.use-case'; import { User } from '../../domain/entities/user.entity'; @Injectable({ providedIn: 'root', }) export class RegisterService { constructor(private registerUserUseCase: RegisterUserUseCase) {} async registerUser(user: User): Promise<void> { await this.registerUserUseCase.execute(user); } }

And a corresponding component for the registration form:

// src/presentation/components/register/register.component.ts import { Component } from '@angular/core'; import { RegisterService } from '../services/register.service'; import { User } from '../../domain/entities/user.entity'; @Component({ selector: 'app-register', templateUrl: './register.component.html', styleUrls: ['./register.component.css'], }) export class RegisterComponent { constructor(private registerService: RegisterService) {} async onSubmit(username: string, password: string, email: string): Promise<void> { const user = new User(); user.username = username; user.password = password; user.email = email; await this.registerService.registerUser(user); } }

Conclusion

This example demonstrates how to structure an Angular application according to Clean Architecture principles. By separating concerns into distinct layers—domain, application, infrastructure, and presentation—you can achieve a maintainable, scalable, and testable application. Remember, the key to Clean Architecture is the separation of concerns, allowing for high cohesion within layers and low coupling between them.