Typed factory silently discards arguments that can't be mapped to a constructor?

155 Views Asked by At

Question

Should a typed factory throw an exception if there are arguments specified in a creation function that can't be mapped to a constructor? E.g.

public class Foo
{
    public Foo() : Foo(new Guid())
    {
    }

    // Used to be 'public Foo(string bar)'
    public Foo(Guid guid)
    {
    }
}

public interface FooFactory
{
    Foo CreateFoo(string bar);
}

We found that part of a bug was being caused by a typed factory silently calling the default constructor instead of throwing an exception when it couldn't find a constructor on Foo that accepted a string. The constructor on Foo had been changed but the method to create it in the typed factory hadn't been updated.

Partial solution?

We experimented a bit and found a potential solution:

public class OurComponentSelector : DefaultTypedFactoryComponentSelector
{
    public override IDictionary GetArguments(MethodInfo method, object[] arguments)
    {
        // Check an appropriate constructor can be found, throw exception if not
    }
}

Would this be an appropriate work-around?

1

There are 1 best solutions below

0
On

Yes. This is exactly the type of extensibility that ITypedFactoryComponentSelector is intended to provide.

Windsor's own DefaultDelegateComponentSelector uses the same extension point to transform arguments before handing them off to the container.