Suppose I have a service which contains info about the logged user in my Angular application. I have a model called Sell
that contains a field which is the user id
who instantiates some Sell
object. Is there a way to inject (I don't know if "inject" is the best word here) the user service inside the model in such way when the constructor is called, Sell takes the user id automatically and assign it to object?
Example:
user.service.ts
...
@Injectable()
export class UserService {
private _id: string = 'some_id';
get id(): string {
return this._id;
}
}
sell.model.ts
export class Sell {
userId: string;
price: number;
...
constructor() {
// some way to have userService here
this.userId = this.userService.id;
}
}
some.component.ts
import { Component } from '@angular/core';
import { Sell } from '../models/sell.model';
@Component({
...
})
export class SomeComponent {
newSell() {
let sell = new Sell();
// with this line, I'd want that the model itself assign user id
// to its object.
console.log(sell.userId) // some_id
}
}
What you are trying to do is reasonable, the way you are trying to do it is considered a bad practice (big flame war back in the day so not going to get into that)
One of the better ways to do something like that is use Factories to construct your objects.
So your code would look like:
// Component needing model
@Component(...)
class SomeComponent {
constructor(sellFactory: SellFactoryService){
const sell = sellFactory.getNewSell();
console.log(sell.userId)
}
/// Sell factory
@Injectable()
class SellFactoryService {
constructor(private _userService: UserService){
}
getNewSell(){
const sell = new Sell();
sell.userId = this._userService.id;
return sell;
}
}
// Your sell class remains dumb (btw Sale would be a much better name for a model)
export class Sell {
userId: string;
price: number;
}
This way everything remains decoupled and testable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With