DEV Community

serjo agro
serjo agro

Posted on

4 2

How to inject services in AdonisJs V5 - Constructor, Method

In this post i will give you 2 examples for injection of services to adonisjs v5 constructors, and methods

create new provider: MyServiceProvider.ts

import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import {MyServiceImp} from "Providers/MyServiceImp";

export default class MyServiceProvider {

  public static needsApplication = true

  constructor (protected application: ApplicationContract) {
     console.log('i was called');
  }

  public register () {
     this.application.container.bind('@ioc:MyNameSpace/MyService', () => {
        console.log('@ioc:MyNameSpace/MyService');
        return new MyServiceImp('http');
     });

     this.application.container.bind('MyNameSpace/MyService', () => {
        console.log('MyNameSpace/MyService');
        return new MyServiceImp('https');
     });
  }

  public async boot () {
    // All bindings are ready, feel free to use them
     console.log('ready');

  }

  public async ready () {
    // App is ready
  }

  public async shutdown () {
    // Cleanup, since app is going down
  }
}


Enter fullscreen mode Exit fullscreen mode

create new interface: MyServiceContract.ts

declare module '@ioc:MyNameSpace/MyService' {

   export interface MyServiceContract {
      protocol?: string;
   }
}

Enter fullscreen mode Exit fullscreen mode

create new interface implementation: MyServiceImp.ts

import {MyServiceContract} from "@ioc:MyNameSpace/MyService";

export class MyServiceImp implements MyServiceContract {
   protocol: string;

   constructor(protocol: string) {
      this.protocol = protocol;
   }
}


Enter fullscreen mode Exit fullscreen mode

add to file adonisrc.json in providers array:

"./providers/MyServiceProvider"
Enter fullscreen mode Exit fullscreen mode

usage:

iam giving here 2 namespacing examples, you can choose how ever you prefer

add to controller/service - what ever you need


import {inject} from "@adonisjs/fold";
import {MyServiceContract} from "@ioc:MyNameSpace/MyService";



// constructor injection, pay attention to array indexes
// this will log from service provider register method
// console.log('@ioc:MyNameSpace/MyService'); - for example


@inject(['@ioc:MyNameSpace/MyService', 'MyNameSpace/MyService'])
export default class MyController {
    constructor(protected service1: MyServiceContract, protected service2: MyServiceContract) {
      console.log(service1, service2);
   }


// option 2 - method injection
// same here, 2 example of getting service from ioc by namespace

@inject([null, '@ioc:MyNameSpace/MyService', 'MyNameSpace/MyService'])
   public async index({inertia, request}: HttpContextContract, myService: MyServiceContract, myService2: MyServiceContract) {
      console.log(myService, myService2);

   }

}
Enter fullscreen mode Exit fullscreen mode

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (5)

Collapse
 
sergefabre profile image
Serge_F

Hello
And How Inject Service in other Service please ?

Collapse
 
serjoagronovdev profile image
serjo agro • Edited

not possible from what i saw, i tried multiple ways

EDIT:
correction, it can be done,

inside your service implementation of the second service:

// @ts-ignore
import {MyServiceContract} from "Providers/MyServiceContract";

inside second service provider register method:

public register() {

  this.myService = this.application.container.resolveBinding('MyNameSpace/MyService')
  console.log(this.myService);

  this.application.container.bind('MyNameSpace/MyService2', () => {
     return new MyServiceImp2('https', this.myService);
  });
Enter fullscreen mode Exit fullscreen mode

}

Collapse
 
jmas profile image
Alexander Maslakov
Collapse
 
jamols09 profile image
jamols09

By Service you mean the business logic class right ? Just like in Laravel where there's a service / repository pattern.

Collapse
 
serjoagronovdev profile image
serjo agro

exactly like in laravel in my opinion

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay