{"id":153,"date":"2023-09-23T16:54:14","date_gmt":"2023-09-23T13:54:14","guid":{"rendered":"https:\/\/codeblog.xyz\/?p=153"},"modified":"2023-10-22T21:28:02","modified_gmt":"2023-10-22T18:28:02","slug":"restful-paradigm-when-not-to-use","status":"publish","type":"post","link":"https:\/\/codeblog.xyz\/?p=153","title":{"rendered":"RESTful Paradigm When to Use and Not to Use"},"content":{"rendered":"\n<p>When considering whether to use REST for a project, especially given your background in C# .NET, Angular, and JavaScript, there are several scenarios and advantages that might make REST a good fit:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Scenarios Ideal for REST<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>CRUD Applications<\/strong>: REST aligns closely with CRUD operations, making it suitable for applications that perform a lot of Create, Read, Update, and Delete operations.<\/li>\n\n\n\n<li><strong>Stateless Services<\/strong>: REST is particularly well-suited for stateless operations where each request from the client to server must contain all the information needed to understand and process the request.<\/li>\n\n\n\n<li><strong>Scalable Systems<\/strong>: Due to its stateless nature, REST is a good fit for applications that need to scale horizontally. Load balancers can route any request to any server, making it easier to handle increased load.<\/li>\n\n\n\n<li><strong>Decoupled Frontend and Backend<\/strong>: When you have a separation between your client and server code, REST can serve as a robust contract that both sides can agree upon.<\/li>\n\n\n\n<li><strong>Multiple Client Types<\/strong>: If your API is going to have multiple clients (web, mobile, other servers), REST&#8217;s stateless, standardized approach can make implementation easier across the board.<\/li>\n\n\n\n<li><strong>Resource-Oriented Architecture<\/strong>: REST is a good choice when you can model your problem domain as a set of resources that can be manipulated through CRUD operations.<\/li>\n\n\n\n<li><strong>Quick Prototyping<\/strong>: Frameworks like ASP.NET Core make it quick and straightforward to spin up a RESTful API, making REST a good choice for prototyping.<\/li>\n\n\n\n<li><strong>Cacheable Data<\/strong>: If your application serves data that can be easily cached to improve performance, REST provides built-in support for cacheability via HTTP headers.<\/li>\n\n\n\n<li><strong>Documentation and Community Support<\/strong>: Given REST&#8217;s popularity, there is a plethora of documentation, tools, and libraries to support development.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Examples in C<\/h3>\n\n\n\n<p>Given your expertise in C#, implementing a REST API in ASP.NET Core would be straightforward:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">\/\/ Simple REST API using ASP.NET Core\n[ApiController]\n[Route(&quot;api\/[controller]&quot;)]\npublic class UserController : ControllerBase\n{\n    [HttpGet]\n    public IEnumerable&lt;User&gt; GetUsers()\n    {\n        \/\/ Retrieve list of users\n    }\n\n    [HttpGet(&quot;{id}&quot;)]\n    public User GetUser(int id)\n    {\n        \/\/ Retrieve a single user by id\n    }\n\n    [HttpPost]\n    public ActionResult&lt;User&gt; PostUser(User user)\n    {\n        \/\/ Create a new user\n    }\n\n    [HttpPut(&quot;{id}&quot;)]\n    public IActionResult PutUser(int id, User user)\n    {\n        \/\/ Update a user\n    }\n\n    [HttpDelete(&quot;{id}&quot;)]\n    public ActionResult&lt;User&gt; DeleteUser(int id)\n    {\n        \/\/ Delete a user\n    }\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<p>Mastering REST could be highly beneficial. It&#8217;s a widely-accepted standard with significant community and enterprise support, making it a safe and future-proof choice. However, as with any technology decision, it&#8217;s crucial to consider the specific needs and constraints of your project.<\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong>When Not to Use<\/strong><\/p>\n\n\n\n<p>Certain types of operations don&#8217;t map neatly onto HTTP verbs (GET, POST, PUT, DELETE) or aren&#8217;t as straightforward to implement in a RESTful paradigm. Here are some scenarios where REST might not be the ideal fit:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Real-Time Updates<\/h3>\n\n\n\n<p>REST is stateless and request-response based. For real-time updates like chatting applications, WebSockets or GraphQL subscriptions might be more suitable.<\/p>\n\n\n\n<p><strong>Best Fit<\/strong>: WebSockets, Server-Sent Events (SSE)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>WebSockets offer two-way communication between the server and client, which is ideal for real-time applications like chats or games.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Batch Processing<\/h3>\n\n\n\n<p>REST doesn&#8217;t naturally support batch operations. If you want to update multiple resources in a single API call, you&#8217;d need to design a custom endpoint for that, which may not follow RESTful principles.<\/p>\n\n\n\n<p><strong>Best Fit<\/strong>: GraphQL, Custom Bulk APIs<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>GraphQL allows you to specify multiple operations in a single query, which can be more efficient for batch processing.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Complex Queries<\/h3>\n\n\n\n<p>REST typically deals with resources one at a time. For complex queries that join, filter, or aggregate multiple types of resources, a more flexible query language like GraphQL might be easier to use.<\/p>\n\n\n\n<p><strong>Best Fit<\/strong>: GraphQL<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>With GraphQL, you can request exactly the data you need and perform complex queries that join, filter, or aggregate multiple types of resources.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Transactions<\/h3>\n\n\n\n<p>HTTP verbs do not naturally map to transactional operations that you might find in a database. For example, if you need to ensure that multiple resources are updated atomically, doing so in a RESTful way can be challenging.<\/p>\n\n\n\n<p><strong>Best Fit<\/strong>: Two-Phase Commit, Custom Transactional APIs<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Two-Phase Commit can ensure that multiple resources are updated atomically, although this may introduce complexity and overhead.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Long-Running Operations<\/h3>\n\n\n\n<p>In REST, a request expects a corresponding response. If an operation might take a long time to complete, you&#8217;ll need to work around this by implementing things like polling, which is less than ideal.<\/p>\n\n\n\n<p><strong>Best Fit<\/strong>: Asynchronous APIs, Job Queues<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Asynchronous APIs or job queues allow you to handle long-running operations by returning a token that you can use to poll or be notified when the operation is complete.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Multiple Operations in a Single Request<\/h3>\n\n\n\n<p>Sometimes, you may need to perform multiple operations that need to be executed in a specific order or within a single transaction. REST doesn&#8217;t have a built-in, standard way to handle this.<\/p>\n\n\n\n<p><strong>Best Fit<\/strong>: GraphQL, RPC (Remote Procedure Call)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>With GraphQL or RPC, you can define custom functions that perform multiple operations atomically or in a specific order.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Streaming Data<\/h3>\n\n\n\n<p>REST is not ideal for scenarios where the client needs a continuous flow of data and would have to constantly poll the server for new information.<\/p>\n\n\n\n<p><strong>Best Fit<\/strong>: WebSockets, Server-Sent Events (SSE), gRPC streaming<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>These protocols support continuous data streams without requiring repeated polling.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Function Calls<\/h3>\n\n\n\n<p>Operations that are function calls involving complex parameters and multiple steps do not fit well into the resource-oriented nature of REST.<\/p>\n\n\n\n<p><strong>Best Fit<\/strong>: RPC (Remote Procedure Call), SOAP<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>RPC and SOAP are more suitable for actions that can&#8217;t be neatly modeled as CRUD (Create, Read, Update, Delete) operations.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example in C#:<\/h3>\n\n\n\n<p>For real-time updates, you might prefer using SignalR instead of REST.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">\/\/ SignalR Hub in C#\npublic class ChatHub : Hub\n{\n    public async Task SendMessage(string user, string message)\n    {\n        await Clients.All.SendAsync(&quot;ReceiveMessage&quot;, user, message);\n    }\n}<\/pre><\/div>\n\n\n\n<p>REST is great for a wide range of applications but does have its limitations. Understanding these can help you decide when to use REST and when to consider other technologies.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When considering whether to use REST for a project, especially given your background in C# .NET, Angular, and JavaScript, there are several scenarios and advantages that might make REST a good fit: Scenarios Ideal for REST Examples in C Given your expertise in C#, implementing a REST API in ASP.NET Core would be straightforward: Summary [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":164,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":3,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":256,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions\/256"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/media\/164"}],"wp:attachment":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}