Skip to content

Generating constants from specified Azure Functions attributes. This can be handy if we want to create middlewares that can act differently based on attribute usage on a function. Like starting a DB transaction for non-readonly functions.

License

nagybalint001/functions-attributes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FunctionsAttributes

license NuGet downloads

Generating constants from specified Azure Functions attributes. This can be handy if we want to create middlewares that can act differently based on attribute usage on a function. Like starting a DB transaction for non-readonly functions.

Sample code:

// here we provide the attributes to listen to:
[assembly: FunctionsAttributes.GenerateFunctionsWith(typeof(CustomAttribute))]
[assembly: FunctionsAttributes.GenerateFunctionsWith(typeof(OtherCustomAttribute))]
namespace FunctionsAttributes.Samples
{
    public class MyFunctions
    {
        [Function("MyFunction")]
        [Custom]
        [OtherCustom]
        public void MyFunctionMethod()
        {
        }

        [Function(nameof(MyOtherFunction))]
        [OtherCustom]
        public void MyOtherFunction()
        {
        }

        // this does not have a Function attribute, so this is skipped
        [Custom]
        public void ThisIsSkipped()
        {
        }
    }

    [AttributeUsage(AttributeTargets.Method)]
    public class CustomAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method)]
    public class OtherCustomAttribute : Attribute
    {
    }

    public class SomeOtherPlace
    {
        public void MyMethod()
        {
            GenerateFunctionsWithAttribute.OtherCustomAttribute.Contains("MyFunction"); // true
            GenerateFunctionsWithAttribute.CustomAttribute.Contains("MyOtherFunction"); // false
        }
    }
}

Generated file should look like this:

using System.Collections.ObjectModel;

namespace FunctionsAttributes
{
    public partial class GenerateFunctionsWithAttribute
    {
        public static readonly ReadOnlyCollection<string> CustomAttribute = new ReadOnlyCollection<string>(new[]
        {
            "MyFunction",
        });
        public static readonly ReadOnlyCollection<string> OtherCustomAttribute = new ReadOnlyCollection<string>(new[]
        {
            "MyFunction",
            "MyOtherFunction",
        });
    }
}

About

Generating constants from specified Azure Functions attributes. This can be handy if we want to create middlewares that can act differently based on attribute usage on a function. Like starting a DB transaction for non-readonly functions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages