{"id":57993,"date":"2023-08-29T14:32:26","date_gmt":"2023-08-29T09:02:26","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=57993"},"modified":"2023-09-05T14:37:11","modified_gmt":"2023-09-05T09:07:11","slug":"designing-a-multi-layered-api-architecture-an-easy-way-to-code-nice","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/designing-a-multi-layered-api-architecture-an-easy-way-to-code-nice\/","title":{"rendered":"Designing a multi layered API architecture: An easy way to code nice."},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p>Writing perfect code is the dream of every developer. The definition of good code not only comprises running a functional code but also consists of writing code that is extensible, debuggable, logged, and catches exceptions well. Dividing it into multiple parts and making it maintainable reduces a lot of debugging. Since the code is converted into smaller chunks, changing the particular component becomes easier. Even the changes we are making become localized, simpler, and less extensive than they would otherwise be.<br \/>\nDesigning effective multi-layered architecture is a common way to avoid unnecessary problems and write high-quality code.<\/p>\n<h2><strong>Project Structure<\/strong><\/h2>\n<p>Multi-layered architecture must be represented by the Project structure designed. Every layer should be enclosed in a particular package where each component should have the same name as the layer itself. This way, getting down to a class where logic is implemented becomes very easy and intuitive. Parallely, this also resolves the problem of where to place a new class.<\/p>\n<h2><strong>What are the different layers we are talking about<\/strong><\/h2>\n<p><strong>\u25cf Controller Layer:\u00a0 \u00a0<\/strong>This layer adopts the request from the client browser or presentation layer and dispatches UI elements or event handlers. UI element represents object state or business logic. It notifies any observer when data changes. Every request passes via a controller and retrieves visual object values. A controller:<\/p>\n<ul>\n<li>manages the incoming work HTTP requests<\/li>\n<li>decides which worker should do the work<\/li>\n<li>splits up the work into sizable units<\/li>\n<li>passes that work the necessary data from the HTTP requests off to the service(s)<\/li>\n<\/ul>\n<p><strong>\u25cf Service Layer: <\/strong>This layer mainly contains the business logic of the application. The service layer is the place that is connected to the data layer to get raw data and get mapped as the response is expected. A Service class typically:<\/p>\n<ul>\n<li>receives the requested data it needs from the manager in order to perform its tasks<\/li>\n<li>figures out the individual details algorithms\/business logic\/database calls\/etc involved in completing the request<\/li>\n<li>is generally only concerned with the tasks he\/she has to complete<\/li>\n<li>not responsible for making decisions about the &#8220;bigger&#8221; picture and orchestrating the different service calls<\/li>\n<li>does the actual work necessary to complete the tasks\/request<\/li>\n<li>returns the completed work a response to the manager<\/li>\n<\/ul>\n<p><strong>\u25cf Data Layer: <\/strong>This layer mainly deals with databases, creating connections, executing SQLs, and other data-related operations.A Data layer typically:<\/p>\n<ul>\n<li>While changing a persistence mechanism, the service layer doesn\u2019t even have to know where the data comes from. For example, if you\u2019re thinking of shifting from using MySQL to MongoDB, all changes need to be done in the DAO layer only.<\/li>\n<li>DAO pattern emphasizes the low coupling between different components of an application. So, the View layer has no dependency on the DAO layer, and only the Service layer depends on it, even with the interfaces and not from concrete implementation.<\/li>\n<li>As the persistence logic is completely separate, it is much easier to write Unit tests for individual components. For example, if you\u2019re using JUnit and Mockito for testing frameworks, it will be easy to mock the individual components of your application.<\/li>\n<li>As we work with interfaces in the DAO pattern, it also emphasizes the style of \u201cwork with interfaces instead of implementation,\u201d which is an excellent OOPs programming style.<\/li>\n<\/ul>\n<p><strong>\u25cf Model Layer: <\/strong>The model represents real-world objects. Thus, these objects are referred to as Models. JPA (Java Persistence API) provides reference or details about ORM( Object Relation Mapping) which means the class can be related to the database table.<\/p>\n<p><strong><strong>\u25cf Utils Packages: <\/strong><\/strong>This layer contains classes that have methods to be used across the applications, the constant values, and the validation methods<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-57994\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-2023-08-16-at-4.11.56-PM.png\" alt=\"\" width=\"1554\" height=\"684\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-2023-08-16-at-4.11.56-PM.png 1554w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-2023-08-16-at-4.11.56-PM-300x132.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-2023-08-16-at-4.11.56-PM-1024x451.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-2023-08-16-at-4.11.56-PM-768x338.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-2023-08-16-at-4.11.56-PM-1536x676.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-2023-08-16-at-4.11.56-PM-624x275.png 624w\" sizes=\"(max-width: 1554px) 100vw, 1554px\" \/><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Introduction Writing perfect code is the dream of every developer. The definition of good code not only comprises running a functional code but also consists of writing code that is extensible, debuggable, logged, and catches exceptions well. Dividing it into multiple parts and making it maintainable reduces a lot of debugging. Since the code is [&hellip;]<\/p>\n","protected":false},"author":1352,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":166},"categories":[1993,3887],"tags":[1832,5333,5334],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57993"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1352"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=57993"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57993\/revisions"}],"predecessor-version":[{"id":58317,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57993\/revisions\/58317"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=57993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=57993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=57993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}