Skip to content

Instantly share code, notes, and snippets.

using Common;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Day_15_Solver
{
public static class Day15Solver
{
public static long Part1Solution(long[] input)
public interface IRepository<TEntity> where TEntity : class
{
TEntity FindById(int id);
IEnumerable<TEntity> Find(ISpecification<TEntity> specification = null);
void Add(TEntity entity);
void AddRange(IEnumerable<TEntity> entities);
void Remove(TEntity entity);
public class SpecificationEvaluator<TEntity> where TEntity : BaseEntity
{
public static IQueryable<TEntity> GetQuery(IQueryable<TEntity> inputQuery, ISpecification<TEntity> specification)
{
var query = inputQuery;
// modify the IQueryable using the specification's criteria expression
if (specification.Criteria != null)
{
query = query.Where(specification.Criteria);
public class UsersController : ControllerBase
{
private readonly IUnitOfWork _unitOfWork;
public UsersController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
// GET: api/Users
public class UsersWithRecipesAndIngredientsSpecification : BaseSpecification<User>
{
public UsersWithRecipesAndIngredientsSpecification() : base()
{
AddInclude(x => x.Recipes);
AddInclude($"{nameof(User.Recipes)}.{nameof(Recipe.Ingredients)}");
}
public UsersWithRecipesAndIngredientsSpecification(int id) : base(x => x.Id == id)
{
public interface IUnitOfWork : IDisposable
{
IRepository<TEntity> Repository<TEntity>() where TEntity : BaseEntity;
int Complete();
}
public class UnitOfWork : IUnitOfWork
{
private readonly ApplicationDbContext _context;
private Hashtable _repositories;
public class Repository<TEntity> : IRepository<TEntity> where TEntity : BaseEntity
{
protected readonly DbContext _context;
public Repository(DbContext context)
{
_context = context;
}
public void Add(TEntity entity)
public abstract class BaseSpecification<T> : ISpecification<T>
{
protected BaseSpecification(Expression<Func<T, bool>> criteria)
{
Criteria = criteria;
}
protected BaseSpecification()
{
}
public interface ISpecification<T>
{
Expression<Func<T, bool>> Criteria { get; }
List<Expression<Func<T, object>>> Includes { get; }
List<string> IncludeStrings { get; }
Expression<Func<T, object>> OrderBy { get; }
Expression<Func<T, object>> OrderByDescending { get; }
Expression<Func<T, object>> GroupBy { get; }
int Take { get; }
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly ApplicationDbContext _context;
public UsersController(ApplicationDbContext context)
{
_context = context;
}