1"use strict";(self.webpackChunk_inversifyjs_inversify_docs_site=self.webpackChunk_inversifyjs_inversify_docs_site||[]).push([["172"],{497(e,n,o){o.r(n),o.d(n,{assets:()=>u,contentTitle:()=>d,default:()=>g,frontMatter:()=>c,metadata:()=>t,toc:()=>h});var t=o(16262),i=o(89292),a=o(19736),s=o(94152),r=o(21422),l=o(48314);let c={slug:"announcing-inversify-7-0-0-alpha-4-prerelease",title:"Announcing 7.0.0-alpha.4",authors:["notaphplover"],tags:["releases"]},d,u={authorsImageUrls:[void 0]},h=[{value:"Motivation",id:"motivation",level:2}];function p(e){let n={a:"a",code:"code",h2:"h2",p:"p",...(0,a.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.p,{children:"In this version, we are announcing a new type of binding!"}),"\n","\n",(0,i.jsxs)(n.p,{children:["Several users asked for a way to pass resolved services as arguments to a factory or a provider. ",(0,i.jsx)(n.a,{href:"/docs/next/api/binding-syntax#toresolvedvalue",children:"Resolved value bindings"})," allows providing service dependencies without relying on ",(0,i.jsx)(n.code,{children:"ResolutionContext"})," (formerly known as ",(0,i.jsx)(n.code,{children:"Context"}),")."]}),"\n",(0,i.jsx)(n.p,{children:"consider the following example:"}),"\n",(0,i.jsx)(l.A,{className:"language-ts",children:r}),"\n",(0,i.jsxs)(n.p,{children:["Previously, the most common way to achieve this was by using ",(0,i.jsx)(n.code,{children:"Context"})," base dynamic values. Instead, ",(0,i.jsx)(n.code,{children:"toResolvedValue"})," can be used to achieve the same result."]}),"\n",(0,i.jsx)(n.h2,{id:"motivation",children:"Motivation"}),"\n",(0,i.jsxs)(n.p,{children:["When ",(0,i.jsx)(n.code,{children:".toResolvedValue"})," is a good enough solution, using ",(0,i.jsx)(n.code,{children:"ResolutionContext"})," should not be the way to go in these scenarios. ",(0,i.jsx)(n.code,{children:"ResolutionContext"})," is a powerful API, but it comes with a few cons as well."]}),"\n",(0,i.jsxs)(n.p,{children:["Every time ",(0,i.jsx)(n.code,{children:"ResolutionContext.get"})," is used, a new resolution is triggered. This resolution is unaware of the parent resolution. Not the end of the world, but this lack of awareness can lead to, for example, not providing the most friendly error messages when a circular dependency is detected if the cycle involves multiple resolution contexts."]}),"\n",(0,i.jsxs)(n.p,{children:["On the other hand, ",(0,i.jsx)(n.code,{children:"ResolutionContext"})," based bindings might be useful in more complex scenarios. For example:"]}),"\n",(0,i.jsx)(l.A,{className:"language-ts",children:s}),"\n",(0,i.jsxs)(n.p,{children:["Can this be done with ",(0,i.jsx)(n.code,{children:"toResolvedValue"}),"? Yes, but it would require extra boilerplate code. You might be interesed in doing so, and implementing your ",(0,i.jsx)(n.code,{children:"Engine"})," factory without relying on ",(0,i.jsx)(n.code,{children:"ResolutionContext"})," would be an excellent way to provide code less coupled to ",(0,i.jsx)(n.code,{children:"inversify"}),". On the other hand, ",(0,i.jsx)(n.code,{children:".toFactory"})," can be used to achieve the same result with less code."]})]})}function g(e={}){let{wrapper:n}={...(0,a.R)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(p,{...e})}):p(e)}},94152(e){e.exports="container.bind<Engine>('Engine').to(PetrolEngine).whenNamed('petrol');\ncontainer.bind<Engine>('Engine').to(DieselEngine).whenNamed('diesel');\n\ncontainer\n .bind<Factory<(displacement: number) => Engine, [string]>>('Factory<Engine>')\n .toFactory((context: ResolutionContext) => {\n return (named: string) => (displacement: number) => {\n const engine: Engine = context.get<Engine>('Engine', {\n name: named,\n });\n engine.displacement = displacement;\n return engine;\n };\n });\n\n@injectable()\nclass DieselCarFactory implements CarFactory {\n readonly #dieselFactory: (displacement: number) => Engine;\n\n constructor(\n @inject('Factory<Engine>')\n factory: (category: string) => (displacement: number) => Engine, // Injecting an engine factory\n ) {\n // Creating a diesel engine factory\n this.#dieselFactory = factory('diesel');\n }\n\n public createEngine(displacement: number): Engine {\n // Creating a concrete diesel engine\n return this.#dieselFactory(displacement);\n }\n}"},21422(e){e.exports="class Katana {\n public material!: string;\n public damage!: number;\n}\n\nconst dbConnectionSymbol: symbol = Symbol.for('DbConnection');\nconst katanaDbCollectionSymbol: symbol = Symbol.for('KatanaRepository');\n\nconst container: Container = new Container();\n\n@injectable()\nclass KatanaRepository {\n readonly #dbCollection: AwesomeDbDriverCollection<Katana>;\n\n constructor(\n @inject(katanaDbCollectionSymbol)\n dbCollection: AwesomeDbDriverCollection<Katana>,\n ) {\n this.#dbCollection = dbCollection;\n }\n\n public async find(query: unknown): Promise<Katana[]> {\n return this.#dbCollection.find(query);\n }\n}\n\ncontainer.bind(MyAwesomeEnvService).toSelf();\ncontainer\n .bind(dbConnectionSymbol)\n .toResolvedValue(\n async (\n envService: MyAwesomeEnvService,\n ): Promise<AwesomeDbDriverConnection> => {\n const databaseUrl: string = envService.getEnvironment().dbUrl;\n\n return AwesomeDbDriverImplementation.connect(databaseUrl);\n },\n [MyAwesomeEnvService],\n )\n .inSingletonScope();\n\ncontainer\n .bind(katanaDbCollectionSymbol)\n .toResolvedValue(\n (\n connection: AwesomeDbDriverConnection,\n ): AwesomeDbDriverCollection<Katana> =>
1 {\n return connection.getCollection(Katana);\n },\n [dbConnectionSymbol],\n )\n .inSingletonScope();\n\ncontainer.bind(KatanaRepository).toSelf();"},16262(e){e.exports=JSON.parse('{"permalink":"/blog/announcing-inversify-7-0-0-alpha-4-prerelease","source":"@site/blog/2025-02-02-announcing-inversify-7-0-0-alpha-4-prerelease/index.mdx","title":"Announcing 7.0.0-alpha.4","description":"In this version, we are announcing a new type of binding!","date":"2025-02-02T00:00:00.000Z","tags":[{"inline":false,"label":"Releases","permalink":"/blog/tags/releases","description":"InversifyJS releases related posts"}],"readingTime":1.4,"hasTruncateMarker":true,"authors":[{"name":"Roberto Pintos L\xf3pez","title":"InversifyJS maintainer","url":"https://github.com/notaphplover","page":{"permalink":"/blog/authors/notaphplover"},"socials":{"github":"https://github.com/notaphplover"},"imageURL":"https://github.com/notaphplover.png","key":"notaphplover"}],"frontMatter":{"slug":"announcing-inversify-7-0-0-alpha-4-prerelease","title":"Announcing 7.0.0-alpha.4","authors":["notaphplover"],"tags":["releases"]},"unlisted":false,"prevItem":{"title":"Announcing 7.0.0-alpha.5","permalink":"/blog/announcing-inversify-7-0-0-alpha-5-prerelease"},"nextItem":{"title":"Announcing 7.0.0-alpha.2","permalink":"/blog/announcing-inversify-7-0-0-alpha-2-prerelease"}}')}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.