I'm just going to create some issues for some required examples.
We need a Node/express example at:
https://github.com/inversify/inversify-code-samples
I've been doing some analysis and we should be able to integrate InversifyJS with Express without problems.
Feel free to send PRs.
@remojansen can you share your analysis, where is the entry point on the express project to bootstrap and resolve the dependency process. im thinking of creating a middleware but didn't get the spot.
A middleware could be a good place. The composition root is around the routing for sure. When a request arrives to the express server the route maps to an action in a controller. So I thinlk when a new requests arrives a new instance of the required controller should be resolved.
class UserController implements IController<User> {}
class InvoiceControllerimplements IController<Invoice> {}
kernel.bind<IController<User>>("IController<User>").to(UserController)
kernel.bind<IController<Invoice>>("IController<Invoice>").to(InvoiceController)
GET /users/1
let controller = kernel<IController<any>>.get("IUserController") // you need to get "UserController"
controller.get(urlArgs)
POST /invoice { id: 1, user: "", date: "" records: [ /* ... */ ] }
let controller = kernel<IController<any>>.get("IInvoiceController")
controller.post(playload)
So we know the controller from the path and we know the action from the request method. That's how I would do it but it is just an opinion. All the dependencies of the controller are injected via its constructor.
This means that we only use Kernel.get in one place around the routing logic but it actually gets invoked with each new request.
I came up with https://github.com/codyjs/express-typescript-ioc after hacking at an express + Inversify project for a while. It's maybe not as simple as an example should be, but it's fairly easy to understand what's going on without digging too much.
Thoughts on this as an example?
Hi @codyjs thanks a lot for this :) I like it a lot! I will be happy to take this in a PR also your framework stuff could become an npm package if you are interested ? I could create a new project something like inversify-express-utils ?
@remojansen Great, glad you like it!
I like the idea of making the "framework" an npm package, and I'd be happy to contribute. Let me know if you need help with setting it up.
Great :smile_cat: I just created a repo for the framework part https://github.com/inversify/inversify-express-utils
Today I can't do any work but I will try to set up the Travis CI build for you as ASAP (probably ready by monday).
Ok. I will start migrating the code after work today
I have a small example I have been playing with... its still ugly, and needs alot of work, but it works... Anyone is free to check it out and give any advice/code...
https://github.com/DemgelOpenSource/express-mvc
If I feel this is working, I will cleanup more and create some better docs... Also the name will change, just trying to come up with something
I see that there are some things really similar to inversify-express-utils like the @controller decorator and some additional things. For example I don't think inversify-express-utils has a @test decorator. You should work with @codyjs to join both ideas. The best would be that the express example used the inversify and inversify-express-utils npm packages.
Hey, just found this project.
Currently trying to learn the whole thing. Maybe when I have a clearer understanding of this, I can provide and / or extend the examples and also support the express-utils project.
But first I have to rewrite my project in order to get some experience.
Help is welcome :smile: at the moment @codyjs is the main author of the express-utils I'm focus on the inversify core but soon enough I will have more free time and I will help on express-utils as well.
I'm closing this we have now a basic express example :smile: If you want to suggest changes or additions you can do it at https://github.com/inversify/InversifyJS/issues/265