Skip to content

Instantly share code, notes, and snippets.

View enRose's full-sized avatar
🎯
Focusing

Yini enRose

🎯
Focusing
  • Wellington, New Zealand
View GitHub Profile
public class ZooChecker
{
public string Check(Zoo zoo)
{
var checks = new List<string>
{
ValidateCats(zoo.Cats, zoo.facility),
ValidateFrogs(zoo.Frogs, zoo.facility),
ValidateDolphins(zoo.Dolphins, zoo.facility),
};
@enRose
enRose / ConditionalMadness.cs
Created March 1, 2020 23:38
Over complicated conditions
if (this code is written only once and never be changed again ||
((the developer wrote it can make changes fairly quick && never leaves the company ||
next developer can make changes fairly quick && never leaves the company) &&
has tests cover && easy to write new tests)
)
{
return "Not a problem";
}
else
{
@enRose
enRose / NonAsyncRuleEngine.cs
Last active July 21, 2020 00:35
Non async rule engine demo
using System.Collections.Generic;
using System.Linq;
namespace Barin.RuleEngine.NonAsyncEngine
{
public abstract class Rule<T>
{
public int Priority { get; set; }
public abstract bool IsValid(T ctx);
}
@enRose
enRose / CatRules.cs
Created March 1, 2020 23:44
Rules and Rule Collection demo on cats
using System;
using System.Collections.Generic;
using System.Linq;
using Barin.RuleEngine.Util;
namespace Barin.RuleEngine.NonAsyncRules
{
public class CatRuleCtx
{
public Guid Id { get; set; }
var ctx = new CatRuleCtx();
var catRules = new CatRules(ctx);
var result = catRules.FirstFails();
result == "Ok" ?
Console.WriteLine("Ok") :
Console.WriteLine("Name of failed rule: " + result);
switch (action.type) {
case actions.GET_CAT_REQUEST:
return { ...state, isFetching: true }
case actions.GET_CAT_SUCCESS:
return { ...state, isFetching: false, payload: action.payload }
case actions.GET_CAT_ERROR:
return { ...state, isFetching: false, error: action.error }
import * as actions from '../actions/cats'
const firstCat: ICat = {
isFetching: false,
error: undefined,
payload: undefined
}
export const cat = (state: ICat = firstCat, action: any) => {
// Computed property names (ES2015)
export function* requestCat() {
try {
const resp = yield call(catService.get)
yield put(actions.requestCatSuccess(resp))
} catch (err) {
yield put(actions.requestCatError(err))
}
}
export function* requestDog() {
import * as actions from '../actions'
import { catService } from '../catServices'
import { put, call, takeLatest } from 'redux-saga/effects'
const safe = (handler:any = null, saga:any, ...args:any) => function* (action:any) {
try {
yield call(saga, ...args, action)
} catch (err) {
yield call(handler, ...args, err)
}
import { createStore, applyMiddleware, compose } from 'redux'
import RootReducer from './reducers'
import createSagaMiddleware from 'redux-saga'
import rootSaga from './sagas'
//option 1
const sagaMiddleware = createSagaMiddleware({
onError: (err) => {
store.dispatch({ type: 'ERROR', payload: err })
}