PageRenderTime 16ms CodeModel.GetById 11ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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

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