Skip to content

Instantly share code, notes, and snippets.

@dcomartin

dcomartin/ProductQuery.cs Secret

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