Lets assume I have a service IFoo and two components providing it; StandardFoo
and SpecialFoo.
SpecialFoo needs some external configuration which may fail (by throwing an
exception from the ctor, for example), and I want it to fall back to
StandardFoo
if something went wrong there.
public interface IFoo { ... }
public class StandardFoo : IFoo { ... }
public class SpecialFoo : IFoo
{
public SpecialFoo(string configFile) { if (!File.Exists(configFile))
throw new FileNotFoundException(configFile); }
}
container.Register(Component.For<IFoo>().ImplementedBy<StandardFoo>().Named("standardFoo").IsFallback());
container.Register(Component.For<IFoo>().ImplementedBy<SpecialFoo>().Named("specialFoo").DependsOn(Dependency.OnAppSettingsValue("fooConfigFile")));
In theory, I'd like IFoo to be resolved to SpecialFoo if it can, but get a
StandardFoo
if anything fails there. Maybe I misunderstood what IsFallback should do,
or maybe I'm missing something minor that makes it not work.
So far, I tried using a custom IHandlerSelector only returns the special
foo when all conditions are met (I simply moved all checks from SpecialFoo ctor
to a static method and call that before returning either the SpecialFoo or
StandardFoo
handler instead of waiting for an exception). It works, but I thought there
must be an easier way. Especially since the code doing the checks is a lot
more than the actual SpecialFoo implementation, which looks kinda off.
I also tried using a typed factory with a custom selector, but I ran into
some other problems that way...let alone the fact that it pretty much
looked like the handler selector; except that now I had an extra interface
implemented by the typed factory facility instead of directly getting my
foo injected.
So, are there any other things I could use instead that make my life a bit
easier, or is the handler selector way the one to go? Because, either way,
the handler selector works just fine without IsFallback; making me think
about what IsFallback should actually do, and whether it works correctly.
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.