Integrating backend services 

We are making really good progress here, so let's keep rollingOne of the best practices in software development is to create code that is reusable, generic, and maintainable. In most applications that are dynamic in nature, we need to make a lot of HTTP calls to create, save, retrieve, edit, or delete data, as per the functional requirements of the application. If we do not have commonly shared HTTP calls, we may end up with a lot of methods having HTTP implementations, and it will be very difficult to maintain them in the long run. How do we address this situation? You already know the answer, my friend. That's right—by using services. In Chapter 11, Dependency Injection and Services, we learned all about Angular services and best practices regarding dependency injection.

Angular guidelines clearly state that all HTTP calls and functionality should be kept in services, which makes it easy to reuse existing code. Angular services are shared functions that allow us to access the properties and methods defined inside it. We will also create our custom services, in which we will implement our HTTP calls and which can be easily reused in various components. Let's create two services—one for working with JSON server APIs, and one for Firestore database operations. For working with JSON server APIs, we will call our DbOperationsService service, and for working with the Firestore database, we will call our CRUDService service. Each of these services will have the methods to make HTTP calls for creating, reading, updating, and deleting the data. Now, let's run the following ng command, which will generate our services:

ng generate service db-operations

Upon successful execution of the preceding command, we will execute the following command to generate another service. Let's call it crud. We will use the following ng command to generate the service. 

ng generate service crud

Following a successful run, we should see the service files being generated, along with their respective spec files. So far, so good. We will need these services when we start the end-to-end integration work. It may look complicated, but trust me, all of it will make a lot of sense in the sections to follow.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.156.250