DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ContextExtensions.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 
19 namespace Deveel.Data.Diagnostics {
20  public static class ContextExtensions {
21  public static void RegisterEvent(this IContext context, IEvent @event) {
22  var currentContext = context;
23  while (currentContext != null) {
24  if (currentContext is IEventScope) {
25  var scope = (IEventScope) currentContext;
26  scope.EventRegistry.RegisterEvent(@event);
27  }
28 
29  currentContext = currentContext.Parent;
30  }
31  }
32 
33  public static void AttachRouter(this IContext context, IEventRouter router) {
34  var currentContext = context;
35  while (currentContext != null) {
36  if (currentContext is IEventScope) {
37  currentContext.RegisterInstance(router);
38  break;
39  }
40 
41  currentContext = currentContext.Parent;
42  }
43  }
44 
45  public static void Route<TEvent>(this IContext context, Action<TEvent> router)
46  where TEvent : class, IEvent {
47  context.Route(router, null);
48  }
49 
50  public static void Route<TEvent>(this IContext context, Action<TEvent> router, Func<TEvent, bool> condition)
51  where TEvent : class, IEvent {
52  context.AttachRouter(new DelegateRouter<TEvent>(router, condition));
53  }
54 
55  #region DelegatedRouter
56 
57  class DelegateRouter<TEvent> : ThreadedQueue<TEvent>, IEventRouter where TEvent : class, IEvent {
58  private Func<TEvent, bool> condition;
59  private Action<TEvent> route;
60 
61  public DelegateRouter(Action<TEvent> route, Func<TEvent, bool> condition) {
62  this.route = route;
63  this.condition = condition;
64  }
65 
66  protected override void Consume(TEvent message) {
67  route(message);
68  }
69 
70  public bool CanRoute(IEvent @event) {
71  if (!(@event is TEvent))
72  return false;
73 
74  if (condition == null)
75  return true;
76 
77  return condition((TEvent)@event);
78  }
79 
80  public void RouteEvent(IEvent e) {
81  Enqueue((TEvent)e);
82  }
83  }
84 
85  #endregion
86  }
87 }
static void RegisterEvent(this IContext context, IEvent @event)
void RouteEvent(IEvent e)
Routes the input event to the final destination.
This is an event occurred during the lifetime of a database.
Definition: IEvent.cs:24
DelegateRouter(Action< TEvent > route, Func< TEvent, bool > condition)
static void AttachRouter(this IContext context, IEventRouter router)
Defines the basic logic for the dispatching of events within a system workflow.
Definition: IEventRouter.cs:36