Guest User

Untitled

a guest
Jul 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. namespace CastleTest
  2. {
  3. using System;
  4. using System.Linq;
  5. using Castle.Core.Logging;
  6. using Castle.Facilities.Logging;
  7. using Castle.MicroKernel;
  8. using Castle.MicroKernel.Registration;
  9. using Castle.MicroKernel.SubSystems.Configuration;
  10. using Castle.Windsor;
  11. using Castle.Windsor.Installer;
  12.  
  13. interface ISubProgram
  14. {
  15. void Run();
  16. }
  17.  
  18. public class FirstProgram : ISubProgram
  19. {
  20. private ILogger logger = NullLogger.Instance;
  21.  
  22. public void Run()
  23. {
  24. logger.Debug("Running first program.");
  25. }
  26.  
  27. public ILogger Logger
  28. {
  29. get
  30. {
  31. return logger;
  32. }
  33. set
  34. {
  35. logger = value;
  36. }
  37. }
  38. }
  39.  
  40. public class SecondProgram : ISubProgram
  41. {
  42. public void Run()
  43. {
  44. Console.WriteLine("Running second program.");
  45. }
  46. }
  47.  
  48. public class SubProgramsInstaller : IWindsorInstaller
  49. {
  50. public void Install(IWindsorContainer container, IConfigurationStore store)
  51. {
  52. //container.Register(Component.For<SubProgram>().ImplementedBy<FirstProgram>());
  53. container.Register(AllTypes
  54. .FromThisAssembly()
  55. //.IncludeNonPublicTypes()
  56. .BasedOn<ISubProgram>()
  57. .Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.Replace("Program", string.Empty)))
  58. .WithService
  59. .Base());
  60. container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.NLog).WithConfig("nlog.config"));
  61. }
  62. }
  63.  
  64. public class Program
  65. {
  66. void Run(string[] options)
  67. {
  68. using (var container = new WindsorContainer())
  69. {
  70. try
  71. {
  72. // install all installers within this assembly
  73. container.Install(FromAssembly.This());
  74. var subProgram = container.Resolve<ISubProgram>(options[0]);
  75. subProgram.Run();
  76. }
  77. catch (ComponentNotFoundException ex)
  78. {
  79. Usage(container, ex);
  80. }
  81. catch (IndexOutOfRangeException ex)
  82. {
  83. Usage(container, ex);
  84. }
  85. }
  86. }
  87.  
  88. private static void Usage(WindsorContainer container, Exception ex)
  89. {
  90. var q = from c in container.ResolveAll<ISubProgram>()
  91. select c.GetType().Name.Replace("Program", string.Empty);
  92. Console.WriteLine("Usage: Program {0}", string.Join("|", q));
  93. var logger = container.Resolve<ILogger>();
  94. logger.Fatal(ex.GetType().ToString(), ex);
  95. logger.Error(ex.GetType().ToString(), ex);
  96. logger.Warn(ex.GetType().ToString(), ex);
  97. logger.Info(ex.GetType().ToString(), ex);
  98. logger.Debug(ex.GetType().ToString(), ex);
  99. }
  100.  
  101. public static void Main(string[] options)
  102. {
  103. try
  104. {
  105. new Program().Run(options);
  106. }
  107. catch (Exception ex)
  108. {
  109. Console.WriteLine(ex);
  110. }
  111. }
  112. }
  113. }
Add Comment
Please, Sign In to add comment