{"id":2786,"date":"2023-07-16T08:20:00","date_gmt":"2023-07-16T06:20:00","guid":{"rendered":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2786"},"modified":"2023-07-17T15:22:53","modified_gmt":"2023-07-17T13:22:53","slug":"dependency-inversion-principle","status":"publish","type":"post","link":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2786","title":{"rendered":"Dependency Inversion Principle"},"content":{"rendered":"<p style=\"text-align: justify;\">Last principle stating that high-level modules should not be dependent low-level modules, and their dependencies should rely on abstraction by interfaces or abstract classes. In simple terms &#8211; we should minify dependencies from certain implementations. The best way to achieve it, is to rely on interfaces &#8211; then our code has small amount of dependencies, and as we know, interfaces are stable &#8211; it means, that if we do any changes to our interface, this change will be related with changes in interface implementation. Lets move on to wrong code, where high-module depends on low-level module:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Employee\n{\n    public string Name { get; set; }\n}\n\npublic class EmployeeRepository\n{\n    public void Add(Employee employee)\n    {\n       \/\/some code to add\n    }\n}\n\npublic class EmployeeService\n{\n    private EmployeeRepository _employeeRepository = new EmployeeRepository();\n\n    public void Add(Employee employee)\n    {\n        _employeeRepository.Add(employee);\n    }\n} \n<\/pre><\/div>\n\n<p style=\"text-align: justify;\">To start with, we have to define some basics, which are high-level modules and low-level modules. In example displayed above, <span style=\"background-color: #efefef;\">EmployeeService<\/span> is high-level module class, and low-level module is <span style=\"background-color: #efefef;\">EmployeeRepository<\/span> class. High-level module in this case is dependent on low-level module, because it uses implementation of <span style=\"background-color: #efefef;\">EmployeeRepository<\/span> class, which is obviously breaking spoken principle. To make this code correct, there is a need to &#8222;switch&#8221; this dependency, just like the name of principle states:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Employee\n{\n    public string Name { get; set; }\n}\n\n\/\/created interface with one method implemented\npublic interface IEmployeeRepository\n{\n    void Add(Employee employee);\n}\n\n\/\/ EmployeeRepository class now inherits IEmployeeRepository interface\npublic class EmployeeRepository : IEmployeeRepository\n{\n    public void Add(Employee employee)\n    {\n        \/\/some code to add\n    }\n}\n\n\/\/created constructor, which injects dependency\npublic class EmployeeService\n{\n    private IEmployeeRepository _employeeRepository;\n\n    public EmployeeService(IEmployeeRepository employeeRepository)\n    {\n        _employeeRepository = employeeRepository;\n    }\n\n    public void Add(Employee employee)\n    {\n        _employeeRepository.Add(employee);\n    }\n} \n<\/pre><\/div>\n\n<p style=\"text-align: justify;\">Correct code above, our class <span style=\"background-color: #efefef;\">EmployeeService<\/span> does not depend on actual implementation of <span style=\"background-color: #efefef;\">EmployeeRepository<\/span>, but on its abstraction &#8211; in this case, I<span style=\"background-color: #efefef;\">EmployeeRepository<\/span> interface. Changes made in low-level module does not affect on high-level module, thanks to that we could &#8222;switch&#8221; our dependencies.&nbsp;<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last principle stating about modules dependencies.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[1,13],"tags":[],"class_list":["post-2786","post","type-post","status-publish","format-standard","hentry","category-bez-kategorii","category-solid-articles"],"_links":{"self":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2786"}],"version-history":[{"count":11,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2786\/revisions"}],"predecessor-version":[{"id":2801,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2786\/revisions\/2801"}],"wp:attachment":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}