PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/BurnSystems/src/Scope/CombinedContext.cs

https://bitbucket.org/mbrenn/entityconnector
C# | 116 lines | 58 code | 13 blank | 45 comment | 4 complexity | dfcb3afd4bd1dbb00774fc8558c7446d MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using BurnSystems.Test;
  6. namespace BurnSystems.Scope
  7. {
  8. /// <summary>
  9. /// Combines a context with an existing context
  10. /// </summary>
  11. public class CombinedContext : IContext
  12. {
  13. /// <summary>
  14. /// Primary context
  15. /// </summary>
  16. private IContext primaryContext;
  17. /// <summary>
  18. /// Secondary source
  19. /// </summary>
  20. private IContext secondaryContext;
  21. /// <summary>
  22. /// Gets the local context source which can be used to add additional factory methods
  23. /// </summary>
  24. public IContextSource LocalContextSource
  25. {
  26. get
  27. {
  28. return this.primaryContext.LocalContextSource;
  29. }
  30. }
  31. /// <summary>
  32. /// Initializes a new instance of the CombinedContext class.
  33. /// </summary>
  34. /// <param name="first">First context</param>
  35. /// <param name="second">Secondary context.
  36. /// This context will be modified, if an additional context source has been added</param>
  37. public CombinedContext(IContext first, IContext second)
  38. {
  39. Ensure.IsNotNull(first);
  40. Ensure.IsNotNull(second);
  41. this.primaryContext = first;
  42. this.secondaryContext = second;
  43. }
  44. /// <summary>
  45. /// Adds an item to context
  46. /// </summary>
  47. /// <param name="source">Source to be added</param>
  48. public void Add(IContextSource source)
  49. {
  50. this.secondaryContext.Add(source);
  51. }
  52. /// <summary>
  53. /// Gets an object, it is created if necessary.
  54. /// This method only works, if there is just one
  55. /// instance within the context source
  56. /// </summary>
  57. /// <typeparam name="T">Type of the parameter</typeparam>
  58. /// <returns>Object to be created or retrieved</returns>
  59. public T Get<T>()
  60. {
  61. var found = this.primaryContext.Get<T>();
  62. if (found == null)
  63. {
  64. found = this.secondaryContext.Get<T>();
  65. }
  66. return found;
  67. }
  68. /// <summary>
  69. /// Gets an object by token. This object is created if necessary.
  70. /// </summary>
  71. /// <typeparam name="T">Type of the parameter</typeparam>
  72. /// <param name="token">Token to be added</param>
  73. /// <returns>Object to be created or retrieved</returns>
  74. public T Get<T>(string token)
  75. {
  76. var found = this.primaryContext.Get<T>(token);
  77. if (found == null)
  78. {
  79. found = this.secondaryContext.Get<T>(token);
  80. }
  81. return found;
  82. }
  83. /// <summary>
  84. /// Gets all object matching to a specific type
  85. /// </summary>
  86. /// <typeparam name="T"></typeparam>
  87. /// <returns>Enumeration of all items matching to the type</returns>
  88. public IEnumerable<T> GetAll<T>()
  89. {
  90. return this.primaryContext.GetAll<T>()
  91. .Union(this.secondaryContext.GetAll<T>());
  92. }
  93. /// <summary>
  94. /// Disposes all items implementing the IDisposable
  95. /// This method shall not be called, because this class shall be used as a helper methods.
  96. /// The methods creating the original context-variables shall dispose the context
  97. /// </summary>
  98. public void Dispose()
  99. {
  100. throw new NotImplementedException("DO NOT CALL ME");
  101. }
  102. }
  103. }