PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Bifrost/Validation/CommandValidatorProvider.cs

#
C# | 156 lines | 96 code | 23 blank | 37 comment | 7 complexity | 6fcec3e2e54f4fd7289a726341bb4aaa MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. #region License
  2. //
  3. // Copyright (c) 2008-2012, DoLittle Studios and Komplett ASA
  4. //
  5. // Licensed under the Microsoft Permissive License (Ms-PL), Version 1.1 (the "License")
  6. // With one exception :
  7. // Commercial libraries that is based partly or fully on Bifrost and is sold commercially,
  8. // must obtain a commercial license.
  9. //
  10. // You may not use this file except in compliance with the License.
  11. // You may obtain a copy of the license at
  12. //
  13. // http://bifrost.codeplex.com/license
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. //
  21. #endregion
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using Bifrost.Commands;
  26. using Bifrost.Execution;
  27. using Microsoft.Practices.ServiceLocation;
  28. using Bifrost.Configuration;
  29. namespace Bifrost.Validation
  30. {
  31. /// <summary>
  32. /// Represents an instance of an <see cref="ICommandValidatorProvider">ICommandValidatorProvider.</see>
  33. /// </summary>
  34. [Singleton]
  35. public class CommandValidatorProvider : ICommandValidatorProvider
  36. {
  37. static ICommandInputValidator NullInputValidator = new NullCommandInputValidator();
  38. static ICommandBusinessValidator NullBusinessValidator = new NullCommandBusinessValidator();
  39. static Type _inputValidatorType = typeof (ICommandInputValidator);
  40. static Type _businessValidatorType = typeof (ICommandBusinessValidator);
  41. static Type _validatesType = typeof (ICanValidate<>);
  42. ITypeDiscoverer _typeDiscoverer;
  43. IContainer _container;
  44. IConfigure _configuration;
  45. Dictionary<Type, Type> _inputValidators;
  46. Dictionary<Type, Type> _businessValidators;
  47. /// <summary>
  48. /// Initializes an instance of <see cref="CommandValidatorProvider"/> CommandValidatorProvider
  49. /// </summary>
  50. /// <param name="typeDiscoverer">
  51. /// An instance of ITypeDiscoverer to help identify and register <see cref="ICommandInputValidator"/> implementations
  52. /// and <see cref="ICommandBusinessValidator"/> implementations
  53. /// </param>
  54. /// <param name="container">An instance of <see cref="IContainer"/> to manage instances of any <see cref="ICommandInputValidator"/></param>
  55. /// <param name="configuration">An instance of <see cref="IConfigure"/> that holds the current configuration</param>
  56. public CommandValidatorProvider(ITypeDiscoverer typeDiscoverer, IContainer container, IConfigure configuration)
  57. {
  58. _typeDiscoverer = typeDiscoverer;
  59. _container = container;
  60. _configuration = configuration;
  61. Initialize();
  62. }
  63. #pragma warning disable 1591 // Xml Comments
  64. public ICommandInputValidator GetInputValidatorFor(ICommand command)
  65. {
  66. return GetInputValidatorFor(command.GetType());
  67. }
  68. public ICommandBusinessValidator GetBusinessValidatorFor(ICommand command)
  69. {
  70. return GetBusinessValidatorFor(command.GetType());
  71. }
  72. public ICommandInputValidator GetInputValidatorFor(Type type)
  73. {
  74. Type registeredType;
  75. _inputValidators.TryGetValue(type, out registeredType);
  76. var inputValidator = registeredType != null ? _container.Get(registeredType) as ICommandInputValidator : NullInputValidator;
  77. return inputValidator;
  78. }
  79. public ICommandBusinessValidator GetBusinessValidatorFor(Type type)
  80. {
  81. Type registeredType;
  82. _businessValidators.TryGetValue(type, out registeredType);
  83. var businessValidator = registeredType != null ? _container.Get(registeredType) as ICommandBusinessValidator : NullBusinessValidator;
  84. return businessValidator;
  85. }
  86. #pragma warning restore 1591 // Xml Comments
  87. /// <summary>
  88. /// Gets a list of registered input validator types
  89. /// </summary>
  90. public IEnumerable<Type> RegisteredInputValidators
  91. {
  92. get { return _inputValidators.Values; }
  93. }
  94. /// <summary>
  95. /// Gets a list of registered business validator types
  96. /// </summary>
  97. public IEnumerable<Type> RegisteredBusinessValidators
  98. {
  99. get { return _businessValidators.Values; }
  100. }
  101. void Initialize()
  102. {
  103. _inputValidators = new Dictionary<Type, Type>();
  104. _businessValidators = new Dictionary<Type, Type>();
  105. var inputValidators = _typeDiscoverer.FindMultiple(_inputValidatorType);
  106. var businessValidators = _typeDiscoverer.FindMultiple(_businessValidatorType);
  107. Array.ForEach(inputValidators, type => Register(type, _inputValidatorType));
  108. Array.ForEach(businessValidators, type => Register(type, _businessValidatorType));
  109. }
  110. void Register(Type typeToRegister, Type registerFor)
  111. {
  112. var validatorRegistry = registerFor == _inputValidatorType
  113. ? _inputValidators
  114. : _businessValidators;
  115. var commandType = GetCommandType(typeToRegister);
  116. if (commandType == null ||
  117. commandType.IsInterface ||
  118. validatorRegistry.ContainsKey(commandType))
  119. return;
  120. validatorRegistry.Add(commandType, typeToRegister);
  121. _container.Bind(typeToRegister, typeToRegister, _configuration.DefaultObjectLifecycle);
  122. }
  123. Type GetCommandType(Type typeToRegister)
  124. {
  125. var types = from interfaceType in typeToRegister.GetInterfaces()
  126. where interfaceType.IsGenericType
  127. let baseInterface = interfaceType.GetGenericTypeDefinition()
  128. where baseInterface == _validatesType
  129. select interfaceType.GetGenericArguments().FirstOrDefault();
  130. return types.FirstOrDefault();
  131. }
  132. }
  133. }