I think I am just look for a bit of code review advice. It might possibly be a methodology question?
Essentially when I am pulling data (usually from a REST request), I generate a service, then inject HTTPclient of Angular common so I can use a simple GET to grab the data. Like below:
interface ReportContact{
Email: string;
Name: string;
ReportName:string;
ReportType:string;
ReportFunction:string;
}
@Injectable({
providedIn: 'root'
})
export class ReportingContactService {
private readonly url = environment.url;
private _http:HttpClient;
constructor(http:HttpClient) {
//DI
this._http=http;
}
getReportContacts():Observable<ReportContact[]>{
return this._http.get<ReportContact[]>(this.url);
}
}
This is generally my go to when I am making a simple service. I then usually use this service in my component like below:
export class ReportingContactComponent implements OnInit {
_service:ReportingContactService;
data:ReportingContact[];
constructor(service:ReportingContactService) {
//DI service
this._service = service;
}
ngOnInit() {
this._service.getReportContacts().subscribe(
data=> {
for(var element of data){
}
});
}
}
Now, where I was hoping for some review and pointers on is, should I now be mapping the return of that service to a class that I create as a model? I am used to the mindset of separating data type objects from the objects used in the rest of the application. So essentially in that for loop (of the component.ts), I would map to these new objects which look like below:
export class ReportingContact{
Email: string;
Name: string;
ReportName:string;
Type:string;
Function:string;
}
Am I wasting my time and is this generally not the "norm"? Again, I just have always come from the mindset of separating, but the Angular framework (or maybe just TS and JS) has introduced new ways of thinking for me, so I like to be keeping to the generally accepted standards.
number
/string
that you wanted to convert to aDate
, or you wanted accessors/methods on the domain object), then this should probably be done in the service.