Skip to content

Instantly share code, notes, and snippets.

@liammclennan
liammclennan / gist:1086996
Created July 17, 2011 01:06
Calling a JavaScript Module That Supports Dependency Injection
// this will cause module A to resolve its own dependencies
var a = require('A')();
console.log(a.action());
// or for testing we can provide the dependencies
var aTest = require('A')({
foo: function() { return 'foo'; }
}, {
@liammclennan
liammclennan / gist:1108480
Created July 27, 2011 01:31
NSubstitute Mocking Multiple calls to a generic method
// does not work
var service = Substitute.For<IService>();
service.Method(Arg.Any<DerivedType>()).Returns(1);
service.Method(Arg.Any<AnotherDerivedType>()).Returns(2);
// works
var service = Substitute.For<IService>();
service.Method(Arg.Is<BaseType>(a => a is DerivedType)).Returns(1);
@liammclennan
liammclennan / gist:1138915
Created August 11, 2011 04:38
Render A Collection of Partials
/// <summary>
/// Render a partial once for each item in a collection
/// </summary>
public static void RenderCollection(this HtmlHelper html, string partial, IEnumerable collection)
{
foreach (var thing in collection)
html.RenderPartial(partial, thing);
}
// usage: @{Html.RenderCollection("_MyPartial", Model.SomeCollection);}
@liammclennan
liammclennan / gist:1141027
Created August 11, 2011 23:06
ViewMap Asp.net mvc controller helper
public ActionResult ViewMap<TSource, TTarget>(TSource model, Action<TTarget> after)
{
if (model == null) return HttpNotFound();
TTarget mapped = Mapper.FindTypeMapFor<TSource, TTarget>() != null
? Mapper.Map<TSource, TTarget>(model)
: Mapper.DynamicMap<TSource, TTarget>(model);
after(mapped);
return View(mapped);
}
@liammclennan
liammclennan / gist:1141049
Created August 11, 2011 23:18
Action Method Using ViewMap
public ActionResult Show(Infrastructure infrastructure)
{
return ViewMap<Infrastructure, ShowInfrastructureModel>(infrastructure);
}
@liammclennan
liammclennan / gist:1199867
Created September 7, 2011 05:50
Most ridiculous way to build a month drop-down
var months = CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames.Take(12);
return months.Aggregate(new List<SelectListItem>(), (accumulator, month) =>
{
accumulator.Add(new SelectListItem{ Value = (accumulator.Count + 1).ToString(), Text = month });
return accumulator;
});
@liammclennan
liammclennan / gist:1241422
Created September 26, 2011 01:34
Nested usings
using (var stream = new MemoryStream())
using (var textWriter = new StreamWriter(stream))
using (var writer = new CsvWriter(textWriter))
{
}
@liammclennan
liammclennan / gist:1503890
Created December 21, 2011 00:05
Custom object-object mapping
public class MyThingMapper : EntityDtoMapper<Thing, ThingViewModel>
{
public MyThingMapper(IThingService service) : base(MappingDirections.Both)
{
...
}
protected override TEntity MapToEntity(TDto source, TEntity destination)
{
DoMapToEntity(source, destination);
@liammclennan
liammclennan / gist:1947078
Created March 1, 2012 03:38
What my backbone 'modules' look like
Home =
Router: Backbone.Router.extend
routes:
'': 'home'
home: ->
new Home.Views.Start()
@liammclennan
liammclennan / gist:1947242
Created March 1, 2012 04:19
test of a backbone view
describe 'home', ->
describe 'load', ->
before ->
App.TestHelper.reset()
it 'should render the home view', ->
new App.Modules.Home.Views.Start()
expect($('.btn:contains(Create a list)').length).to.be(1)