K
K
Kirill Nesmeyanov2016-07-07 20:53:28
JavaScript
Kirill Nesmeyanov, 2016-07-07 20:53:28

Can you offer ideas/criticisms for the Di package API?

Briefly about the project: Container with service location and dependency injection.
Actually a link to the wiki on this very api - there are examples of specifying types for injection and declaring the behavior of "services": https://github.com/SerafimArts/DependencyInjecton/wiki

I do not want to duplicate, because it is quite a mile away.

JS has a few problems that I've encountered:
What I would like:
Injection via flowtype ( https://flowtype.org/ ):
class Some {
    constructor(user: User, guard: BaseAuth) { ... }
}

Problem:
Cannot get type information when using FlowType for automatic argument injection. Here this plugin does not work: https://www.npmjs.com/package/babel-plugin-type-me...
Solution:
It was decided to do it through decorators ( babel-plugin-decorators-legacy ), in particular @Inject with specifying the types of the arguments or the service locator in them.
I would not want to use non-standard legacy syntax/behavior (decorators). Any ideas how else to do it?
I would like #2:
To have two methods: `bind` for factory and `singleton` for singletons and, depending on the type of the second argument, determine the method of declaration (for a class in the case of `bind` each time a new object, for a function - a function call, etc. .d.):
app.bind('some', User);
app.make('some') // новый объект User
app.make('some') // новый объект User, совершенно другой, нежели раньше

app.bind('some', (app: Container) => new User('some', 'any'));
app.make('some') // вызываем замыкание, прокидываем контейнер (резолв из flowtype) и возвращаем результат
app.make('some') // Проделываем тоже самое ещё раз

Problem #2:
In ES6, it's impossible to tell if a function is a method, a class, or a regular function.
Decision #2:
Divided the methods into several, different ones:
1) Creates an instance from the function
2) Calls the function
3) Calls the function with the specified context
I.e. I had to make a kilometer of different functions, instead of a pair with polymorphic behavior (so to speak).
Any other ideas on how to simplify/improve the API so that it is convenient to use? There is a small example of implementation (you can put a bolt on the code, because it is now the design stage), but because of the jambs in JS, I decided to start designing the interface first.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question