Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider using modern C# features for generated triggers #1577

Open
captainsafia opened this issue Oct 8, 2024 · 1 comment
Open

Consider using modern C# features for generated triggers #1577

captainsafia opened this issue Oct 8, 2024 · 1 comment

Comments

@captainsafia
Copy link

captainsafia commented Oct 8, 2024

Currently, instantiated new triggers with the C# isolate model will produce code like the following:

using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public class BlobTriggerCSharp
    {
        private readonly ILogger<BlobTriggerCSharp> _logger;

        public BlobTriggerCSharp(ILogger<BlobTriggerCSharp> logger)
        {
            _logger = logger;
        }

        [Function(nameof(BlobTriggerCSharp))]
        public async Task Run([BlobTrigger("PathValue/{name}", Connection = "ConnectionValue")] Stream stream, string name)
        {
            using var blobStreamReader = new StreamReader(stream);
            var content = await blobStreamReader.ReadToEndAsync();
            _logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {content}");
        }
    }
}

If possible, this code would benefit from using the new primary constructors feature in C# 12:

using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public class BlobTriggerCSharp(ILogger<BlobTriggerCSharp> logger)
    {
        [Function(nameof(BlobTriggerCSharp))]
        public async Task Run([BlobTrigger("PathValue/{name}", Connection = "ConnectionValue")] Stream stream, string name)
        {
            using var blobStreamReader = new StreamReader(stream);
            var content = await blobStreamReader.ReadToEndAsync();
            logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {content}");
        }
    }
}

We could potentially reduce more nesting and code by using file-scoped namespaces as well to further reduce it to:

using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function;

public class BlobTriggerCSharp(ILogger<BlobTriggerCSharp> logger)
{
    [Function(nameof(BlobTriggerCSharp))]
    public async Task Run([BlobTrigger("PathValue/{name}", Connection = "ConnectionValue")] Stream stream, string name)
    {
        using var blobStreamReader = new StreamReader(stream);
        var content = await blobStreamReader.ReadToEndAsync();
        logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {content}");
    }
}
@captainsafia captainsafia changed the title Consider using C# primary constructors for generated trigger code Consider using modern C# features for generated triggers Oct 8, 2024
@captainsafia
Copy link
Author

Edit: We can further reduce the amount of code in the generated template by using global using to get rid of the using statements at the top.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant