PageRenderTime 56ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/BurnSystems/src/Scope/Extensions.cs

https://bitbucket.org/mbrenn/entityconnector
C# | 79 lines | 48 code | 5 blank | 26 comment | 8 complexity | 1f54aa81dd17831d84d2d92436e15535 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace BurnSystems.Scope
  6. {
  7. /// <summary>
  8. /// Contains some extension methods for Context
  9. /// </summary>
  10. public static class Extensions
  11. {
  12. /// <summary>
  13. /// Tries to add the context source of the given object.
  14. /// If the object implements IContextSource or IContextSourceFactory, the object
  15. /// can be added
  16. /// </summary>
  17. /// <param name="context">Context, where Context Source shall be added</param>
  18. /// <param name="value">Object perhaps containing a context source</param>
  19. /// <returns>true, if something could be added, otherwise false</returns>
  20. public static bool TryToAddContextSource(this IContext context, object value)
  21. {
  22. var contextSource = value as IContextSource;
  23. if (contextSource != null)
  24. {
  25. context.Add(contextSource);
  26. return true;
  27. }
  28. var contextSourceFactory = value as IContextSourceFactory;
  29. if (contextSourceFactory != null)
  30. {
  31. context.Add(contextSourceFactory.CreateContextSource());
  32. return true;
  33. }
  34. return false;
  35. }
  36. /// <summary>
  37. /// Adds a context object, if no object is already existing at the place.
  38. /// This is done by check, if an instance can be created
  39. /// </summary>
  40. /// <typeparam name="T">Type of the context</typeparam>
  41. /// <param name="contextSource">Contextsource to whom the object shall be added</param>
  42. /// <param name="factory">Factory method</param>
  43. public static void AddIfNotExisting<T>(this IContextSource contextSource, Func<T> factory)
  44. {
  45. using (var testContext = new Context(contextSource, "TestContext"))
  46. {
  47. var value = testContext.Get<T>();
  48. if (value == null)
  49. {
  50. contextSource.Add<T>(factory);
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// Adds a context object, if no object is already existing at the place.
  56. /// This is done by check, if an instance can be created
  57. /// </summary>
  58. /// <typeparam name="T">Type of the context</typeparam>
  59. /// <param name="contextSource">Contextsource to whom the object shall be added</param>
  60. /// <param name="token">Token of the object</param>
  61. /// <param name="factory">Factory method</param>
  62. public static void AddIfNotExisting<T>(this IContextSource contextSource, string token, Func<T> factory)
  63. {
  64. using (var testContext = new Context(contextSource, "TestContext"))
  65. {
  66. var value = testContext.Get<T>(token);
  67. if (value == null)
  68. {
  69. contextSource.Add<T>(token, factory);
  70. }
  71. }
  72. }
  73. }
  74. }