question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Providing values to the constructor/factory

See original GitHub issue

Hello,

maybe I am getting something wrong, but I am looking for a way to provide a parameter to the constructor. My use case is, that I would like to inject a logger, that should be instantiated with a module name, e.g. const logger = new Logger('moduleX');

That I would like to change to work like this

class ModuleX {

  @Inject
  private logger: Logger;

  public doSmth () {
    
    this.logger.info('log something');
  }
}

Obviously this is not supported and therefor I assume not a good practice, or am I missing something?

thank you!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
atonamycommented, Jun 8, 2019

@pleerock How about situation when we have to pass data dynamically from external source? Can we do it this way?

@Service({factory: (data: {id: {engineType: string}}) => {
  return new Car(data.id.engineType)
}})
class Car {
  constructor (public engineType: string) {
  }
}

const dynamicDataFromExternalSource = {...more, engineType: "electric"}
const car = Container.of({engineType: dynamicDataFromExternalSource.engineType}).get(Car)
2reactions
pleerockcommented, Feb 22, 2018

Depend on what logger is. If it is a service then it should look something like this:

@Service()
export class Logger {

     constructor(@Inject("logger.name") name: string) {
     }

}

And then somewhere: 

Container.set("logger.name", "some name");

Else, if logger not a service then you can simply do:

Container.set(Logger, new Logger("some name"));
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to assign value to a variable inside factory constructor ...
Problem: In factory constructor, when I get data , I also need to initialise downloadUrl field but I can't do that in factory...
Read more >
Java Constructors vs Static Factory Methods - Baeldung
Learn about static factory methods in Java and why they're sometimes preferred over constructors for instantiating and initializing objects.
Read more >
Dart/Flutter Constructors tutorial with examples - BezKoder
We can use the factory keyword for a constructor that return an object instead of creating a new instance. class Customer { String...
Read more >
Difference Between Constructor and Static Factory Method in ...
The static factory method has names that clarify the code, unlike the constructors. In the static factory method, we do not need to...
Read more >
Replace constructor with factory method | IntelliJ IDEA ...
The Replace Constructor With Factory Method refactoring lets you hide a constructor and replace it with a static method which returns a new ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found