Check out Scott Hanselman's 2014 list of Windows developer and power user tools. Standouts for me, that I hadn't checked out before, include: PostSharp and NCrunch.
Monday, December 23, 2013
Thursday, November 14, 2013
Simple TypeMock Auto-Mocking container
TDD is a great methodology for simplifying your code and focusing on delivering business value. When doing TDD, we always want to strive to make it as friction-free as possible. This means making writing tests and, more importantly, refactoring as painless as we can. One of the ways we can do that is to use an Auto-Mocking container.
A very common reason for tests to change is because we added a new dependency to our SUT and we have to modify all of our previous tests to use this new dependency. One way to avoid this is to use an object that will build your SUT and inject all appropriate dependencies as mocked objects. So your tests go from looking like this:
to this:
We can now add another dependency without having to modify this test (assuming that this dependency is not used in this test).
Most of the popular mocking frameworks that are in use today (Moq, Rhino.Mocks, NSubstitute, etc) all have 1 or more Auto-Mocking frameworks that can be downloaded via NuGet. On our current project, we are using TypeMock, which is a very powerful, commercial grade, mocking tool. It can mock out almost anything and is used quite extensively in the SharePoint development world. There is no Auto-Mocking container for it, so I decided to write one.
An Auto-Mocking container needs to be able to do 2 things. Generate a SUT object with all dependencies mocked out and be able to retrieve the dependencies that were injected. TypeMock uses the following method to generate a mock object
Since it uses a static generic method, there is no way to call this method with a Type object without using reflection. So we will use reflection to call this method. We can get the MethodInfo object for this method in the following way:
and we would use it like so:
When creating our SUT we need to use reflection to get the types of the constructor parameters and use the method above to create them. In order to retrieve them at a later point we need to create a registry, which will simply be a Dictionary<Type, object>; In the case of multiple constructors we will pick the largest constructor. Here's what our CreateSut method looks like:
To get our generated mock objects we can grab them from our dictionary after our SUT has been created. The full source for the Automocker can be found on Github.
A very common reason for tests to change is because we added a new dependency to our SUT and we have to modify all of our previous tests to use this new dependency. One way to avoid this is to use an object that will build your SUT and inject all appropriate dependencies as mocked objects. So your tests go from looking like this:
dependency1 = new Mock<IDependency1>(); dependency2 = new Mock<IDependency2>(); sut = new MyClass(dependency1, dependency2);
to this:
mocker = new Automocker(); sut = mocker.CreateSut<MyClass>(); dependency1 = mocker.GetMock<IDependency1>(); dependency2 = mocker.GetMock<IDependency2>();
We can now add another dependency without having to modify this test (assuming that this dependency is not used in this test).
Most of the popular mocking frameworks that are in use today (Moq, Rhino.Mocks, NSubstitute, etc) all have 1 or more Auto-Mocking frameworks that can be downloaded via NuGet. On our current project, we are using TypeMock, which is a very powerful, commercial grade, mocking tool. It can mock out almost anything and is used quite extensively in the SharePoint development world. There is no Auto-Mocking container for it, so I decided to write one.
An Auto-Mocking container needs to be able to do 2 things. Generate a SUT object with all dependencies mocked out and be able to retrieve the dependencies that were injected. TypeMock uses the following method to generate a mock object
Isolate.Fake.Instance<IDependency>();
Since it uses a static generic method, there is no way to call this method with a Type object without using reflection. So we will use reflection to call this method. We can get the MethodInfo object for this method in the following way:
TypeMockInstance = Isolate.Fake.GetType() .GetMethods() .Where(m => m.Name == "Instance") .Select(m => new { Method = m, Params = m.GetParameters(), Args = m.GetGenericArguments() }) .Where(x => x.Params.Length == 0 && x.Args.Length == 1) .Select(x => x.Method) .First();
and we would use it like so:
TypeMockInstance.MakeGenericMethod(type).Invoke(Isolate.Fake, null);
When creating our SUT we need to use reflection to get the types of the constructor parameters and use the method above to create them. In order to retrieve them at a later point we need to create a registry, which will simply be a Dictionary<Type, object>; In the case of multiple constructors we will pick the largest constructor. Here's what our CreateSut method looks like:
public T CreateSut<T>() { var type = typeof (T); var constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); //get largest constructor ConstructorInfo largestConstructor = null; foreach (var constructor in constructors) { if (largestConstructor == null || largestConstructor.GetParameters().Length < constructor.GetParameters().Length) { largestConstructor = constructor; } } //generate mocks and build up parameter list var parameters = new List<object>(); foreach (var parameterInfo in largestConstructor.GetParameters()) { if (!_typeRegistry.ContainsKey(parameterInfo.ParameterType)) { _typeRegistry[parameterInfo.ParameterType] = GetMockInstance(parameterInfo.ParameterType); } parameters.Add(_typeRegistry[parameterInfo.ParameterType]); } return (T) largestConstructor.Invoke(parameters.ToArray()); }
To get our generated mock objects we can grab them from our dictionary after our SUT has been created. The full source for the Automocker can be found on Github.
Tuesday, June 18, 2013
Implementing an external service lookup cache
We have a form in our web application that takes an address. The field is an auto-complete field and as the user types their address, the form shows them matching addresses. There is a web service that is used in this organization to help with address lookup. This service takes a string (partial address) and returns a list of addresses that start with that particular string, up to a maximum of 1000 addresses. I wanted to implement a cache of this look up and it turned out to be an interesting little problem. How do we cache the addresses and more importantly, how can we figure out if we should hit the server or the cache?
An initial thought was that we could store all addresses from the service in a HashSet to avoid duplicates. We could check if there are any items in the cache that start with the search string and if so, we can return those items. Here's what this lookup class might look like.
This idea works, but consider a worst case performance issue of traversing a large collection of values, not finding the value and then making a call to the web service after all. Not to mention the issue of having an incomplete cache of values due to the web service only returning the top 1000 results. For an illustration of the issue consider a web service that only returns the top 2 results.
To solve this issue I decided in addition to caching the address list, I would also cache the searches. This will fix both issues, the first by improving cache check performance, the second by returning a better result set from the cache. Below is the implementation:
The next potential for improvement would be to implement a rolling cache. One that only keeps the n most recent searches. I haven't taken this implementation that far, but a potential implementation might be to associate the search results on a per search basis in a dictionary, rather than keeping a global address list. Let me know how you might implement a rolling cache or if you have any improvement suggestions with my implementation.
An initial thought was that we could store all addresses from the service in a HashSet to avoid duplicates. We could check if there are any items in the cache that start with the search string and if so, we can return those items. Here's what this lookup class might look like.
public class AddressLookup { public static AddressLookup Instance { get; private set; } private ISet<AddressType> Addresses { get { lock (this) { if (HttpRuntime.Cache["Addresses"] == null) { HttpRuntime.Cache["Addresses"] = new HashSet<AddressType>(); } return (ISet<AddressType>)HttpRuntime.Cache["Addresses"]; } } } static AddressLookup() { Instance = new AddressLookup(); } public IEnumerable<AddressType> GetByPartialAddress(string searchAddress) { lock (this) { searchAddress = searchAddress.ToUpper(); var cachedAddresses = Addresses.Where(address => address.FormattedAddress.StartsWith(searchAddress, StringComparison.InvariantCultureIgnoreCase)).ToArray(); if (cachedAddresses.Length > 0) { return cachedAddresses; } var addresses = new AddressManager().RetrieveByPartialAddress(searchAddress); Addresses.UnionWith(addresses); return addresses; } } }
This idea works, but consider a worst case performance issue of traversing a large collection of values, not finding the value and then making a call to the web service after all. Not to mention the issue of having an incomplete cache of values due to the web service only returning the top 1000 results. For an illustration of the issue consider a web service that only returns the top 2 results.
Total data set | 1, 12, 122 |
Uncached results for search string "1" | 1, 12 |
Uncached results for search string "12" | 12, 122 |
Cached results for search string "12" after searching string "1" | 12 |
To solve this issue I decided in addition to caching the address list, I would also cache the searches. This will fix both issues, the first by improving cache check performance, the second by returning a better result set from the cache. Below is the implementation:
public class AddressLookup { public static AddressLookup Instance { get; private set; } private ISet<string> Searches { get { lock (this) { if (HttpRuntime.Cache["AddressSearches"] == null) { HttpRuntime.Cache["AddressSearches"] = new HashSet<string>(); } return (ISet<string>)HttpRuntime.Cache["AddressSearches"]; } } } private ISet<AddressType> Addresses { get { lock (this) { if (HttpRuntime.Cache["Addresses"] == null) { HttpRuntime.Cache["Addresses"] = new HashSet<AddressType>(); } return (ISet<AddressType>)HttpRuntime.Cache["Addresses"]; } } } static AddressLookup() { Instance = new AddressLookup(); } public IEnumerable<AddressType> GetByPartialAddress(string searchAddress) { lock (this) { searchAddress = searchAddress.ToUpper(); if (Searches.Contains(searchAddress)) { return Addresses.Where(address => address.FormattedAddress.StartsWith(searchAddress, StringComparison.InvariantCultureIgnoreCase)); } Searches.Add(searchAddress); var addresses = new AddressManager().RetrieveByPartialAddress(searchAddress); Addresses.UnionWith(addresses); return addresses; } } }
The next potential for improvement would be to implement a rolling cache. One that only keeps the n most recent searches. I haven't taken this implementation that far, but a potential implementation might be to associate the search results on a per search basis in a dictionary, rather than keeping a global address list. Let me know how you might implement a rolling cache or if you have any improvement suggestions with my implementation.
Labels:
.NET,
ASP.NET,
design pattern
Thursday, February 28, 2013
How I feel as a developer
Here's a blog with a bunch of animated gifs illustrating what it's like to be a developer.
Labels:
Funny
Subscribe to:
Posts (Atom)