Skip to content

Instantly share code, notes, and snippets.

@kemsky
kemsky / UseCase.cs
Created July 4, 2023 16:52
[Medium] Use case
public static class Specifications
{
// create specification from expression, delegate is compiled on first usage
public static readonly Specification<Department> ActiveDepartment = new(x => x.Active);
// create specification from delegate, expression is decompiled from delegate on first usage
public static readonly Specification<User> ActiveUser = new(
default,
x => x.Active
);
@kemsky
kemsky / SpecificationInterface.cs
Last active July 4, 2023 16:42
[Medium] Specification interface
// implenation details omitted intentionally
public sealed class Specification<TSource>
{
// for expression based APIs
public Expression<Func<TSource, bool>> IsSatisfiedByExpression { get; }
// for regular code
public Func<TSource, bool> IsSatisfiedBy { get; }
@kemsky
kemsky / NestedSpecificationProblem.cs
Created July 4, 2023 15:59
[Medium] nested specifications problem
public sealed class ActiveDepartmentSpecification : LinqSpecification<Department>
{
public override Expression<Func<Department, bool>> AsExpression() => x => x.Active;
}
public sealed class UserWithActiveDepartmentSpecification : LinqSpecification<User>
{
// see, we can't reuse ActiveDepartmentSpecification here:
public override Expression<Func<User, bool>> AsExpression() => x => x.Departments.Any(z => z.Active);
}
@kemsky
kemsky / CanonicalSpecification.cs
Last active July 4, 2023 17:10
[Medium] Canonical specification implementation
// source: https://en.wikipedia.org/wiki/Specification_pattern
// see full implementation there
public interface ISpecification<T>
{
bool IsSatisfiedBy(T candidate);
ISpecification<T> And(ISpecification<T> other);
ISpecification<T> AndNot(ISpecification<T> other);
ISpecification<T> Or(ISpecification<T> other);
ISpecification<T> OrNot(ISpecification<T> other);
@kemsky
kemsky / Service.cs
Last active July 4, 2023 17:09
[Medium] DRY violation
public class UserService
{
private readonly DbContext _context;
public UserService(DbContext context)
{
_context = context;
}
public Task<User> GetUser(int userId)
@kemsky
kemsky / Entities.cs
Last active July 4, 2023 15:27
[Medium] Entities
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
public Subscription Subscription { get; set; }
public List<Department> Departments { get; set; }
}
public class Department
@kemsky
kemsky / projections.cs
Last active April 1, 2023 05:17
Remaining bits
public static class ExpressionExtensions
{
public static Option<object, Expression> TryEvaluate(this Expression expression)
{
if (expression is MemberExpression memberExpression)
{
if (memberExpression.Expression == null)
{
if (memberExpression.Member is PropertyInfo staticProperty)
{
@kemsky
kemsky / exceljs.html
Created October 13, 2018 14:59
ExcelJS example
<html>
<head>
<title>exceljs sample</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/exceljs/1.6.2/exceljs.js"></script>
<script>
function save(blob, fileName) {
var a = document.createElement("a");
document.body.appendChild(a);
@kemsky
kemsky / time.ts
Created March 1, 2017 13:14
Measure and report Angular 2 change detection time
const detectChanges = changeDetector['_view']['detectChanges'];
let detect = 0;
let title = document.getElementsByTagName('title')[0] as HTMLTitleElement;
changeDetector['_view']['detectChanges'] = () => {
detect++;
const start = getTimer();
@kemsky
kemsky / sort.ts
Created January 21, 2017 22:26
Sort Example, TypeScript
export interface ISortOptions<T>
{
ignoreCase?:boolean;
projection?:(item:T) => any;
property?:string;
order?:boolean;
optimize?:boolean;
}
export function lense<T, U>(property:string):(item:T) => U