PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Bifrost.WCF/Commands/CommandCoordinatorService.cs

#
C# | 80 lines | 55 code | 6 blank | 19 comment | 1 complexity | 8ad836616b97845ea81b06c35f3726fb 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 System.ServiceModel;
  26. using System.ServiceModel.Activation;
  27. using System.ServiceModel.Web;
  28. using Bifrost.Commands;
  29. using Bifrost.Execution;
  30. using Bifrost.Serialization;
  31. namespace Bifrost.WCF.Commands
  32. {
  33. [ServiceContract]
  34. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  35. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
  36. public class CommandCoordinatorService
  37. {
  38. ICommandCoordinator _commandCoordinator;
  39. ISerializer _serializer;
  40. ITypeDiscoverer _typeDiscoverer;
  41. Dictionary<string, Type> _commandTypes;
  42. public CommandCoordinatorService(
  43. ICommandCoordinator commandCoordinator,
  44. ISerializer serializer,
  45. ITypeDiscoverer typeDiscoverer)
  46. {
  47. _commandCoordinator = commandCoordinator;
  48. _serializer = serializer;
  49. _typeDiscoverer = typeDiscoverer;
  50. PopulateCommandTypes();
  51. }
  52. void PopulateCommandTypes()
  53. {
  54. var commands = _typeDiscoverer.FindMultiple<ICommand>();
  55. _commandTypes = commands.Select(c=>c).ToDictionary(c => c.Name);
  56. }
  57. [WebInvoke(
  58. Method="POST",
  59. RequestFormat=WebMessageFormat.Json,
  60. ResponseFormat=WebMessageFormat.Json,
  61. UriTemplate="handle")]
  62. public CommandResult Handle(CommandDescriptor commandDescriptor)
  63. {
  64. var commandName = commandDescriptor.Name;
  65. if (_commandTypes.ContainsKey(commandName))
  66. {
  67. var commandInstance = _serializer.FromJson(_commandTypes[commandName], commandDescriptor.Command) as ICommand;
  68. var result = _commandCoordinator.Handle(commandInstance);
  69. return result;
  70. }
  71. return null;
  72. }
  73. }
  74. }