elysia icon indicating copy to clipboard operation
elysia copied to clipboard

Add a new constructor parameter to have integrated services

Open Arthurmtro opened this issue 1 year ago • 5 comments

Hey, I'm working on a project, I love the Controller | Service concept for this kind of framework, but actually I was using the

// thing.controller.ts
const service = new MyControllerService() // this is the boring thing because it feels like a hack
export const myController = new Elysia({  prefix: '/myController' })

myController.get(':id', ctx => (
 return service.handleId(ctx.params.id)
)

So in my actual workflow I've managed to have something I prefer, and I fill is more in touch with the mantras of Elysia

// thing.controller.ts
const service = new MyControllerService() // this is the boring thing because it feels like a hack
export const myController = new Elysia({  prefix: '/myController' })
  .decorate('service', new MyControllerService())

myController.get(':id', ctx => (
 return ctx.service.handleId(ctx.params.id)
)

But I notice I do this in any of my controller, as in a clean architecture I believe in the separation of concepts, so controller doesn't deal with all the service related shit but only with the http/api things

What if, we were adding this service as a new parameter for the Elysia constructor ?? I mean, optional but can be cleaner for projects and make it feels less hacky (and can be optimized later on!)

// thing.controller.ts
export const myController = new Elysia({
  prefix: '/myController',
  service: new myController(), //this can be object that already understand some concept of Elysia, so we can extends at creation to have some set of tools ! class ConversionService extends Service {...}
})

myController.get(':id', ctx => (
 return ctx.service.handleId(ctx.params.id)
)

What are your thought on this?

I really think I'm not the only one that does tricky things to handle this, and I think it's a so important flow that including it in the constructor would be even cleaner for code experience

I absolutely can contribute to this, but I want to know that do you think of this all

Happy coding !!

Arthurmtro avatar Nov 29 '23 00:11 Arthurmtro