0
\$\begingroup\$

I created online shop with angular 7 and I am not sure is good architecture. I created 5 modules:

  • main module
  • products
  • shop-cart
  • core
  • admin

I have service :

  • admin.service.ts
  • products.service.ts
  • data-storage-service.ts
  • message-data.service.ts
  • shop-cart.service.ts

I have module for routing:

  • app-routing.module.ts
  • products.service.ts

In main module I include all others module:

@NgModule({
  declarations: [
    AppComponent


  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    CoreModule,
    AdminModule,
    SharedModule,
    ShopCartModule,
    ProductsModule,
    HttpClientModule,
    FormsModule, 
    ModalModule.forRoot(),

    HttpClientInMemoryWebApiModule.forRoot(
      InMemoryDataService, { dataEncapsulation: false }
    )

  ],
  providers: [,
    InMemoryDataService],
  bootstrap: [AppComponent]
})
export class AppModule { }

This is my core module where I include all service:

@NgModule({


  imports: [
    CommonModule,
    AppRoutingModule
  ],
  exports: [
    AppRoutingModule,
    HeaderComponent,
    FooterComponent

  ],
  declarations: [
    HeaderComponent,
    HomeComponent,
    FooterComponent
  ],
  providers:[
    DataStorageServiceService,
    ProductsService,
    ShopCartService,
    AdminSerivice,
    MessageService
  ]
})
export class CoreModule { }

I using data-storage-service.ts for communication with server. In my admin.service.ts and products.service.ts I have methods for calling method form data-storage-service.ts and store data in list. Example: data-storage-service.ts have method :

 /** GET Category from the server */
  getCategory(): Observable<CategoryModel[]> {
    return this.http.get<CategoryModel[]>(this.cateogryUrl).pipe(

      catchError(this.handleError('getProduct', []))
    )
  }

admin.service.ts have method:

getCategoryFromServer() {
        this.dataStorageServiceServiceta.getCategory().subscribe((category: CategoryModel[]) => {
            this.categoryList = category;
            this.cateogoryChanged.next(this.categoryList.slice())
        })
    }

And in componentadmin.componenet.ts in ngOnInit I have

 ngOnInit() {
    this.admin.getCagegoryFromServer();
  }

Are is this correct enter image description here?

\$\endgroup\$
0
\$\begingroup\$

It seems to me more logical that the declaration of all services is done in your "main" rather than in "core". It is better to separate as much as possible the dependencies between modules of the same level.

In addition, the module "core" and "shared" are quite close in philosophy, they allow to share basic code in your project. I think that in the future it will be confusing.

Maybe renamed core to "home" or just a part of your application and just use "shared" to share modules.

Then, personally I will pass the definition of routes in the module file.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.