views:

133

answers:

2

Hi there,

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>();
A: 

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>();
Joshua Flanagan
Hi,thank you Joshua, that makes it a lot clearer to me.Right now I'm getting an error 202 though.Probably it has something to do with the constructors, so i#ll be more specific about the classes involved.thanks -jan
Jan Selke
A: 

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)
    {...}
    ...
}
Jan Selke