This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserService | |
{ | |
private readonly DbContext _context; | |
public UserService(DbContext context) | |
{ | |
_context = context; | |
} | |
public Task<User> GetUser(int userId) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const detectChanges = changeDetector['_view']['detectChanges']; | |
let detect = 0; | |
let title = document.getElementsByTagName('title')[0] as HTMLTitleElement; | |
changeDetector['_view']['detectChanges'] = () => { | |
detect++; | |
const start = getTimer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |