For example, this is book.service.ts
import { Injectable } from "@nestjs/common";
import { Book } from '../interfaces/book.interface'
@Injectable()
export class BooksService {
private readonly books: Book[] = [];
private create(book: Book) {
console.log(book)
this.books.push(book);
}
private findAll(): Book[] {
return this.books;
}
}
Another buy.service.ts
import { Injectable } from "@nestjs/common";
import { Book } from '../interfaces/book.interface'
@Injectable()
export class BuyService {
private readonly books: Book[] = [];
private findAll(): Book[] {
return this.books;
}
}
The private findAll() methods are the same in the two files. How to create a common logic for them?
>Solution :
If I were you, I’d inject the BookService into the BuyService and not make the findAll() method private. This way you can call this.bookService.findAll() from within the BuyService. Otherwise, you’ll need to abstract up another layer and inject that abstraction into both BookService and BuyService.
Sounds like overall tightly coupling code, so be cautious you don’t end up with crossed domain lines and tightly coupled dependencies