code_junkie

Does Castle-Windsor support ForwardedTypes via XML configuration

Nov 12th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. public class HandlerForwardingFacility : AbstractFacility
  2. {
  3. IConversionManager conversionManager;
  4.  
  5. protected override void Init()
  6. {
  7. conversionManager = (IConversionManager)Kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey);
  8. Kernel.HandlerRegistered += new HandlerDelegate(Kernel_HandlerRegistered);
  9. }
  10.  
  11. void Kernel_HandlerRegistered(IHandler handler, ref bool stateChanged)
  12. {
  13. if (handler is ForwardingHandler) return;
  14.  
  15. var model = handler.ComponentModel;
  16.  
  17. if (model.Configuration == null) return;
  18.  
  19. var forward = model.Configuration.Children["forward"];
  20. if (forward == null) return;
  21.  
  22. foreach (var service in forward.Children)
  23. {
  24. Type forwardedType = (Type)conversionManager.PerformConversion(service, typeof (Type));
  25. Kernel.RegisterHandlerForwarding(forwardedType, model.Name);
  26. }
  27. }
  28. }
  29.  
  30. public interface IDog
  31. {
  32. void Bark();
  33. }
  34.  
  35. public interface IDuck
  36. {
  37. void Quack();
  38. }
  39.  
  40. public class Mutant : IDog, IDuck
  41. {
  42. public void Bark()
  43. {
  44. Console.WriteLine("Bark");
  45. }
  46.  
  47. public void Quack()
  48. {
  49. Console.WriteLine("Quack");
  50. }
  51. }
  52.  
  53. <castle>
  54. <facilities>
  55. <facility id="facility.handlerForwarding" type="Example.Facilities.HandlerForwardingFacility, Example" />
  56. </facilities>
  57. <components>
  58. <component id="mutant" service="Example.IDog, Example" type="Example.Mutant, Example">
  59. <forward>
  60. <service>Example.IDuck, Example</service>
  61. </forward>
  62. </component>
  63. </components>
  64. </castle>
  65.  
  66. WindsorContainer container = new WindsorContainer(new XmlInterpreter());
  67.  
  68. var dog = container.Resolve<IDog>();
  69. var duck = container.Resolve<IDuck>();
  70.  
  71. Assert.AreSame(dog, duck);
Add Comment
Please, Sign In to add comment