Skip to content

Update additional templates to use annotations #1287

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

Merged
merged 6 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
{
"display-name": "Annotations Framework",
"display-name": "Annotations Framework Sample",
"system-name": "Annotations",
"description": "(Preview) Use the .NET Lambda Annotations framework to write Lambda Functions.",
"description": "Sample application demonstrating how to use the Lambda Annotations framework.",
"sort-order": 120,
"hidden-tags": [
"C#",
"ServerlessProject"
],
"tags": [
"Container",
"Annotations",
"Preview"
"Annotations"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"Lambda",
"Serverless"
],
"name": "Lambda Annotations Framework (Preview)",
"name": "Lambda Annotations Framework Sample",
"identity": "AWS.Lambda.Serverless.Annotations.CSharp",
"groupIdentity": "AWS.Lambda.Serverless.Annotations",
"shortName": "serverless.Annotations",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
<PackageReference Include="Amazon.Lambda.Annotations" Version="0.13.2" />
<PackageReference Include="Amazon.Lambda.Annotations" Version="1.0.0" />
</ItemGroup>
<!--
The FrameworkReference is used to reduce the deployment bundle size by not having to include
dependencies like Microsoft.Extensions.DependencyInjection. The Microsoft.AspNetCore.App
which is available in the Managed .NET Lambda runtime already includes those assemblies.
-->
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace BlueprintBaseName._1;

/// <summary>
/// The implementation of <see cref="ICalculatorService"/>
/// that will be used by our Lambda functions.
/// </summary>
public class CalculatorService : ICalculatorService
{
/// <inheritdoc/>
public int Add(int x, int y)
{
return x + y;
}

/// <inheritdoc/>
public int Subtract(int x, int y)
{
return x - y;
}

/// <inheritdoc/>
public int Multiply(int x, int y)
{
return x * y;
}

/// <inheritdoc/>
public int Divide(int x, int y)
{
return x / y;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace BlueprintBaseName._1;

/// <summary>
/// An interface for a service that implements the business logic of our Lambda functions
/// </summary>
public interface ICalculatorService
{
/// <summary>
/// Adds x and y together
/// </summary>
/// <param name="x">Addend</param>
/// <param name="y">Addend</param>
/// <returns>Sum of x and y</returns>
int Add(int x, int y);

/// <summary>
/// Subtracts y from x
/// </summary>
/// <param name="x">Minuend</param>
/// <param name="y">Subtrahend</param>
/// <returns>x - y</returns>
int Subtract(int x, int y);

/// <summary>
/// Multiplies x and y
/// </summary>
/// <param name="x">Multiplicand</param>
/// <param name="y">Multiplier</param>
/// <returns>x * y</returns>
int Multiply(int x, int y);

/// <summary>
/// Divides x by y
/// </summary>
/// <param name="x">Dividend</param>
/// <param name="y">Divisor</param>
/// <returns>x / y</returns>
int Divide(int x, int y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ namespace BlueprintBaseName._1;
/// </summary>
public class Functions
{
private ICalculatorService _calculatorService;

/// <summary>
/// Default constructor.
/// </summary>
public Functions()
/// <remarks>
/// The <see cref="ICalculatorService"/> implementation that we
/// instantiated in <see cref="Startup"/> will be injected here.
///
/// As an alternative, a dependency could be injected into each
/// Lambda function handler via the [FromServices] attribute.
/// </remarks>
public Functions(ICalculatorService calculatorService)
{
_calculatorService = calculatorService;
}

/// <summary>
Expand Down Expand Up @@ -46,8 +56,10 @@ public string Default()
[HttpApi(LambdaHttpMethod.Get, "/add/{x}/{y}")]
public int Add(int x, int y, ILambdaContext context)
{
context.Logger.LogInformation($"{x} plus {y} is {x + y}");
return x + y;
var sum = _calculatorService.Add(x, y);

context.Logger.LogInformation($"{x} plus {y} is {sum}");
return sum;
}

/// <summary>
Expand All @@ -60,8 +72,10 @@ public int Add(int x, int y, ILambdaContext context)
[HttpApi(LambdaHttpMethod.Get, "/subtract/{x}/{y}")]
public int Subtract(int x, int y, ILambdaContext context)
{
context.Logger.LogInformation($"{x} subtract {y} is {x - y}");
return x - y;
var difference = _calculatorService.Subtract(x, y);

context.Logger.LogInformation($"{x} subtract {y} is {difference}");
return difference;
}

/// <summary>
Expand All @@ -74,8 +88,10 @@ public int Subtract(int x, int y, ILambdaContext context)
[HttpApi(LambdaHttpMethod.Get, "/multiply/{x}/{y}")]
public int Multiply(int x, int y, ILambdaContext context)
{
context.Logger.LogInformation($"{x} multiply {y} is {x * y}");
return x * y;
var product = _calculatorService.Multiply(x, y);

context.Logger.LogInformation($"{x} multiplied by {y} is {product}");
return product;
}

/// <summary>
Expand All @@ -88,7 +104,9 @@ public int Multiply(int x, int y, ILambdaContext context)
[HttpApi(LambdaHttpMethod.Get, "/divide/{x}/{y}")]
public int Divide(int x, int y, ILambdaContext context)
{
context.Logger.LogInformation($"{x} divide {y} is {x / y}");
return x / y;
var quotient = _calculatorService.Divide(x, y);

context.Logger.LogInformation($"{x} divided by {y} is {quotient}");
return quotient;
}
}
Loading