Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

Making IEnumerable Default Comparer work with your own types November 26, 2009

Filed under: C#,Linq — Rupert Bates @ 11:22 am

As part of the Open Platform client library I have created a type for the tags that are returned by the api:

	//Attributes omitted for brevity
	public class Tag
	{
		public string Name { get; set; }
		public string Type { get; set; }
		public string Filter { get; set; }
		public string ApiUrl { get; set; }
		public string WebUrl { get; set; }
	}

The Filter property acts like a primary key so if two Tag objects have the same Filter then they should be considered equal.
This test describes the behaviour I want:

		[Test]
		public void Test_tag_equality()
		{
			Tag t1 = new Tag { Filter = "/Rupert" };
			Tag t2 = new Tag { Filter = "/Rupert" };
			Tag t3 = new Tag { Filter = "/Harry" };
			Tag t4 = t3;
			Assert.AreEqual(t1, t2);
			Assert.AreNotEqual(t1, t3);
			Assert.AreNotSame(t1, t2);
			Assert.AreSame(t3, t4);
		}

So I overrode Equals

        public override bool Equals(object obj)
        {     
            return Filter == ((Tag) obj).Filter;
        }

and my test passed.
However later on I wanted to use IEnumerable.Intersection() to find all the elements from one list that were also in another list as follows:

//Get the tags which match with music types
            var musicTypes = new[]
			{
				new Tag{Filter="/music/popandrock"}, 
                new Tag{Filter="/music/classicalmusicandopera"}, 
                new Tag{Filter="/music/electronicmusic"}, 
                new Tag{Filter="/music/urban"},
                new Tag{Filter="/music/folk"},
                new Tag{Filter="/music/worldmusic"}
            };

            //filter the results to get the ones which are tagged with the musicType tags
            var filtered = results.Results.Where(c => c.TaggedWith.Intersect(musicTypes).Count() > 0);

but my intersection always returned 0 matches, in other words the way that Intersect() was comparing the equality of the elements of my IEnumerable was not using my Equals() override (or not exclusively anyway). There is an overload for this function that takes a custom EqualityComparer, but I didn’t want to use that.
I investigated a bit more and found that IEnumerable uses the IEquatable interface to determine whether elements are equal so I implemented this and thought that would be that:

	public class Tag : IEquatable<Tag>
	{
		...

		#region IEquatable<Tag> Members

		public bool Equals(Tag other)
		{
			return Filter == other.Filter;
		}

		#endregion
	}

but my intersection still failed to return any results.
After quite a bit of head scratching I tried implementing GetHashCode() as well and finally it worked

	//The final implementation
	public class Tag : IEquatable<Tag>
	{
		...

		public override bool Equals(object obj)
		{
			return Filter == ((Tag)obj).Filter;
		}
		public override int GetHashCode()
		{
			return Filter.GetHashCode();
		}
		#region IEquatable<Tag> Members

		public bool Equals(Tag other)
		{
			return Filter == other.Filter;
		}
		#endregion
	}

Stepping through the code with the debugger it seems that all of these methods are called. It actually works correctly without the IEquatable implementation, but I have left it in for completeness.

Advertisements
 

New .Net Client Library for Guardian Open Platform

Filed under: Asp.Net,C#,guardian.co.uk,Web — Rupert Bates @ 10:30 am

I’ve recently created a .Net client library for the Guardian’s Open Platform API which allows you to query guardian.co.uk, pull back content and ‘Build applications with the Guardian’.

The project is open source and hosted on codeplex.

You can find out more about the Open Platform API here:

http://www.guardian.co.uk/open-platform

 

Use CacheProfile attribute with output caching in asp.net mvc November 22, 2009

Filed under: Asp.Net,C#,MVC,Web — Rupert Bates @ 9:21 pm

I’ve recently been setting up some caching on a new Asp.Net MVC site by using the OutputCache attribute on my controllers:


//cache this for an hour
 [OutputCache(Duration=60 * 60, VaryByParam="")]

This works really well except that it means hard codeding the cache time into my app so if I want to change it I need to recompile and deploy my code which is obviously far from ideal. So I changed my code to load the value from the web.config and ran into this error:

‘An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type’

Not very helpful.

But fortunately there is a better way of doing all this using cache profiles, I just set up a cache profile in my web.config:


<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="HomePage" duration="3600" varyByParam="None" location="ServerAndClient"/>
</outputCacheProfiles>
</outputCacheSettings>

</caching>

</system.web>

And then reference this in my attribute:

 [OutputCache( CacheProfile="HomePage")]

And bingo, we’re sucking diesel as a friend of mine says!

 

Create an Asp.Net MVC HtmlHelper for use in unit tests November 17, 2009

Filed under: Asp.Net,C#,Coding,MVC,Web — Rupert Bates @ 5:47 pm

Here’s a utility factory class to create an HtmlHelper instance so that you can unit test extension methods written on it. It will take a controller and a model for methods which rely on those. I adapted it from this post, and changed it to work with Asp.Net version 1.0 and to accept a model.

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using KableDirect.Web.FrontEnd.Controllers;

namespace KableDirect.UnitTests
{
    class HtmlHelperFactory
    {
        public static HtmlHelper CreateInstance(RouteData route)
        {
            return CreateInstance(route, new HomeController());
        }
        public static HtmlHelper CreateInstance(RouteData route, Controller controller)
        {
            return CreateInstance(route, controller, null);
        }
        public static HtmlHelper CreateInstance(RouteData route, Controller controller, object model)
        {
            HttpContextBase httpContext = new HttpContextDummy();

            var cc = new ControllerContext(httpContext, route, controller);
            var vd = new ViewDataDictionary(model);
            ViewContext vc = new ViewContext(cc, new ViewDummy(), vd, new TempDataDictionary());
            return new HtmlHelper(vc, new ViewDataContainerDummy(vd), new RouteCollection());
        }


        // Dummy classes needed to be able to create HtmlHelper

        private class HttpRequestDummy : HttpRequestBase
        {
            public override string ApplicationPath
            {
                get { return ""; }
            }

            public override string AppRelativeCurrentExecutionFilePath
            {
                // Any shorter string here gives exception:
                // index larger than length of string
                get { return "~/"; }
            }

            public override string PathInfo
            {
                get { return ""; }
            }
        }

        private class HttpResponseDummy : HttpResponseBase
        {
            public override string ApplyAppPathModifier(string virtualPath)
            {
                return virtualPath;
            }
        }

        private class HttpContextDummy : HttpContextBase
        {
            public override HttpRequestBase Request
            {
                get { return new HttpRequestDummy(); }
            }

            public override HttpResponseBase Response
            {
                get { return new HttpResponseDummy(); }
            }
        }

        private class ViewDummy : IView
        {
            public void Render(ViewContext viewContext, System.IO.TextWriter writer)
            {
                throw new NotImplementedException();
            }
        }

        private class ViewDataContainerDummy : IViewDataContainer
        {
            public ViewDataContainerDummy()
            {
            }

            public ViewDataContainerDummy(ViewDataDictionary dataDictionary)
            {
                _data = dataDictionary;
            }

            private ViewDataDictionary _data;
            public ViewDataDictionary ViewData
            {
                get { return _data; }
                set { _data = value; }
            }
        }
    }
}