[TestClass]
public class MyTest
{
private FooBuilder fooBuilder;
[TestInitialize]
public void Setup()
{
SessionObjectCache.Remove(CachedEntries.USER_CONTEXT);
var registry = new MocksRegistry();
registry.BarRepositoryMock.Setup(repo =>
repo.GetAll()).Returns(new List<Bar>().AsQueryable());
registry.BazServiceMock.Setup(repo =>
repo.DoesMatchAnyBazForBars(It.IsAny<List<Bar>>()));
fooBuilder = new FooBuilder
{
BarRepository = registry.BarRepositoryMock.Object,
BazService = registry.BazServiceMock.Object
};
var testServices = new Dictionary<string, object>();
testServices[AppCtxIds.FOO_BUILDER] = fooBuilder;
ServiceLocator.LookupSource = () => testServices;
}
[TestMethod]
public void LetsTest()
{
var qux = BuildQux("Tom");
var userContext = SessionObjectCache.LazyGet<UserContext>
(CachedEntries.USER_CONTEXT,
new Func<UserContext>(() =>
{
return new UserContext { Qux = qux };
}));
Assert.IsTrue(SessionObjectCache.Exists(CachedEntries.USER_CONTEXT));
Assert.AreEqual("Tom", SessionObjectCache.Get<UserContext>
(CachedEntries.USER_CONTEXT).Qux.Name);
}
private Qux BuildQux(string whatever)
{
return fooBuilder.Build(whatever);
}
private class TestController : Controller
{
}
}
Tuesday, October 11, 2011
another mocking example
Sunday, October 9, 2011
Model Bind to a Collection
I'm trying to do something like this in this post in the name of figuring out how to model bind collections. My approach is more ghetto. I have three objects:
using System.Collections.Generic;
namespace MvcApplication.Models
{
public class Foo
{
public string Whatever { get; set; }
public List<Bar> SomeCollection { get; set; }
}
}
namespace MvcApplication.Models
{
public class Bar
{
public int Number { get; set; }
}
}
namespace MvcApplication.Models
{
public class Baz
{
public string Question { get; set; }
}
}
Alright, here is a view that uses a list of Baz as a its model and will bind to a Foo:
@using MvcApplication.Models
@model List<Baz>
@{
ViewBag.Title = "Home Page";
Int32 counter = 0;
}
<h2>@ViewBag.Message</h2>
<form method="post" action="/home/about/">
<input type="test" name="Whatever" />
@foreach(Baz baz in Model)
{
<br/>
<br/>
@baz.Question
<table>
<tr>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>
<input type="radio" name="SomeCollection[@counter].Number" value="0" />
</td>
<td>
<input type="radio" name="SomeCollection[@counter].Number" value="1" />
</td>
<td>
<input type="radio" name="SomeCollection[@counter].Number" value="2" />
</td>
<td>
<input type="radio" name="SomeCollection[@counter].Number" value="3" />
</td>
<td>
<input type="radio" name="SomeCollection[@counter].Number" value="4" />
</td>
<td>
<input type="radio" name="SomeCollection[@counter].Number" value="5" />
</td>
<td>
<input type="radio" name="SomeCollection[@counter].Number" value="6" />
</td>
</tr>
</table>
counter++;
}
<input type="submit" value="submit" />
</form>
Wednesday, October 5, 2011
does a key exist in a Dictionary or HashTable?
Check to see if a key exists in a Dictionary or, in this case, a HashTable:
if(myDictionary.ContainsKey("whatever")) {
Tuesday, October 4, 2011
what is an n+1 problem?
- If one has an object and the object has a collection of children...
- And, if one queries the number of children...
- And if one then runs a while (myCounter < numberOfChildren) loop, querying a child object for each of the passes through the loop...
- One will have n number of queries for n number of children...
- +1 query for the query to get the count (or is it the query to get the object to begin with? –I’m fuzzy on this.)
our two ways of mocking methods
We have two ways of mocking with MOQ for the two different approaches to inversion of control. For Dependency Injection:
IQueryable<Foo> foos = TestObjects.BuildFooCollection().AsQueryable();
mockFooRepository = new Mock<IFooRepository>();
mockFooRepository.Setup(repo => repo.GetAll()).Returns(foos);
fakeFooRepo = mockFooRepository.Object;
In the situation above, fakeFooRepo would be handed to a FooRepository getsetter on a controller in lieu allowing dependency inject to put a real FooRepository there. Alternatively... For Service Locator:
registry = new MocksRegistry();
ServiceLocator.LookupSource = () =>
{
return registry;
};
IQueryable<Bar> bars = TestObjects.BuildFooCollection().AsQueryable();
registry.BarRepositoryMock.Setup(repo => repo.GetAll()).Returns(bars);
Sunday, October 2, 2011
noob question: why should I care about delegates?
- In the art of functional programming, one can hand an anonymous delegate, as wrapped in a Func perhaps, to data (I envision a Func being handed to a method on a repository) in lieu of the usual thing in which an object is handed into a method. In the functional programming approach, the receiving method needs only know that it is to run a function of a certain <object,object> shape and nothing more. The functionality that the method fires is thus not shaped by the method itself and is instead shaped by what is handed in. Yay!
- The line of code that calls a method and the method itself are loosely coupled by way of a Func. You could put another Func "in front" of a Func too to chain them sequentially and then later detach the first Func as a something run by the second Func if you desire.
- One may spool up delegates and ultimately execute spools of delegates.
- Closures allow one to encapsulate some behavior and then pass it around like any other object. What is more, apparently state may be retained in closures! This is next for me to research.