[{"data":1,"prerenderedAt":205},["ShallowReactive",2],{"term-d\u002Fdependency-inversion":3,"related-d\u002Fdependency-inversion":191},{"id":4,"title":5,"acronym":6,"body":7,"category":173,"description":174,"difficulty":175,"extension":176,"letter":177,"meta":178,"navigation":104,"path":179,"related":180,"seo":185,"sitemap":186,"stem":189,"subcategory":6,"__hash__":190},"terms\u002Fterms\u002Fd\u002Fdependency-inversion.md","Dependency Inversion",null,{"type":8,"value":9,"toc":167},"minimark",[10,15,19,23,26,30,156,160,163],[11,12,14],"h2",{"id":13},"eli5-the-vibe-check","ELI5 — The Vibe Check",[16,17,18],"p",{},"Dependency Inversion says high-level code shouldn't depend on low-level code — both should depend on abstractions. Your UserService shouldn't import PostgresDatabase directly. It should depend on a DatabaseInterface, and Postgres is just one thing that satisfies that interface.",[11,20,22],{"id":21},"real-talk","Real Talk",[16,24,25],{},"The Dependency Inversion Principle states that high-level modules should not depend on low-level modules — both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions. Implemented through interfaces and Dependency Injection.",[11,27,29],{"id":28},"show-me-the-code","Show Me The Code",[31,32,37],"pre",{"className":33,"code":34,"language":35,"meta":36,"style":36},"language-typescript shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","\u002F\u002F Without DI: tightly coupled\nclass UserService {\n  private db = new PostgresDatabase(); \u002F\u002F hard dependency\n}\n\n\u002F\u002F With DI: depends on abstraction\nclass UserService {\n  constructor(private db: IDatabase) {} \u002F\u002F injected\n}\n","typescript","",[38,39,40,49,64,93,99,106,112,121,151],"code",{"__ignoreMap":36},[41,42,45],"span",{"class":43,"line":44},"line",1,[41,46,48],{"class":47},"sHwdD","\u002F\u002F Without DI: tightly coupled\n",[41,50,52,56,60],{"class":43,"line":51},2,[41,53,55],{"class":54},"spNyl","class",[41,57,59],{"class":58},"sBMFI"," UserService",[41,61,63],{"class":62},"sMK4o"," {\n",[41,65,67,70,74,77,80,83,87,90],{"class":43,"line":66},3,[41,68,69],{"class":54},"  private",[41,71,73],{"class":72},"swJcz"," db",[41,75,76],{"class":62}," =",[41,78,79],{"class":62}," new",[41,81,82],{"class":72}," PostgresDatabase",[41,84,86],{"class":85},"sTEyZ","()",[41,88,89],{"class":62},";",[41,91,92],{"class":47}," \u002F\u002F hard dependency\n",[41,94,96],{"class":43,"line":95},4,[41,97,98],{"class":62},"}\n",[41,100,102],{"class":43,"line":101},5,[41,103,105],{"emptyLinePlaceholder":104},true,"\n",[41,107,109],{"class":43,"line":108},6,[41,110,111],{"class":47},"\u002F\u002F With DI: depends on abstraction\n",[41,113,115,117,119],{"class":43,"line":114},7,[41,116,55],{"class":54},[41,118,59],{"class":58},[41,120,63],{"class":62},[41,122,124,127,130,133,136,139,142,145,148],{"class":43,"line":123},8,[41,125,126],{"class":54},"  constructor",[41,128,129],{"class":62},"(",[41,131,132],{"class":54},"private",[41,134,73],{"class":135},"sHdIc",[41,137,138],{"class":62},":",[41,140,141],{"class":58}," IDatabase",[41,143,144],{"class":62},")",[41,146,147],{"class":62}," {}",[41,149,150],{"class":47}," \u002F\u002F injected\n",[41,152,154],{"class":43,"line":153},9,[41,155,98],{"class":62},[11,157,159],{"id":158},"when-youll-hear-this","When You'll Hear This",[16,161,162],{},"\"Apply Dependency Inversion so we can mock the database in tests.\" \u002F \"DIP is why we inject repositories instead of instantiating them.\"",[164,165,166],"style",{},"html pre.shiki code .sHwdD, html code.shiki .sHwdD{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic}html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .sBMFI, html code.shiki .sBMFI{--shiki-light:#E2931D;--shiki-default:#FFCB6B;--shiki-dark:#FFCB6B}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .sHdIc, html code.shiki .sHdIc{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#BABED8;--shiki-dark-font-style:italic}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":36,"searchDepth":51,"depth":51,"links":168},[169,170,171,172],{"id":13,"depth":51,"text":14},{"id":21,"depth":51,"text":22},{"id":28,"depth":51,"text":29},{"id":158,"depth":51,"text":159},"architecture","Dependency Inversion says high-level code shouldn't depend on low-level code — both should depend on abstractions.","intermediate","md","d",{},"\u002Fterms\u002Fd\u002Fdependency-inversion",[181,182,183,184],"SOLID","Dependency Injection","Inversion of Control","Interface Segregation",{"title":5,"description":174},{"changefreq":187,"priority":188},"weekly",0.7,"terms\u002Fd\u002Fdependency-inversion","EwgZG3SHRkGep1bDCMfWIty_Fj_Wyjyt2VK6LOvV9kU",[192,195,198,202],{"title":182,"path":193,"acronym":6,"category":173,"difficulty":175,"description":194},"\u002Fterms\u002Fd\u002Fdependency-injection","Instead of your UserService creating its own DatabaseConnection (tight coupling), you pass the database in from outside: new UserService(db).",{"title":184,"path":196,"acronym":6,"category":173,"difficulty":175,"description":197},"\u002Fterms\u002Fi\u002Finterface-segregation","Interface Segregation means don't force classes to implement methods they don't need.",{"title":183,"path":199,"acronym":200,"category":173,"difficulty":175,"description":201},"\u002Fterms\u002Fi\u002Finversion-of-control","IoC","Inversion of Control is when a framework calls YOUR code instead of you calling the framework. You don't control the flow anymore — the framework does.",{"title":181,"path":203,"acronym":181,"category":173,"difficulty":175,"description":204},"\u002Fterms\u002Fs\u002Fsolid","SOLID is five rules for writing code that doesn't turn into a nightmare over time. Each letter stands for a different rule.",1776518273703]