Megrez7
Megrez7

Reputation: 1477

Input binding in function other than Azure Function within the same project

I have a Visual Studio Azure Function project with number of functions. Most of them need logic calculating some value based on Azure Storage Table entity.

I can easily make input binding to that entity in each Azure Function:

[FunctionName("Function1")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, 
    [Table("Table", "SomeValue", "SomeOtherValue")] MyPoco poco, ILogger log)
{
    // some logic based on poco object
}

However this creates redundancy as each function must contain the same logic and table binding. I have created a static method for that logic, but input binding does not work there.

public static int DoTheLogic([Table("Table", "SomeValue", "SomeOtherValue")] MyPoco poco)
{
    // some logic based on poco object
    // and return int value to calling Azure Function
}
  1. Can input binding be done only in functions being Azure Functions?
  2. How can I reference to Azure Table in DoTheLogic function if the first one is true.
  3. Same, could ILogger log be used by DoTheLogic function?

The only solution I can see is passing poco and log from each Azure Function to DoTheLogic but that does not look good.

Upvotes: 0

Views: 207

Answers (1)

user1672994
user1672994

Reputation: 10849

Responses based on my opinion:

Can input binding be done only in functions being Azure Functions?

I believe yes it can only done in Azure functions as Binding is injected ExtensionConfigContext by Azure function framework which won't be available in normal functions

How can I reference to Azure Table in DoTheLogic function if the first one is true.

I agree that passing the poco object is right choice which makes your method as pure as it's working on the passes parameters.

Same, could ILogger log be used by DoTheLogic function?

Yes, ILogger should be used in DoTheLogic. However, I would suggest to inject the ILogger<T> in the class containing DoTheLogic method instead of passing it. With that way you will have segregation of concern. Like as shown in this implementation.

Upvotes: 1

Related Questions