Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do you think automatic registration could be implemented? #6

Open
chuano opened this issue Apr 27, 2022 · 2 comments
Open

Do you think automatic registration could be implemented? #6

chuano opened this issue Apr 27, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@chuano
Copy link

chuano commented Apr 27, 2022

It would be great to be able to automatically register all services that have the decorator.

As far as I know, it is not possible to do this because the meta-data is not generated without importing the classes, but maybe there is a way and I don't know it.

If you know any way in which we can approach this, I would like to collaborate with it. Just give me some clue and I'll start investigating.

Thanks.

@artberri artberri added the enhancement New feature or request label Jun 9, 2022
@artberri
Copy link
Owner

artberri commented Jun 9, 2022

First of all, sorry for being so late answering you @chuano.

I'm not 100% sure about what are you asking for. I guess that you would like to be able to use DIOD's container without the need of registering every service. For example, instead of doing this:

const builder = new ContainerBuilder()
builder.register(Mailer).use(AcmeMailer)
builder.register(UserRepository).use(SqliteUserRepository)
builder.registerAndUse(SignUpUseCase) 
const container = builder.build()

const signUpUseCase = container.get(SignUpUseCase)

To just do this:

const builder = new ContainerBuilder()
const container = builder.build()

const signUpUseCase = container.get(SignUpUseCase)

Am I right? I think that this would be impossible, because in that example I am using abstractions and not only class implementations. But it could exist the possibility to automagically register all the services if they all are class implementations.

I mean, instead of doing this:

const builder = new ContainerBuilder()
builder.registerAndUse(AcmeMailer)
builder.registerAndUse(SqliteUserRepository)
builder.registerAndUse(SignUpUseCase) 
const container = builder.build()

const signUpUseCase = container.get(SignUpUseCase)

To just do this, I think it could be possible:

const builder = new ContainerBuilder()
const container = builder.build()

const signUpUseCase = container.get(SignUpUseCase)

As you said importing all the classes is necessary, but in this example it is supposed that the SignUpUseCase will be something like this:

import { Service } from 'diod'
import { SqliteUserRepository } from './SqliteUserRepository'
import { AcmeMailer } from './AcmeMailer'

@Service()
export class SignUpUseCase {
  constructor(
    private readonly userRepository: SqliteUserRepository,
    private readonly mailer: AcmeMailer
  ) {}

  execute(userData: UserDto): void {
    const user = this.userRepository.create(userData)
    this.mailer.sendConfirmationEmail(user)
  }
}

So, let me know if this example is what you are looking for (I'm assuming you are using Typescript). In that case, I will investigate and see what can I do.

@devlegacy
Copy link

devlegacy commented Oct 2, 2022

First of all, sorry for being so late answering you @chuano.

I'm not 100% sure about what are you asking for. I guess that you would like to be able to use DIOD's container without the need of registering every service. For example, instead of doing this:

const builder = new ContainerBuilder()
builder.register(Mailer).use(AcmeMailer)
builder.register(UserRepository).use(SqliteUserRepository)
builder.registerAndUse(SignUpUseCase) 
const container = builder.build()

const signUpUseCase = container.get(SignUpUseCase)

To just do this:

const builder = new ContainerBuilder()
const container = builder.build()

const signUpUseCase = container.get(SignUpUseCase)

Am I right? I think that this would be impossible, because in that example I am using abstractions and not only class implementations. But it could exist the possibility to automagically register all the services if they all are class implementations.

I mean, instead of doing this:

const builder = new ContainerBuilder()
builder.registerAndUse(AcmeMailer)
builder.registerAndUse(SqliteUserRepository)
builder.registerAndUse(SignUpUseCase) 
const container = builder.build()

const signUpUseCase = container.get(SignUpUseCase)

To just do this, I think it could be possible:

const builder = new ContainerBuilder()
const container = builder.build()

const signUpUseCase = container.get(SignUpUseCase)

As you said importing all the classes is necessary, but in this example it is supposed that the SignUpUseCase will be something like this:

import { Service } from 'diod'
import { SqliteUserRepository } from './SqliteUserRepository'
import { AcmeMailer } from './AcmeMailer'

@Service()
export class SignUpUseCase {
  constructor(
    private readonly userRepository: SqliteUserRepository,
    private readonly mailer: AcmeMailer
  ) {}

  execute(userData: UserDto): void {
    const user = this.userRepository.create(userData)
    this.mailer.sendConfirmationEmail(user)
  }
}

So, let me know if this example is what you are looking for (I'm assuming you are using Typescript). In that case, I will investigate and see what can I do.

I'm interested about your last example, I was trying to implement it but It couldn't be possible
Is there any way to use classes as direct injection by constructor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants