DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

The Latest Frameworks Topics

article thumbnail
Defining Implementations of Services: Dependency Injection in Angular
I was working on a practical project using Angular 8, where I was creating a service and thinking more about how to implement SOLID principles. I decided to have an interface to define my service and a class to implement the logic. Everything was okay with that design, but a question came to my mind, "How can I implement dependency injection with my Angular component?" In this example, I am going to use an abstract class, Let's create an abstract class, named IGreetingsService. This will be used as an Interface with a greeting method. TypeScript x 1 export abstract class IGreetingsService { 2 constructor() { } 3 abstract greeting(): String; 4 } Now, let's create a class, GreetingsServiceImpl. This class implements the IGreetingsService abstract class. TypeScript xxxxxxxxxx 1 1 export class GreetingsServiceImpl implements IGreetingsService { 2 constructor() { } 3 greeting(): String{ 4 return "Pruebaa"; 5 }; 6 } At this point, we have a regular class to use as a service, but we need to add the annotation @Injectable. We have to put it at IGreetingsService. TypeScript x 10 1 @Injectable({ 2 providedIn: 'root', 3 useClass: GreetingsServiceImpl, 4 }) 5 export abstract class IGreetingsService { 6 7 constructor() { } 8 9 abstract greeting(): String; 10 } The magic is at the property useClass. This class is used by the Angular framework to know what concrete class has to use for dependency injection when you are using theIGreetingsService abstract class. So we are ready to use our service into an angular component. TypeScript xxxxxxxxxx 1 12 1 @Component({ 2 selector: 'my-app', 3 templateUrl: './app.component.html', 4 styleUrls: [ './app.component.css' ] 5 }) 6 export class AppComponent { 7 name: String; 8 constructor(private service: IGreetingsService ) { 9 this.name = this.service.greeting(); 10 } 11 12 } At the moment, to create an instance of the AppComponent, Angular automatically creates an instance of GreetingsServiceImpl , instead of IGreetingsService. (Remember IGreetingsService is an abstract class). This approach can help you in several situations; for example, think about a service using REST services to get some data, but now there is a new requirement, and you need to get the same data from session storage or local storage. You can create a new class implementing your abstract class and update the property useClass to refer to your new class. Another situation would be if you needed a different implementation on production environments and testing environments. You can find the complete source code here: https://stackblitz.com/edit/angular8-injectiondependency-interface. A final note, you can define a provider in the component definition annotation to inject a specific implementation of your service. TypeScript x 1 @Component({ 2 selector: 'my-app', 3 templateUrl: './app.component.html', 4 styleUrls: [ './app.component.css' ], 5 providers :[{ provide: IGreetingsService, useClass: GreetingsServiceImpl }] 6 }) Further Reading Angular: Everything You Need to Know [Tutorials]. About Dependency Injection.
March 9, 2020
by Javier Santos
· 9,472 Views · 4 Likes
article thumbnail
Spring Security — Chapter 1
In this article, see how to get started using Spring Security to manage authentication and authorization in a Java application.
March 5, 2020
by Vinu Sagar
· 17,607 Views · 20 Likes
article thumbnail
10 Famous Apps Using React.js
See ten famous applications that are using React.js nowadays, including Facebook, WhatsApp, and more.
March 4, 2020
by Manish Patel
· 22,374 Views · 4 Likes
article thumbnail
Remove a BOM Character From an Apache Camel Exchange Message (DSL Java)
In this article, we provide a brief tutorial on how to remove a BOM character from an Apache Camel exchange message with DSL Java.
Updated March 4, 2020
by Anna Star
· 16,522 Views · 5 Likes
article thumbnail
Geospatial Data Analysis in Angular
This article will explain the method to create an interactive geospatial data visualization with Angular 8, MapboxGL and DeckGL.
March 2, 2020
by Imaginea Technologies
· 8,530 Views · 3 Likes
article thumbnail
Testing Asynchronous Operations in Spring With JUnit 5 and Byteman
In this article, we will be discussing how to test operations in an application that uses a Spring context (with asynchronous operations enabled).
February 28, 2020
by Szymon Tarnowski DZone Core CORE
· 39,512 Views · 7 Likes
article thumbnail
Build a REST Service That Consumes Any Type of Request Format
In this article we discuss how to build a REST service that can consume any type of request format, including XML and JSON.
February 27, 2020
by Santosh Devkate
· 34,115 Views · 9 Likes
article thumbnail
Upload and Retrieve Files/Images Using Spring Boot, Angular, and MySQL
This article demonstrates how you can select an image using the Angular 8 UI, send it to the Spring backend, and retrieve it later from MySQL.
February 26, 2020
by Rida Shaikh
· 128,850 Views · 6 Likes
article thumbnail
Handling Flutter Webview Back-Button
In this article, we discuss how to handle back-button operations with Flutter and webview in order to allow users to navigate between pages.
Updated February 19, 2020
by Omer Yilmaz
· 29,981 Views · 4 Likes
article thumbnail
Data Binding and Server-Side Rendering in Angular and React
In this article, we discuss how data binding and server-side rendering differs in both Angular and React.
February 19, 2020
by Hiren Dhaduk
· 9,997 Views · 6 Likes
article thumbnail
Incorporating Fault-Tolerance Into Your Microservice Architecture
In this article, we discuss the process of incorporating fault-tolerance into a microservice architecture.
February 19, 2020
by Oresztesz Margaritisz DZone Core CORE
· 10,541 Views · 13 Likes
article thumbnail
Building Mancala Game in Microservices Using Spring Boot (Part 3: Web Client Microservice Implementation Using Vaadin)
Find out how!
Updated February 19, 2020
by Esfandiyar Talebi
· 14,898 Views · 3 Likes
article thumbnail
Building Mancala Game in Microservices Using Spring Boot (Part 2: Mancala API Implementation)
The next step!
Updated February 19, 2020
by Esfandiyar Talebi
· 17,395 Views · 9 Likes
article thumbnail
Include With Where Clause
In this article, take a look at a tutorial that explains how to write a certain query in Entity Framework.
February 18, 2020
by Muhammad Adil Malik
· 24,856 Views · 3 Likes
article thumbnail
Securing Spring Boot Microservices with JSON Web Tokens (JWT)
This article demonstrates how JWTs can be used for securing access to Java microservices built with Spring Boot.
Updated February 17, 2020
by Gopinath Parimi
· 49,479 Views · 4 Likes
article thumbnail
Angular Image Gallery With Bootstrap
Create a quick Flickr image gallery from scratch with images of varying dimensions using Angular and Bootstrap.
February 14, 2020
by Jyoti Barik
· 31,707 Views · 2 Likes
article thumbnail
Create Fast and Easy Docker Images With Jib
In this article, create Docker images with Jib.
February 14, 2020
by Gunter Rotsaert DZone Core CORE
· 23,156 Views · 6 Likes
article thumbnail
Testing Asynchronous Operations in Spring With Spock and Byteman
In this article, we discuss how to test asynchronous operations in Spring applications with Spock and Byteman.
February 13, 2020
by Szymon Tarnowski DZone Core CORE
· 13,300 Views · 5 Likes
article thumbnail
13 of the Best React JavaScript Frameworks
The following are 13 of the best React JavaScript frameworks; all are open source. React Native is used for building native applications.
February 12, 2020
by Amit Dua
· 15,340 Views · 11 Likes
article thumbnail
How to Publish Docker Images on a Private Nexus Repository Using Jib Maven Plugin
Take a look at how to create a Nexus repository manager using HTTP and how to set up a Docker repository to publish Docker images using the jib plugin.
February 11, 2020
by Awkash Agrawal
· 32,871 Views · 8 Likes
  • Previous
  • ...
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • ...
  • Next
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×