Currently, I am immersed in a complex Angular 2 endeavor that requires fetching various types of objects from a noSQL database. These objects are represented using straightforward model classes, and I have set up services to retrieve the data from the DB and create instances of these objects. This process is similar to the methodology demonstrated in the Heroes tutorial.
However, I have encountered an issue where each type of object necessitates its own service with slight variations. The primary discrepancy lies in the fact that I need the service to instantiate and return different object types. To streamline these services, I attempted to pass the object type from the component to the service, allowing the service to make the appropriate DB call and construct the specified object type.
This setup appears as follows:
Within the component:
import { Market } from './market'; // Market represents a basic model class
/* additional code */
ngOnInit(){
this.route.params.forEach(params => {
this.nodeID = params['id'];
this.nodeService.getNodeOfType(Market, this.nodeID)
.subscribe(market => this.market = market)
});
}
Service:
export class NodeService {
getNodeOfType(type, nodeID: number) {
return this.neo4j.readProperties(nodeID)
.map(properties => new type(nodeID, properties));
}
}
I have two inquiries:
How should I specify the class type when passing it as a parameter in the function?
Given my limited experience with TypeScript, and the absence of similar examples in the documentation, I suspect this approach might be considered an anti-pattern. Could this design practice potentially lead to complications down the road?