NotificationService.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Autofac;
  2. using GreenTree.Nachtragsmanagement.Core;
  3. using GreenTree.Nachtragsmanagement.Core.Plugins;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace GreenTree.Nachtragsmanagement.Services.Misc
  11. {
  12. public class NotificationService : INotificationService
  13. {
  14. /// <summary>
  15. /// Loads all implementations of the INotificationPlugin
  16. /// </summary>
  17. public IEnumerable<INotificationPlugin> LoadNotificationPlugins()
  18. {
  19. var type = typeof(INotificationPlugin);
  20. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  21. var types = assemblies
  22. .SelectMany(p => p.GetTypes())
  23. .Where(p => type.IsAssignableFrom(p) && !p.IsInterface)
  24. .Select(p => Activator.CreateInstance(p))
  25. .OfType<INotificationPlugin>()
  26. .ToList();
  27. return types;
  28. }
  29. /// <summary>
  30. /// Searches for all implementations of the INotificationPlugin
  31. /// </summary>
  32. public IEnumerable<INotificationPlugin> GetNotificationPlugins()
  33. {
  34. var notificationPlugins = Singleton<IContainer>.Instance.Resolve<IEnumerable<INotificationPlugin>>();
  35. if (notificationPlugins != null)
  36. return notificationPlugins;
  37. return LoadNotificationPlugins();
  38. }
  39. /// <summary>
  40. /// Gets a notification plugin by a specific name
  41. /// </summary>
  42. /// <param name="pluginSystemName">SystemName of notification plugin.</param>
  43. public INotificationPlugin GetNotificationPlugin(string pluginSystemName)
  44. {
  45. if (String.IsNullOrEmpty(pluginSystemName))
  46. return null;
  47. var notificationPlugin = Singleton<IContainer>.Instance.ResolveNamed<INotificationPlugin>(pluginSystemName);
  48. if (notificationPlugin != null)
  49. return notificationPlugin;
  50. var notificationPlugins = GetNotificationPlugins();
  51. if (notificationPlugins == null)
  52. return null;
  53. return notificationPlugins
  54. .FirstOrDefault(p => p.SystemName == pluginSystemName);
  55. }
  56. }
  57. }