1

I have been given a project that uses Durable Functions to run a few sequential logic steps with some triggers for timers/http etc.

Problem is there is sooooo much boilerplate code here, there is about 150 lines in each Azure function and about 120 lines of that are virtually identical between function classes. This means when a new one is added the developer has to know to copy the same conventions and setup triggers etc which is tiresome and makes things hard to read/maintain.

If this wasn't using Azure Functions and was just daisy chained awaited functions with some orchestration logic I would normally expect an interface or abstract class of some kinda to enforce the convention so a new addition would be inherited/implemented and you implement the bits that are unique to your instance following the convention.

ANYWAY as it is all using Azure Functions the only documentation I can find for setting up Functions/Triggers is using attributes, but they require compile time values to be passed in as well as some other constraints, but I was wondering if there is any escape hatch for people to do something like

appBuilder.RegisterFunction(nameof(SomeFunctionSomewhere), 
                            x => x.WithHttpTrigger(AuthorizationLevel.Anonymous, "GET"))

The main issue I am facing is if I were to move to an abstract class approach to wrap up the conventions, trying to name the functions wouldn't work as they would all end up with same name as the attribute cannot take any polymorphic data into the names etc (from what I can tell).

If you can't do this no problem but wanted to ask

5
  • 2
    You could just write a code generator to output all the boilerplate stuff. Having said that, I can't really comment on your specific code, but usually this can be extracted into base classes etc. At least, that is what I do in the codebase I maintain. Commented Oct 6 at 15:16
  • 1
    Not sure what exactly your boilerplate is, but if it's literally identical, as you write, then how about Composition? If you can factor out 120 of 150 lines into 1 line, that would ease the pain already, right? Commented Oct 6 at 15:18
  • I would rather composition over inheritance but the problem is you cant pass around the durable function context anywhere to have that invoke other azure functions etc, and most of the boiler plate is a timer/http trigger scheduling a durable function which then calls several other activity triggers which end up resolving to some c# service calls which are the unique bit. So I can't really do much as you cant await on non azure functions in a durable context and you cant pass it to another method to invoke on its behalf :( Commented Oct 6 at 15:36
  • It's called a base class. I do this on all of our functions. Most are one or two lines long at this point regardless of the complexity behind them. Commented Oct 6 at 15:40
  • @gilliduck How do you handle duplicate function names in your inherited classes from your base class? i.e if you have an abstract class with [Function(nameof(MyFunction))] public Task MyFunction([ActivityTrigger] string someData) every inherited class would cause that MyFunction to get registered right? so it would throw issues? if you have a way around this can you post it as an answer with an example? Commented Oct 7 at 15:08

1 Answer 1

1

appBuilder.RegisterFunction(functionProperty.Name, x=>x.WithHttpTrigger())

Here classes implement abstract class but will the help of Name property we will not get duplicate function names for each concrete class and using framework Function Monkey we can bind to specific function and call it without failure in function deployment

Sign up to request clarification or add additional context in comments.

1 Comment

Do you have any links to documentation as I cant find this method anywhere, and in the above example I just made this up as a sort of "what I would like to see" not a real world API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.