Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 (ng2) - Service vs util, when to use which?

Tags:

angular

After looking at many different forum posts and example projects, people seem to choose between a service or a util for the same type of functionality. What I have read so far is people saying that Services are better because of dependency injection which will make testing easier. Others say utils are better because they are faster. I was wondering if there is any type of preferred rule on when to use a service vs a util and why?

Here is an example that is extracting table columns from loaded data:

Service

export class TableService {
  getColumns(data:Array<any>):Array<GridColumnInterface> {
    let columns:Array<GridColumnInterface> = [];

    data.forEach((column:any, index:number) => {
      columns.push({
        headerName: column.name,
        field: String(index),
        hide: column.isHidden,
        class: 'text-center'
      });
    });

    return columns;
  }
}

Util

export class TableUtil {
  static getColumns(data:Array<any>):Array<GridColumnInterface> {
    let columns:Array<GridColumnInterface> = [];

    data.forEach((column:any, index:number) => {
      columns.push({
        headerName: column.name,
        field: String(index),
        hide: column.isHidden,
        class: 'text-center'
      });
    });

    return columns;
  }
}

So which would you choose and why?

like image 269
Marin Petkov Avatar asked Oct 17 '16 18:10

Marin Petkov


People also ask

What are the types of services in Angular?

There are two types of services in angular: Built-in services – There are approximately 30 built-in services in angular. Custom services – In angular if the user wants to create its own service he/she can do so.

Why services are used in Angular?

The main objective of a service is to organize and share business logic, models, or data and functions with different components of an Angular application. They are usually implemented through dependency injection.

Which of the below file will be available when we create a service in Angular application?

The ngOnInit function gets called by default in any component created. The date is fetched from the service as shown above. To fetch more details of the service, we need to first include the service in the component ts file.

Can a service call another service Angular?

Angular Service that needs another service You don't have to do much to configure dependency between services. A service that needs another service should have a @Injectable decorator and then you can apply the same constructor injection pattern where you provide the service dependency with in the constructor.


1 Answers

If you care about maintainability and testability use a service.

If you run into situations where profiling reveals that using the service takes up a notable amount of CPU resources, then switch to an util-class and do benchmarks to get proper numbers how much faster your app runs with util-classes. If this makes a difference in your application (highly unlikely btw) then change that service class to an util-class. I'd consider everything else premature optimization.

I'm confident that, when you are able to reduce the number of requests your app makes to your server, it will contribute more to the user experience of your application then you will ever be able to make with service vs util.

Angular2 AoT converts DI code to static code anyway, therefore there is not much potential for performance improvement.

like image 131
Günter Zöchbauer Avatar answered Sep 18 '22 09:09

Günter Zöchbauer