Skip to content

Instantly share code, notes, and snippets.

@tonysneed
Created December 21, 2018 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonysneed/3e884440afc8602ce047d8ff0f780bf4 to your computer and use it in GitHub Desktop.
Save tonysneed/3e884440afc8602ce047d8ff0f780bf4 to your computer and use it in GitHub Desktop.
public class Function
{
// Repository
public IProductRepository ProductRepository { get; }
public Function()
{
// Get dependency resolver
var resolver = new DependencyResolver(ConfigureServices);
// Get products repo
ProductRepository = resolver.ServiceProvider.GetService<IProductRepository>();
}
// Use this ctor from unit tests that can mock IProductRepository
public Function(IProductRepository productRepository)
{
ProductRepository = productRepository;
}
/// <summary>
/// A simple function that takes an id and returns a product.
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public async Task<Product> FunctionHandler(string input, ILambdaContext context)
{
int.TryParse(input, out var id);
if (id == 0) return null;
return await ProductRepository.GetProduct(id);
}
// Register services with DI system
private void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IProductRepository, ProductRepository>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment