Creating a class called User
raises the concern of having access to services within it, especially when using the constructor()
for dependency injection.
Below is an implementation of the User
class:
import { ElementRef, Injectable } from '@angular/core';
import { DbService } from '../service/db.service';
import { ImageService } from '../service/skin.service';
export class User {
name: string;
img: ElementRef<HTMLCanvasElement>;
constructor(
name: string,
img?: ElementRef<HTMLCanvasElement> | string[]) {
this.name = name;
if (img) {
if (img instanceof ElementRef) {
this.img = img;
} else {
this.img.nativeElement = this.ImageService.render(img);
}
} else {
DbService.getSkin(name).subscribe(res => this.img.nativeElement.this.service.render(res));
}
}
}
Rather than making required functions public static
, what is the best way to incorporate DbService and ImageService into the User
class?