Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created October 21, 2020 21:50
Show Gist options
  • Save dcomartin/f8908e2884bf74d29c2d8c31f4f7af13 to your computer and use it in GitHub Desktop.
Save dcomartin/f8908e2884bf74d29c2d8c31f4f7af13 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Threading.Tasks;
using Catalog.Contracts;
using Microsoft.EntityFrameworkCore;
namespace Catalog
{
public interface IProductQuery
{
Task<Product> GetProduct(Guid productId);
}
public class Product
{
public Guid Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class ProductQuery : IProductQuery
{
private readonly CatalogDbContext _dbContext;
public ProductQuery(CatalogDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<Product> GetProduct(Guid productId)
{
return await _dbContext.Products
.Where(x => x.Id == productId)
.Select(x => new Product
{
Id = x.Id,
Name = x.Name,
Price = x.Price
})
.SingleAsync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment