0

I am creating a generic crud service that i want to use throughout my angular application in various feature modules.

To achieve this i need to pass a string value to a service in providers: of an angular module, so i can then set a value to the super of the base service. Is this possible?

Config

import { InjectionToken } from '@angular/core';

export const COLLECTION_REF = new InjectionToken<string>('collection');

Module

 import { GenericEntityService } from '@mifowi/core/services/genericEntity.service';
import { COLLECTION_REF } from './orgModuleConfig';
    import { NgModule } from '@angular/core';
    import { RouterModule } from '@angular/router';


@NgModule({
    ....
    **I want to pass in a string value of 'Organization' to the GenericEntityService**
    providers: [
    GenericEntityService,
    { provide: COLLECTION_REF, useValue: 'Organization' }
],
})
export class OrganizationsModule {}

Generic Service

 import { FirestoreService } from './firestore.service';
    import { Injectable } from '@angular/core';
    import {
        EntityCollectionServiceBase,
        EntityCollectionServiceElementsFactory
    } from '@ngrx/data';
    import { Observable } from 'rxjs';




     @Injectable()
        export class GenericEntityService extends EntityCollectionServiceBase<any> {
            constructor(
    @Inject(COLLECTION_REF) private collection: string,
                public firestoreService: FirestoreService,
                elementsFactory: EntityCollectionServiceElementsFactory
            ) {
                **I want to set the string below to

 the value passed in providers in the module - see above**
            super(collection, elementsFactory);
        }

        getMyEntityList(entity: string, id: string): Observable<any[]> {
            return this.firestoreService.getMyEntityList(entity, id);
        }

        getEntityByName(entity: string, str: string): Observable<any[]> {
            return this.firestoreService.colWithName$(entity, str);
        }

        createNewEntity(entity: string, payload: any): void {
            this.firestoreService.createDoc(entity, payload);
        }

        updateEntity(entity: string, payload: any): void {
            this.firestoreService.updateDoc(entity, payload);
        }

        deleteEntity(entity: string, payload: any): void {
            this.firestoreService.deleteDoc(entity, payload);
        }
    }
ccocker
  • 1,146
  • 5
  • 15
  • 39
  • Google about injection tokens, I think that's what you need. – dcg Jul 19 '19 at 15:07
  • @dcg yes tried that along with useValue, useFactory etc, just cannot seem to get it working – ccocker Jul 19 '19 at 15:25
  • I think you have some things mixed up, why are you trying to get your generic service to extend a specific module? I would imagine you would want to create a specific service or something that extends the generic. – rhavelka Jul 19 '19 at 15:43
  • @rhavelka - I suspect you are right but essentially i am using ngrx-data and i want a generic service that i can use througout the application. If i have to create a service for each feature module that does exactly the same thing then it defeats the purpose of having a generic crud service. I would basically create the same service 50 times for the 50 feature modules and the only difference would be the string reference to the firestore collection – ccocker Jul 19 '19 at 15:47

0 Answers0