Skip to content

Instantly share code, notes, and snippets.

View bruceharrison1984's full-sized avatar
🔥

Bruce Harrison bruceharrison1984

🔥
View GitHub Profile
public class FunctionWrapper
{
private readonly ILogger _log;
public FunctionWrapper(ILogger<FunctionWrapper> log)
{
_log = log;
}
public async Task<IActionResult> Execute(Func<Task<IActionResult>> azureFunction)
builder.Services.AddTransient<FunctionWrapper>();
[FunctionName("GetWidget")]
public async Task<IActionResult> GetWidget([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "/widgets/{id}")] HttpRequest req, int id, ILogger log)
{
log.LogInformation($"getting widget: ${id}");
try
{
var selectedItem = await _dbContext.Widgets.FindAsync(id);
return new ObjectResult(selectedItem);
}
catch (Exception e)
public class Widgets
{
private readonly DbContext _DbContext;
private readonly FunctionWrapper _functionWrapper;
public Widgets(DbContext DbContext, FunctionWrapper functionWrapper)
{
_DbContext = DbContext;
_functionWrapper = functionWrapper;
}
using System;
namespace WidgetApi.Models
{
public abstract class ModelBase
{
public int Id { get; set; }
public int CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public int? UpdatedBy { get; set; }
using System.Net.Http;
using FluentValidation;
using WidgetApi.Models;
namespace WidgetApi.FunctionHelpers
{
public class BaseValidator<T> : AbstractValidator<T> where T : ModelBase
{
public BaseValidator()
{
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using FluentValidation;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.Logging;
using WidgetApi.Models;
@bruceharrison1984
bruceharrison1984 / response-envelope-example.cs
Last active September 5, 2019 03:58
Example of a C# Asp.net core response envelope
using System.Net;
using Microsoft.AspNetCore.Mvc;
namespace WidgetApi.Models
{
internal class ResponseEnvelopeResult<T> : ObjectResult where T : class
{
/// <summary>
/// Data envelope for wrapping responses
/// </summary>
public class KeyResults
{
private readonly WidgetDbContext _dbContext;
private readonly FunctionWrapper<Widget> _functionWrapper;
public Widgets(WidgetDbContext dbContext, FunctionWrapper<Widget> functionWrapper)
{
_dbContext = dbContext;
_functionWrapper = functionWrapper;
}
public override void Configure(IFunctionsHostBuilder builder)
{
...
builder.Services.AddLogging();
//workaround azure functions not handling nested arrays by default
builder.Services.AddTransient<IConfigureOptions<MvcOptions>, MvcJsonMvcOptionsSetup>();
builder.Services.AddTransient<BaseValidator<Widget>, WidgetValidator>(o => new WidgetValidator());
builder.Services.AddTransient<BaseValidator<User>, UserValidator>(o => new UserValidator());