DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SystemContextExtensions.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 using Deveel.Data.Diagnostics;
20 using Deveel.Data.Services;
21 
22 namespace Deveel.Data.Sql.Triggers {
23  public static class SystemContextExtensions {
24  public static void UseTriggerListener(this ISystemContext context, ITriggerListener listener) {
25  var router = context.ResolveService<TriggerEventRouter>();
26  if (router == null)
27  context.RegisterService<TriggerEventRouter>();
28 
29  context.RegisterInstance(listener);
30  }
31 
32  public static void ListenTriggers(this ISystemContext context, Action<TriggerEvent> listener) {
33  context.UseTriggerListener(new DelegatedTriggerListener(context, listener));
34  }
35 
36  #region DelegatedTriggerListener
37 
38  private class DelegatedTriggerListener : ITriggerListener, IDisposable {
39  private Action<TriggerEvent> listener;
41 
42  public DelegatedTriggerListener(ISystemContext systemContext, Action<TriggerEvent> listener) {
43  this.systemContext = systemContext;
44  this.listener = listener;
45  }
46 
47  public void OnTriggerEvent(TriggerEvent trigger) {
48  try {
49  if (listener != null)
50  listener(trigger);
51  } catch (Exception ex) {
52  // TODO: form a source...
53  // TODO: systemContext.EventRegistry.Error(null, ex);
54  }
55  }
56 
57  public void Dispose() {
58  listener = null;
59  systemContext = null;
60  }
61  }
62 
63  #endregion
64  }
65 }
static void ListenTriggers(this ISystemContext context, Action< TriggerEvent > listener)
The execution context of a database system, that is defining the configurations and the components us...
static void UseTriggerListener(this ISystemContext context, ITriggerListener listener)
DelegatedTriggerListener(ISystemContext systemContext, Action< TriggerEvent > listener)