Skip to content

Instantly share code, notes, and snippets.

View mvalipour's full-sized avatar
🚴‍♂️
Cycling...

| Mo Valipour mvalipour

🚴‍♂️
Cycling...
View GitHub Profile
@mvalipour
mvalipour / @about.md
Last active January 13, 2018 12:09
ASP.NET MVC client-side `Redirect` methods and `ActionResult`

ASP.NET MVC client-side Redirect methods and ActionResult

A little background

When in need of redirecting the user on client-side (e.g. when in ajax actions), Most of the developers choose the easy way and just return a JavascriptResult('window.location = ...') and passing to it Url.Action(...).

But what is wrong with this?

  1. Testability: There are several ways that the above approach makes unit-testing difficult (if not impossible).
@mvalipour
mvalipour / install-hubflow.cmd
Last active September 8, 2018 08:36
Install HubFlow on windows
@echo off
setlocal
rem ===================
rem Set GIT_HOME
rem ===================
rem Read the Git for Windows installation path from the Registry.
:REG_QUERY
@echo off
setlocal
rem Read the Git for Windows installation path from the Registry.
:REG_QUERY
for /f "skip=2 delims=: tokens=1*" %%a in ('reg query "HKLM\SOFTWARE%WOW%\Microsoft\Windows\CurrentVersion\Uninstall\Git_is1" /v InstallLocation 2^> nul') do (
for /f "tokens=3" %%z in ("%%a") do (
set GIT=%%z:%%b
)
@mvalipour
mvalipour / 2015-02-03-azure-worker-role-health.md
Created February 3, 2015 23:11
Add a heartbeat end-point to an azure worker role

Some background...

For one of my projects, I have a worker role on azure that performs some action in the background. In fact there are several of them that work in an orchestration together.

While building these workers, I noticed that I'm going to need one way to find out whether the worker is OK in the background and possibly get some very abstract information out of it. Having such a health monitorning is vital for a system which is composed of many background workers.

So the main question is:

How can we add a minimalistic http end-point to a worker

@mvalipour
mvalipour / 2015-02-28-angular-node-oauth-jwt.md
Last active August 29, 2015 14:16
Building an angular/node app that is secured by OAuth and JWT

So I recently started building an app using angular and node (express). To avoid any session memory and truly scalable and segregated API, I decided to build a token-based authentication for my app.

So what is JWT?

As defined on their website:

JSON Web Token (JWT) is a compact URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is digitally signed using JSON Web Signature (JWS).

What I found interesting about JWT is that:

@mvalipour
mvalipour / readme.md
Last active August 29, 2015 14:27
onetime time shortcut

Save the following script in a file called ot.cmd (replace ot with any shortcut name you wish to use) and place it in a folder -- say c:\temp\onetime-shortcut

onetime time %*

run the following command to add this path to your windows PATH:

@mvalipour
mvalipour / readme.md
Last active September 22, 2015 12:48
Upgrade DNX environment to beta7
  • Install VS 2015 "beta7 tooling" from here. -- it only works in IE for me!
  • Close all visual studios
  • Install beta-7
dnvm upgrade-self
dnvm install latest
dnvm alias default 1.0.0-beta7
dnvm use default -p
@mvalipour
mvalipour / readme.md
Created September 23, 2015 10:42
ASP.NET 5 tricks!

If you have Oops! with no details, do this:

<add key="ASPNET_DETAILED_ERRORS" value="true" />
@mvalipour
mvalipour / IQueryable<>.WhereIdIsAnyOf.cs
Last active December 22, 2015 10:08
Run ID search on an EntityFramework query in a way that results in parameterized sql `IN` clause
public static IQueryable<T> WhereIdIsAnyOf<T>(this IQueryable<T> source, int[] ids)
{
var parameter = Expression.Parameter(typeof(T));
Expression expression = null;
foreach(var value in ids)
{
// NOTE: here instead of refering to the id directly (via Expression.Constant)
// an intermediary object is used to refer to the value
// to avoid compile-time constant folding
@mvalipour
mvalipour / IEnumerable<>.Slice.cs
Created December 22, 2015 10:08
Slice a collection into chunks with optionally being able to make all chunks fixed size and provide default value for paddings
public static IEnumerable<IEnumerable<T>> Slice<T>(this IEnumerable<T> items, int size, bool fixedSize = false, T defaultValue = default(T))
{
var start = 0;
items = items.ToArray();
var total = items.Count();
while (start < total)
{
var res = items.Skip(start).Take(size).ToList();
while (res.Count < size)
{