0

I am trying to use TemplateMethod pattern for creating ServiceBaseClass which will be called from RESTAPI

I am trying to create a wrapper API to call another API, another API will have around 50 endpoints

Need for using TemplateMethod is to provide abstract methods for creating requestURI and requestPaylod like below

public abstract class ServiceBase
    {
        // Template method
        public Task<HttpResponseMessage> CallPostAPI()
        {
            var client = Utilities.GetHttpClient();
            return client.PostAsync(CreateRequestURI(), CreateRequestBody());
        }
        // Abstract operations
        public abstract string CreateRequestURI();
        public abstract HTTPContent CreateRequestBody();

    }

Problem is I will have to create 1 class for every request, rather I wish to create 1 class for every logical operation

What will be correct pattern to allow developers to create their own overridden implementation for creating request and responses and logic to call API will ne centralized or common

Sagar
  • 645
  • 2
  • 9
  • 31
  • _"I am trying to use TemplateMethod pattern for creating ServiceBaseClass which will be called from RESTAPI"_ - **hang on**... what prompted this? why do you think this will _improve_ your codebase or otherwise add-value? (Remember that the GoF's OOP design-patterns can be considered simply as workarounds for limitations in the expressiveness of a language, so not all patterns are necessarily appropriate for any given situation in a modern or constantly-evolving multiparadigm language like C#, which is why I'm curious why you're embarking on this course of action...) – Dai Dec 05 '22 at 03:57
  • _"Problem is I will have to create 1 class for every request, rather I wish to create 1 class for every logical operation"_ - I see no distinction between the two. – Dai Dec 05 '22 at 03:57
  • My RequestPaylod for every request are different for every endpoint(which is expected) and I am using different logic to transform every RequestPayload before sending it to POST API. Hence I wanted developers to just focus on creating URI and RequestPayload and take care of mundane logic in one place So from API controller , we will call just execute method and it will Algorithm to transform URI and RequustPayload I wanted some pattern to achieve this instead of writing code in multiple functions – Sagar Dec 05 '22 at 10:51

0 Answers0