Skip to content

Instantly share code, notes, and snippets.

@DaveHogan
DaveHogan / DateTimeExtensions.cs
Last active December 22, 2015 00:39
Replacing DateTime.Now method to assist with time sensitive unit tests
// Following on from: http://stackoverflow.com/q/7661953/235644
//
// Observations:
// Should consider using UTC for better globalization support
//
// Usage:
DateTimeExtensions.SetNowFunction(() => date1);
/// <summary>
/// Allow custom generation of Now times.
@DaveHogan
DaveHogan / gist:6421973
Created September 3, 2013 10:05
How to select from different tables in a VIEW based on a config value / IF condition
-- Sneaky way to select from a specific table using a config value
-- Views do not support IF or anything else useful
-- Obviously the table schemas need to match otherwise the UNION will fail
SELECT t.* FROM ATable t
INNER JOIN Config c on 'ConfigKeyToMatch' = c.ConfigKey
WHERE c.ConfigValue = 'True'
UNION
SELECT t.* FROM DifferentTable t
@DaveHogan
DaveHogan / gist:6542937
Created September 12, 2013 19:53
Casting IPrincipal (HttpContext.Current.User) to IMyPrincipal using Ninject binding
kernel.Bind<IMyPrincipal>().ToMethod(c => Convert(HttpContext.Current.User));
private static IMyPrincipal Convert(IPrincipal principal)
{
if (principal is Common.CustomPrincipal)
return (Common.CustomPrincipal)principal;
else
return new Common.AnonymousPrincipal(principal);
}
@DaveHogan
DaveHogan / MustBeEmptyAttribute.cs
Last active September 25, 2017 00:08
MVC ValidationAttribute to ensure property is empty. Sounds strange but I create for an anti-bot field to which is hidden in css but bots still try filling in.
public class MustBeEmptyAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
if (value is string)
{
@DaveHogan
DaveHogan / HtmlHelperExtensions.cs
Created September 13, 2013 22:03
MVC Html helper class that will render an table row
public static class HtmlHelperExtensions
{
public static MvcHtmlString EmptyRow(this HtmlHelper helper, string emptyTextMessage, int rowCount)
{
return new MvcHtmlString(string.Format("<tr class=\"emptyRow\"><td colspan=\"{0}\">{1}</td></tr>", rowCount, emptyTextMessage));
}
}
@DaveHogan
DaveHogan / gist:6715348
Created September 26, 2013 14:54
Install the full ruby with rails stack from terminal window
\curl -L https://get.rvm.io | bash -s stable --autolibs=3 --rails
@DaveHogan
DaveHogan / gist:8096726
Created December 23, 2013 12:57
Useful technique for skipping a PAUSE at the end of a batch file.
if (%1)==(NOPAUSE) goto :eof
pause
@DaveHogan
DaveHogan / repeatString-fsharp-recursion-example
Last active January 2, 2016 20:29
FSharp recursion example - repeatString
let rec repeatString stringToRepeat numberOfTimesToRepeat =
if numberOfTimesToRepeat = 0 then ""
else
stringToRepeat + (repeatString stringToRepeat (numberOfTimesToRepeat - 1))

Bundling & Minification

Bring in LigerShark.WebOptimizer.Core nuget package

_viewImports file must contain the WebOptimizer TagHelpers

Include the following tag into _viewImports.cshtml file

@addTagHelper *, WebOptimizer.Core

this enables the cachebuster