Skip to content

Instantly share code, notes, and snippets.

View JeremySkinner's full-sized avatar

Jeremy Skinner JeremySkinner

View GitHub Profile
@JeremySkinner
JeremySkinner / Microsoft.Powershell_Profile.ps1
Created January 25, 2012 23:32
Custom Powershell profile that uses both posh-git and posh-hg
$myDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Import-Module "$mydir\posh-hg\posh-hg"
Import-Module "$mydir\posh-git\posh-git"
function prompt {
write-host "$pwd" -NoNewLine -foregroundcolor green
$Global:GitStatus = Get-GitStatus
Write-GitStatus $GitStatus
Write-HgStatus
@JeremySkinner
JeremySkinner / ConditionalCollectionValidation.cs
Created April 28, 2011 14:15
FluentValidation syntax for conditional validation of collection elements
public class TestValidator : AbstractValidator<Customer> {
public TestValidator() {
RuleFor(cust => cust.Orders)
.SetCollectionValidator(new OrderValidator())
.IncludeWhere(order => order.Amount > 50);
//OR
RuleFor(cust => cust.Orders)
.SetCollectionValidator(new OrderValidator(), includeWhere: order => order.Amount > 50);
<table>
<tbody data-bind="template: 'MyTemplate'></tbody>
</table>
<script id="MyTemplate" type="x-jquery-tmpl">
{{each(i, row) Rows}}
<tr>
<td>
<select data-bind="options: row.Options, optionsText: 'Text', optionsValue: 'Value'"></select>
</td>
@JeremySkinner
JeremySkinner / 01_Demo.cs
Created January 23, 2011 21:21
A little strongly-typed wrapper around WebMatrix.Data - this is now its own project at https://github.com/JeremySkinner/WebMatrix.Data.StronglyTyped
// WebMatrix.Data's Query method returns an IEnumerable<dynamic>
// While this is great for simple scenarios, if you want to add behaviour to your DB objects
// then it can be a pain. Here's a simple wrapper that adds a strongly-typed Query method
// This is a simple implementation and could be improved.
// Represents a table in the database
public class User {
public int Id { get; set; }
public string Name { get; set; }
}
protected override bool RenderHeader() {
base.RenderHeader();
return !IsDataSourceEmpty();
}
protected override bool ShouldRenderHeader() {
return true;
}
public interface ICommandHandler<TCommand> {
void Handle(TCommand command);
}
public interface IDispatcher {
void Invoke<TCommand>(TCommand command);
}
public class Dispatcher : IDispatcher {
public void Invoke<TCommand>(TCommand command) {
//MyScript.cs
public class MyScript : Phantom.Core.BuildScript {
Target @default =() => {
System.Console.WriteLine("foo");
};
}
phantom.exe -f:path/to/MyScript.cs
// runs the "default" target from myscript.cs
public class PeopleGridModel : GridModel<Person> {
public PeopleGridModel(HtmlHelper html) {
Column.For(x => html.ActionLink("View Details", "Show", new{id = x.Id}));
}
}
<%= Html.Grid(Model).WithModel(new PeopleGridModel(Html)) %>
// Should allow for this syntax:
// RuleFor(x => x.Surname).NotNull().WithLocalizedMessage(() => MyResources.SomeResource, x => "foo", x => "bar");
// ...where MyResources.SomeResource has format placeholders
public static IRuleBuilderOptions<T, TProperty> WithLocalizedMessage<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, Expression<Func<string>> resourceSelector, params Func<T, object>[] funcs) {
return rule.Configure(config => {
config.CurrentValidator.ErrorMessageSource = LocalizedStringSource.CreateFromExpression(resourceSelector, new StaticResourceAccessorBuilder());
funcs
.Select(func => new Func<object, object>(x => func((T)x)))
<%= Html.Grid(Model).Columns(column => {
column.For(x => "<a href='" + Url.Action("Edit") + "'><img src='/content/images/edit.jpg' /></a>").Encode(false);
}) %>