Angular Service Simplification with @Service()

𝗔𝗻𝗴𝘂𝗹𝗮𝗿 𝗶𝘀 𝗴𝗲𝘁𝘁𝗶𝗻𝗴 𝗮 𝗰𝗹𝗲𝗮𝗻𝗲𝗿 𝘄𝗮𝘆 𝘁𝗼 𝗱𝗲𝗳𝗶𝗻𝗲 𝘀𝗲𝗿𝘃𝗶𝗰𝗲𝘀: @𝗦𝗲𝗿𝘃𝗶𝗰𝗲(). For years, we have used: @𝘐𝘯𝘫𝘦𝘤𝘵𝘢𝘣𝘭𝘦({ 𝘱𝘳𝘰𝘷𝘪𝘥𝘦𝘥𝘐𝘯: '𝘳𝘰𝘰𝘵' }) 𝘦𝘹𝘱𝘰𝘳𝘵 𝘤𝘭𝘢𝘴𝘴 𝘋𝘢𝘵𝘢𝘚𝘦𝘳𝘷𝘪𝘤𝘦 {} It works well, but for the most common use case, it can feel a little verbose. Most Angular services are:  • singletons  • provided at root level  • using inject() for dependencies  • not using advanced provider options like useClass, useValue, or    useExisting That is where the idea of @Service() becomes interesting. Instead of writing service intent through @Injectable, the code becomes more direct: @𝘚𝘦𝘳𝘷𝘪𝘤𝘦() 𝘦𝘹𝘱𝘰𝘳𝘵 𝘤𝘭𝘢𝘴𝘴 𝘋𝘢𝘵𝘢𝘚𝘦𝘳𝘷𝘪𝘤𝘦 {  𝘱𝘳𝘪𝘷𝘢𝘵𝘦 𝘩𝘵𝘵𝘱 = 𝘪𝘯𝘫𝘦𝘤𝘵(𝘏𝘵𝘵𝘱𝘊𝘭𝘪𝘦𝘯𝘵); } The main idea is not to replace every @Injectable() use case. It is more about making the common service pattern cleaner: @Service() → simple root service @Injectable() → advanced DI configuration I like this direction because it makes Angular code easier to read for both new developers and experienced teams.  • Less ceremony.  • Clearer intent.  • More modern Angular style. Of course, this should still be treated carefully until it becomes officially stable. But as a direction, I think this makes sense. What do you think? 𝗪𝗼𝘂𝗹𝗱 𝘆𝗼𝘂 𝗽𝗿𝗲𝗳𝗲𝗿 @𝗦𝗲𝗿𝘃𝗶𝗰𝗲() 𝗳𝗼𝗿 𝘀𝗶𝗺𝗽𝗹𝗲 𝗔𝗻𝗴𝘂𝗹𝗮𝗿 𝘀𝗲𝗿𝘃𝗶𝗰𝗲𝘀, 𝗼𝗿 𝗸𝗲𝗲𝗽 𝗲𝘃𝗲𝗿𝘆𝘁𝗵𝗶𝗻𝗴 𝘂𝗻𝗱𝗲𝗿 @𝗜𝗻𝗷𝗲𝗰𝘁𝗮𝗯𝗹𝗲()? #Angular #Angular22 #AngularServices #DependencyInjection #AngularDI #Injectable #TypeScript #FrontendDevelopment #WebDevelopment #AngularDeveloper #JavaScript #SoftwareEngineering #FrontendArchitecture #CleanCode #DeveloperExperience

To view or add a comment, sign in

Explore content categories