Kralizek / AWSLambdaSharpTemplate

A template for AWS Lambda functions written in C# with .NET Core
MIT License
61 stars 25 forks source link

Use one base function for all Event Handlers #9

Closed AmmarBashir closed 5 years ago

AmmarBashir commented 5 years ago

Hello Kralizek, Thanks for your awesome templates and package, I have been thinking is there any way to implement one base function to be used by all event handlers to control the configuration from just one place, if we have multiple functions and every function needs Configure, ConfigureServices, ConfigureLogging, I don't want to repeat this same code for all functions? Can you suggest a workaround? That would be very appreciated.

   public class Function<T> : EventFunction<T>
{

    protected override void Configure(IConfigurationBuilder builder)
    {
        builder.AddEnvironmentVariables()
               .AddJsonFile("appsettings.json");
    }

    protected override void ConfigureLogging(ILoggerFactory loggerFactory, IExecutionEnvironment executionEnvironment)
    {
        /*loggerFactory.AddLambdaLogger(new LambdaLoggerOptions
        {
            IncludeCategory = true,
            IncludeLogLevel = true,
            IncludeNewline = true
        });*/
    }

    protected override void ConfigureServices(IServiceCollection services)
    {
        services.RegisterIoC();
        RegisterHandler<PremierEventHandler<T>>(services);
    }
}

Thanks again, Ammar

Kralizek commented 5 years ago

Hi @AmmarBashir,

Thank you for posting here.

I'm not sure I understand your use case: this library aims at a speeding up the creation of Lambda functions but there still is a strong 1:1 connection between projects and functions.

If you really wanted to pack more than one function in the same project, you can still do it with some manual work but I don't think it should be something that this library and its templates should support natively.

Please feel free to add more insights if you feel that I had missed something!

AmmarBashir commented 5 years ago

Thank you @Kralizek for your insights. I have refactored to create BaseFunction class that has all Configure methods and it is inherited by other Function class, If I need other functionality in any override methods and I can simply do so in child function class.

Kralizek commented 5 years ago

I'd gladly see what you came up with. Would you mind posting a sample in a gist?

AmmarBashir commented 5 years ago

Yes I would love to, here it is: https://gist.github.com/AmmarBashir/107cbcc59897744e120e1f492383cfa2

I'd also like to have your opinion. Any ideas would be really appreciated.

Thanks