Noor A Shuvo
Noor A Shuvo

Reputation: 2817

Route or multiple function in Azure Function App

I am working with Azure Function to consume a SOAP Service and exposing the data in 5 REST endpoints. What I did is, uses the

class ServiceFactory {
    // properties
    // String path, ILogger log, IConfig mappingConfig
    //constructors
        public IService CreateService() {
            switch(path) {
                case ServicePath.service1:
                    return new service1(log, mappingConfig);
                case ServicePath.service2:
                    return new service2(log, mappingConfig);                    
                case ServicePath.service3:
                    return new service3(log, mappingConfig);
                case ServicePath.service4:
                    return new service4(log, mappingConfig);
        }
    }
}
        

and then, the caller method is the azure function

[FunctionName("ServiceFunction")]
public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "{path?}")]
            HttpRequest req,
            ILogger log, string? path)
{
    // Validate Credential
    var validatorResult = await ValidateCredential(_credential);
    if (!validatorResult.IsValid)
    {
        var errors = validatorResult.Errors.Select(error => new
        {
            field = error.PropertyName,
            error = error.ErrorMessage
        });

        return new BadRequestObjectResult(
            JsonConvert.SerializeObject(
                new
                {
                    success = false,
                    message = errors
                }
            )
        );
    }
    
    
    IService service = ServiceFactory(path, req, log, _mappingConfigurationProvider, _service, _credential).CreateService();
    return await service.ServiceTask();
}

so the path is here to call different endpoints.

I am asked to implement each of the endpoint with different functions. What will be the pros and cons here?

Upvotes: 1

Views: 1472

Answers (1)

Thiago Custodio
Thiago Custodio

Reputation: 18387

Pros:

Single responsibility per function, better maintainability, open closed principle

PS: Extract the common logic to a class and share it among the functions.

Cons:

I can't think any cons about this.

Upvotes: 3

Related Questions