Skip to content

Instantly share code, notes, and snippets.

View MarcoNicolodi's full-sized avatar

Marco Nicolodi MarcoNicolodi

View GitHub Profile
@MarcoNicolodi
MarcoNicolodi / EffectivenessAssessmentPending.cs
Last active January 22, 2019 10:58
Chain of responsibility
public class EffectivenessAssessmentPending : RuleHandler {
public override Label Run(Request request)
{
if(!request.HasEffectiveness)
{
return request.Approve ? Label.ApprovedOff : Label.ReprovedOff;
} else {
return base.Run(request);
}
@MarcoNicolodi
MarcoNicolodi / Result.cs
Last active January 22, 2019 11:10
Chain of responsibility
protected string GetClass(string code, bool aprove)
{
var request = new Request(code, approve, TrainingEvaluations, TrainningEffectiveness);
var chain = new CriteriaHasEvaluation();
chain
.SetNext(new EffectivenessAssessmentPending())
.SetNext(new NewItem())
.SetNext(new CriteriaEvaluated());
@MarcoNicolodi
MarcoNicolodi / Before.cs
Created January 19, 2019 16:32
Chain of responsibility
protected string GetClass(string code, bool approve)
{
var evaluation = TrainingEvaluations.Find(x => x.EmployeeCode == code.Split('_')[0] && x.CriteriaCode == code.Split('_')[1]);
var criteriaHasEvaluation = evaluation.Any();
var isNew = code.Split('_')[1] == IEntity.NewCode;
var effectiveness = TrainingEffectiveness.Find(x => x.EmployeeCode == code.Split('_')[0]);
var hasEffectiveness = effectiveness.Any();
if (hasEvaluation)
@MarcoNicolodi
MarcoNicolodi / GenericRuleHandler.cs
Created January 21, 2019 23:42
ChainOfResponsibility
public abstract class RuleHandler<TRequest, TReturn>
{
private RuleHandler<TRequest, TReturn> _nextHandler;
public RuleHandler<TRequest, TReturn> SetNext(RuleHandler<TRequest, TReturn> handler)
{
_nextHandler = handler;
return _nextHandler;
}
@MarcoNicolodi
MarcoNicolodi / EffectivenessAssessmentPending.cs
Last active January 22, 2019 01:08
Chain of responsibility
public class EffectivenessAssessmentPending : RuleHandler<Request,Label> {
public override Label Run(Request request)
{
if(!request.HasEffectiveness)
{
return request.Approve ? Label.ApprovedOff : Label.ReprovedOff;
} else {
return base.Run(request);
}
const chooseFile = async ({ directory, message }) => {
const candidateFiles = await filesystem.list(directory)
const promptConfig = {
name: 'chosenFile',
message,
choices: ['all', prompt.separator(), ...candidateFiles]
}
const { chosenFile } = await prompt.ask(promptConfig)
const chooseFile = async ({ directory, message, matching }) => {
let candidateFiles
if (matching) {
candidateFiles = await filesystem.find(directory, { matching })
} else {
candidateFiles = await filesystem.list(directory)
}
const promptConfig = {
name: 'chosenFile',
public abstract class FileChooser
{
protected abstract Task<IEnumerable<string>> Find(Request request);
public async Task<string> Choose(Request request)
{
var candidateFiles = await Find(request);
var chosenFile = await Prompt.Ask(new {
message = request.Message,
choices: candidateFiles
public class MatchingFinder : FileChooser
{
protected override async Task<IEnumerable<string>> Find(Request request)
=> await Filesystem.Find(request.Directory, request.Matching);
}
public class ListFinder : FileChooser
{
protected override async Task<IEnumerable<string>> Find(Request request)
=> await Filesystem.List(request.Directory);
if (matching) {
candidateFiles = await filesystem.find(directory, { matching })
} else {
candidateFiles = await filesystem.list(directory)
}