Archive for July, 2010

Published by breki on 31 Jul 2010

Fresh Catch For July 31st

These are my new delicious links for July 31st:


Published by breki on 25 Jul 2010

Maperitive: Tile Generator

Maperitive Tile Generator

I’ve just released Maperitive build 924 which has two new commands:

Eastern Alps

Published by breki on 23 Jul 2010

Fresh Catch For July 23rd

These are my new delicious links for July 23rd:


Published by breki on 21 Jul 2010

Too Much Version Control?

Too much version control?

Published by breki on 16 Jul 2010

Fresh Catch For July 16th

These are my new delicious links for July 16th:


Published by breki on 13 Jul 2010

Maperitive Has Menus!

Maperitive Got Its First Menus

DOWNLOAD LINK: http://maperitive.net/download/Maperitive-907.zip

Four months after the first public release, Maperitive has finally received some improvements to the GUI: menus. Right now menus cover only non-interactive commands, but in near future I will add dialogs which I guess will make all command line haters very happy.

All the functionality will still be available through the command line. Menus will cover only the most commonly used functions and settings.

Published by breki on 12 Jul 2010

Fresh Catch For July 12th

These are my new delicious links for July 12th:


Published by breki on 12 Jul 2010

Uri Class Cheat Sheet

Another reference post, this time covering usage of .NET Uri class.

Example URL: http://somewhere.else.com:9999/service/packages?page=2#fragment

The table below contains values for various properties of Uri for that example URL:

AbsolutePath /service/packages
AbsoluteUri http://somewhere.else.com:9999/service/packages?page=2#fragment
Authority somewhere.else.com:9999
DnsSafeHost somewhere.else.com
Fragment #fragment
Host somewhere.else.com
HostNameType Dns
IsAbsoluteUri true
IsDefaultPort false
IsFile false
IsLoopback false
IsUnc false
LocalPath /service/packages
OriginalString http://somewhere.else.com:9999/service/packages?page=2#fragment
PathAndQuery /service/packages?page=2
Port 9999
Query ?page=2
Scheme http
Segments array: “/”, “service/”, “packages”
UserEscaped false
UserInfo “” (empty string)

Published by breki on 11 Jul 2010

Windsor Castle: Auto-Wiring Using Parameter Names

NOTE: this is probably only one way of doing things in Windsor Castle. If you know of any other, don’t hesitate to write a comment.

A design problem I was trying to solve yesterday: I have a component which depends on two implementations of the same interface:

public class HelpCommand (    
   ICommandDescriptor oneLinerDescriptor,     
   ICommandDescriptor detailedDescriptor)     
{     
   ...     
}     

HelpCommand prints out human-readable descriptions of various commands in Maperitive. If the user specifies the exact command, HelpCommand prints out a detailed description. On the other hand, if the command parameter is not specified, HelpCommand lists all of the commands and prints a one-liner description for each of them. So there are two implementations of the ICommandDescriptor interface: OneLinerDescriptor and DetailedDescriptor classes.

The problem I was having with this design is that all of my *Command components are registered in one go, using the convention like

container.Register (AllTypes.Of<ICommand>().FromAssembly (typeof(ICommand).Assembly)    
   .Configure (c => c.Named (c.Implementation.Name).LifeStyle.Transient));

so I’m letting Windsor do all the wiring magic without me being able to provide any custom registration logic for individual commands like HelpCommand. So how do I tell Windsor which component to use for oneLinerDescriptor and which for detailedDescriptor?

Bad Solution

Of course, I could simply specify the concrete implementations in HelpCommand’s constructor:

public class HelpCommand (    
   OneLinerDescriptor oneLinerDescriptor,     
   DetailedDescriptor detailedDescriptor)     
{     
   ...     
}     

But this couples the three classes together and goes against the “rely on abstractions, not implementations” mantra. It also makes unit testing of HelpCommand more difficult, since I have to drag the two descriptor classes around (and they could have their own dependencies to satisfy, which further bloats the unit testing code).

Factories?

I could use a factory to get the implementations I need, but that would be wasteful: a simple constructor dependency is much slicker and it doesn’t introduce an additional component (factory) into the picture. Also, factories are more suited for components which are created later, on-demand. In my case I want the descriptors to be available right at the beginning of HelpCommand’s lifecycle.

Resolving By Name

So I got an idea: why not use the name of the constructor parameter as an indicator of what kind of implementation I need?

This requires writing a custom subresolver:

    public class SubDependencyResolverByName : ISubDependencyResolver
    {
        public SubDependencyResolverByName(IKernel kernel, string componentPrefix)
        {
            this.kernel = kernel;
            this.componentPrefix = componentPrefix;
        }

        public bool CanResolve(
            CreationContext context, 
            ISubDependencyResolver contextHandlerResolver, 
            ComponentModel model, 
            DependencyModel dependency)
        {
            if (dependency.IsOptional)
                return false;

            return FindCandidateComponent(dependency.DependencyKey, dependency.TargetType) != null;
        }

        public object Resolve(
            CreationContext context, 
            ISubDependencyResolver contextHandlerResolver, 
            ComponentModel model, 
            DependencyModel dependency)
        {
            ComponentModel dependencyResolvingComponent = FindCandidateComponent (dependency.DependencyKey, dependency.TargetType);
            return kernel.Resolve(dependencyResolvingComponent.Name, dependency.TargetType);
        }

        protected ComponentModel FindCandidateComponent (string dependencyKey, Type targetType)
        {
            foreach (GraphNode node in kernel.GraphNodes)
            {
                ComponentModel visitedComponent = (ComponentModel) node;
                if (visitedComponent.Service != targetType)
                    continue;

                if (componentPrefix == null)
                {
                    if (0 == string.Compare(visitedComponent.Name, dependencyKey, StringComparison.InvariantCultureIgnoreCase))
                        return visitedComponent;
                }
                else
                {
                    if (false == visitedComponent.Name.StartsWith(componentPrefix, StringComparison.InvariantCultureIgnoreCase))
                        continue;

                    string nameWithoutPrefix = visitedComponent.Name.Substring(componentPrefix.Length);
                    if (0 == string.Compare (nameWithoutPrefix, dependencyKey, StringComparison.InvariantCultureIgnoreCase))
                        return visitedComponent;                    
                }
            }

            return null;
        }

        private readonly IKernel kernel;
        private readonly string componentPrefix;
    }

…and attaching it to the container:

container.Kernel.Resolver.AddSubResolver (
   new SubDependencyResolverByName (container.Kernel, "auto_"));

Results

Initially I had problems with this new resolver: it tried to be too smart and it messed with the existing (non-problematic) component wiring. So I decided to limit its scope only on dependency components which have a container ID starting with a certain prefix (in my case “auto_”). So if I want OneLinerDescriptor and DetailedDescriptor to be auto-detected by the resolver, I need to register them as “auto_OneLinerDescriptor” and “auto_DetailedDescriptor” in the Windsor container (casing is ignored). The resolver is currently limited to constructor dependencies only, but I see no reason it shouldn’t work for property injections too.

Is name-matching a good approach to auto-wiring components? I don’t know, time will tell. I certainly feel there’s something missing in how Windsor resolves dependencies by the default: type-matching is fine for simple scenarios, but as soon as you have more than one implementation of the same service interface, you need to specify service overrides or rely on "black magic” to wire things for you.

Published by breki on 07 Jul 2010

System.Web.HttpRequest Paths Cheat Sheet

More a “remember” post for me than a real reference thing, but I think it might be useful to anybody doing more than simple work with HttpRequests.

Example request URL: http://localhost:65107/Services.svc/services?page=30

The table below contains values for various path-related properties of HttpRequest for that example request URL:

ApplicationPath /
AppRelativeCurrentExecutionFilePath ~/Services.svc
CurrentExecutionFilePath /Services.svc
FilePath /Services.svc
Path /Services.svc/services
PathInfo /services
PhysicalApplicationPath D:\svn\eEnvoyer\SEPA\EEnvoyer.Server\
PhysicalPath D:\svn\eEnvoyer\SEPA\EEnvoyer.Server\Services.svc
RawUrl /Services.svc/services?page=30
Url http://localhost:65107/Services.svc/services?page=30

Next »