This inquiry arises within the framework of Enterprise Application development.
After exploring numerous books and online resources on Angular applications, a recurring pattern seems to be the practice of exporting classes (components, services, entities, etc.) in the type definition file and then directly importing them wherever needed. This is akin to using namespaces in C#, even if the classes belong to different angular modules.
For instance:
// In 'Commons/logger.service.ts'
export class LoggerService { ... }
// In 'Core/common.service.ts'
export class CommonService { ... }
// Inside 'Products/' module
import { LoggerService } from '../Commons/logger.service'
import { CommonService } from '../Core/common.service'
export class ProductComponent { ... }
However, upon embarking on a large enterprise project, I encountered an unfamiliar approach where files were created to organize each type of class (such as service, entity, method parameter, component), with each file exporting its contents. These files were then exported in their respective angular module file, and rather than importing types directly from their individual files, imports were done from the designated module.
The above example would be transformed into something like this:
// In 'Commons/logger.service.ts'
export class LoggerService { ... }
// In 'Commons/services.ts'
export * from './logger.service.ts'
export * from './other.service.ts'
export * from './another.service.ts'
// In 'Commons/commons.module.ts'
export * from 'services.ts'
export * from 'entities.ts'
/* ... */
// In 'Products/' module
import { LoggerService, OtherService } from '../Commons/commons.module'
export class ProductComponent { ... }
Given that this alternative approach is more verbose and can lead to awkward results (such as cyclic reference warnings when importing classes within the same module), my questions are:
- Which approach is advisable from a standpoint of good design or best practices?
- Is this new approach preferable over the previous one? If so, why and in what scenarios?
- Why hasn't this approach been highlighted in major documentation sources such as Angular online docs or books?
- What are the advantages and disadvantages of adopting this alternate approach?