ILearnable .Net

August 3, 2011

Bug in PageTypeBuilder 1.3.0 and 1.3.1

Filed under: Uncategorized — andreakn @ 09:19

Quite a while back I convinced Joel to include a feature in PTB 1.3 which enabled putting PageTypeProperty attributes on interface declarations, which has made my life using PTB a heckuva lot easier,

Instead of having to use inheritance to get properties onto pages I now mostly use interfaces, especially for “side-features” like stuff that is supposed to go into the sidebar of a site it doesn’t always make sense to specify this using inheritance, sooner or later you end up with a page which needs the side-feature, but have nothing else in common with the other pages, and inheritance trees based on side-feature is an antipattern only until you add side-feature number two, then it becomes impossible.

Now for years I have preferred to use PTB *only* for defining the pages at startup-time, and not for extracting the data from them during run-time. (I have found that the convenience of specifying prop names as strings to EPiServer:Property far outweighs the potential for harm during refactoring) And if this is all you are doing then both 1.3.0 and 1.3.1 will work fine for you.

It wasn’t until some collegues of mine started using the new interface features that I became aware that I had introduced a bug into PTB: Whenever you declare page type properties using interfaces you cannot get those properties back using the same interface. As I said I never used PTB for this purpose, that’s my excuse and I’m sticking to it.

10 minutes after being made aware of this defect I had created a fix for it and shipped it to Joel.

That’s where it stopped. Joel never did release a fixed version of PTB 1.3.0, and when 1.3.1 came out it had the same bug in it.

I asked Joel why during NDC2011 and he claimed that he had found some problem with the fix but couldn’t remember what that was exactly.

This means that whenever I start up a new project I always have to use my special-build PTB and not the official one.

Anyhoo, I thought more people might be interested in the fix, so here goes, all you need to change the implementation of one method in PageTypeBuilder/Reflection/MethodInfoExtensions.cs:


...
public static bool IsGetterOrSetterForPropertyWithAttribute(this MethodInfo propertyGetterOrSetter, Type attributeType)
        {
            if (!propertyGetterOrSetter.IsGetterOrSetter())
                return false;

            string propertyName = propertyGetterOrSetter.GetPropertyName();
            PropertyInfo property = propertyGetterOrSetter.DeclaringType.GetAllValidPageTypePropertiesFromClassAndImplementedInterfaces().FirstOrDefault(p=>p.Name.Equals(propertyName));

            if (property == null)
                return false;

            return property.HasAttribute(attributeType);
        }
...

Blog at WordPress.com.