Home > Back-end >  Shared service between two projects - need to pull in a property that comes from different dependenc
Shared service between two projects - need to pull in a property that comes from different dependenc

Time:01-12

I have three projects, let's call them Project A, Project B, and Common.

Common, as you might've guessed, is a project containing files used in both projects.

There is a component, previously in Project A, that will now also be used in Project B, so I've moved it to Common. This component relies on a service, so it makes sense to me to also move the service to Common (service is a singleton). However, the service makes HTTP calls and there is a specific ID that is needed for the HTTP calls (it's part of the URLs).

The specific ID that is needed for the service's HTTP call is available in both Project A and in Project B, but they come from different sources, and it wouldn't be appropriate to move these sources to Common. So, I'm trying to figure out a way to perhaps conditionally pull the ID from either source, within the service, depending on which project is using the component, but I'm not sure what would be the best way to do this.

Something I considered was using a method to set the URL in the service, passing in what project is using the service, and then having it pull in the proper service from either Project A or Project B. But I'm not sure this is a great solution. Here's the pseudocode:

setBaseUrl(project) {
  if (!this.baseURL) {
    if (project === 'ProjectA') {
      this.baseUrl  = injector.get(projectAService).id;
    } else {
      this.baseUrl  = injector.get(projectBService).id;
    }
  }
}

Any help would be much appreciated. Perhaps this is something that a wrapper class/inheritance could effectively solve, but right now I'm stumped.

CodePudding user response:

You are looking for Bridge pattern.

In other words, Depend on abstractions, not on concretions.

So basically your code should look like below:

setBaseUrl(project) {
  if (!this.baseURL) {
      this.baseUrl  = this.projectService.id
    } 
  }
}

Your ProjectA and ProjectB component dependency injection should look like this:

constructor(@Inject(PROJECT_SERVICE) projectService: IProjectService) {}

Steps to do:

In common folder, create file like tokens.ts and create token

export const PROJECT_SERVICE = new InjectionToken('PROJECT_SERVICE );

Then in ProjectA module:

 providers: [
  { provide: PROJECT_SERVICE, useClass: ProjectAService }
]

And

Then in ProjectB module:

   providers: [
      { provide: PROJECT_SERVICE, useClass: ProjectBService }
    ]

Also ProjectA and ProjectB implements IProjectService

export class ProjectAService implements IProjectService {}
export class ProjectBService implements IProjectService {}

Here we go, enjoy!

CodePudding user response:

The best way to achieve your goal is to create a provider that provides the specific ID and inject it into your service. In this way, you can provide different IDs for each project.

Common

export const SpecificID = new InjectionToken<string>('SPECIFIC_ID');


export class YourCommonService {

  constructor(
    private http: HttpClient,
    @Inject(SpecificID) private specificID: string, // Here you get the specific ID
  ) { }

}

Project A app.module.ts

@NgModule({
  // ...
  providers: [
    { provide: SpecificID, useValue: 'ProjA_Key' } // Here you provide the ID that belongs to the specific project
  ],
})
export class AppModule { }
  •  Tags:  
  • Related