Question

i'm trying to make my exceptionhandling dependend on the context. I have a factory class constructing exceptionhandlers. the handler should be constructed by the last thrown exception type. configuring structuremap the classic way, it works out fine. trying to use conditional constructing, my code fails and i can't see why?! What am i missing? Where is my major mistake?

regards, -jan

Working code:

ObjectFactory.Initialize(x => 
    x.ForRequestedType<IExceptionHandler>()
        .TheDefault.Is.OfConcreteType<MyExceptionHandler>());

Non-working code

ObjectFactory.Initialize(x =>
    x.ForRequestedType<IExceptionHandler>().TheDefault.Is.Conditional(o =>
        o.TheDefault.Is.OfConcreteType(MyExceptionHandler)));

Getting an instance:

IExceptionHandler handler = ObjectFactory.With("exception").EqualTo(exception).GetInstance<IExceptionHandler>();

So I'll try to to be more specific:

I have a BaseExceptionHandler, MyExceptionHandler inheriting from Base and MyException inheriting from System.Exception. Right now, if I try get my handler i get an error 202: No Default Instance defined for PluginFamily MyException...
The classes look like shown below...

public class MyException : System.Exception
{
    public MyException()
    {...}
    ...
}

public class BaseExceptionHandler
{
    public BaseExceptionHandler(Exception exception)
    {...}
    ...
}

public class MyExceptionHandler : BaseExceptionHandler
{
    public MyExceptionHandler(MyException exception) : base(exception)
    {...}
    ...
}
Was it helpful?

Solution

Your problem has nothing to do with the Conditional registration. It has to do with the way you are passing the exception. The .With(string parameterName) syntax should only be used with primitive types (string, int, etc). For an exception, you want the With(T instance) syntax:

IExceptionHandler handler = ObjectFactory.With<Exception>(exception).GetInstance<IExceptionHandler>();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top