zazoomauro / node-dependency-injection

The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
https://github.com/zazoomauro/node-dependency-injection/wiki
MIT License
275 stars 34 forks source link

Enable main property for factory #200

Closed coal182 closed 1 year ago

coal182 commented 1 year ago

The main property in a service definition allows to specify a main class into a file specified by a path in the class property. This is helpful when the filename doesn't match with the service class, for example when using kebab-case for filenames.

Shop.Shared.MongoConfig:
    factory:
      class: ../../../../../contexts/shop/shared/infrastructure/persistence/mongo/mongo-config-factory
      main: MongoConfigFactory
      method: 'createConfig'

Factories definitions doesn't handle the main property at the moment. Since main property is not available for factory (instead of service). Would be possible to add it here?

https://github.com/zazoomauro/node-dependency-injection/blob/cfe270e218d7c6523d08360fc75af5f844687b34/lib/Loader/FileLoader.js#L97

to look like:

object = this._requireClassNameFromPath(service.factory.class, service.factory.main) 

Thanks in advance.

zazoomauro commented 1 year ago

Hello @coal182

feel free to create a PR with this. Sounds reasonable. Please, follow this guide: https://github.com/zazoomauro/node-dependency-injection/blob/master/CONTRIBUTING.md

zazoomauro commented 1 year ago

https://github.com/zazoomauro/node-dependency-injection/wiki/Factory#using-main-to-the-factory-method