Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.