Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nestjs Dependency Injection and DDD / Clean Architecture

I'm experimenting with Nestjs by trying to implement a clean-architecture structure and I'd like to validate my solution because I'm not sure I understand the best way to do it. Please note that the example is almost pseudo-code and a lot of types are missing or generic because they're not the focus of the discussion.

Starting from my domain logic, I might want to implement it in a class like the following:

@Injectable()
export class ProfileDomainEntity {
  async addAge(profileId: string, age: number): Promise<void> {
    const profile = await this.profilesRepository.getOne(profileId)
    profile.age = age
    await this.profilesRepository.updateOne(profileId, profile)
  }
}

Here I need to get access to the profileRepository, but following the principles of the clean architecture, I don't want to be bothered with the implementation just now so I write an interface for it:

interface IProfilesRepository {
  getOne (profileId: string): object
  updateOne (profileId: string, profile: object): bool
}

Then I inject the dependency in the ProfileDomainEntity constructor and I make sure it's gonna follow the expected interface:

export class ProfileDomainEntity {
  constructor(
    private readonly profilesRepository: IProfilesRepository
  ){}

  async addAge(profileId: string, age: number): Promise<void> {
    const profile = await this.profilesRepository.getOne(profileId)
    profile.age = age

    await this.profilesRepository.updateOne(profileId, profile)
  }
}

And then I create a simple in memory implementation that let me run the code:

class ProfilesRepository implements IProfileRepository {
  private profiles = {}

  getOne(profileId: string) {
    return Promise.resolve(this.profiles[profileId])
  }

  updateOne(profileId: string, profile: object) {
    this.profiles[profileId] = profile
    return Promise.resolve(true)
  }
}

Now it's time to wiring everything together by using a module:

@Module({
  providers: [
    ProfileDomainEntity,
    ProfilesRepository
  ]
})
export class ProfilesModule {}

The problem here is that obviously ProfileRepository implements IProfilesRepository but it's not IProfilesRepository and therefore, as far as I understand, the token is different and Nest is not able to resolve the dependency.

The only solution that I've found to this is to user a custom provider to manually set the token:

@Module({
  providers: [
    ProfileDomainEntity,
    {
      provide: 'IProfilesRepository',
      useClass: ProfilesRepository
    }
  ]
})
export class ProfilesModule {}

And modify the ProfileDomainEntity by specifying the token to use with @Inject:

export class ProfileDomainEntity {
  constructor(
    @Inject('IProfilesRepository') private readonly profilesRepository: IProfilesRepository
  ){}
}

Is this a reasonable approach to use to deal wit all my dependencies or am I completely off-track? Is there any better solution? I'm new fairly new to all of these things (NestJs, clean architecture/DDD and Typescript as well) so I might be totally wrong here.

Thanks

like image 939
Alerosa Avatar asked Oct 24 '18 12:10

Alerosa


1 Answers

It is not possible to resolve dependency by the interface in NestJS due to the language limitations/features (see structural vs nominal typing).

And, if you are using an interface to define a (type of) dependency, then you have to use string tokens. But, you also can use class itself, or its name as a string literal, so you don't need to mention it during injection in, say, dependant's constructor.

Example:

// *** app.module.ts ***
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AppServiceMock } from './app.service.mock';

process.env.NODE_ENV = 'test'; // or 'development'

const appServiceProvider = {
  provide: AppService, // or string token 'AppService'
  useClass: process.env.NODE_ENV === 'test' ? AppServiceMock : AppService,
};

@Module({
  imports: [],
  controllers: [AppController],
  providers: [appServiceProvider],
})
export class AppModule {}

// *** app.controller.ts ***
import { Get, Controller } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  root(): string {
    return this.appService.root();
  }
}

You also can use an abstract class instead of an interface or give both interface and implementation class a similar name (and use aliases in-place).

Yes, comparing to C#/Java this might look like a dirty hack. Just keep in mind that interfaces are design-time only. In my example, AppServiceMock and AppService are not even inheriting from interface nor abstract/base class (in real world, they should, of course) and everything will work as long as they implement method root(): string.

Quote from the NestJS docs on this topic:

NOTICE

Instead of a custom token, we have used the ConfigService class, and therefore we have overridden the default implementation.

like image 168
amankkg Avatar answered Sep 25 '22 10:09

amankkg