DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Block.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 using System.Collections.Generic;
19 
20 using Deveel.Data.Diagnostics;
21 
22 namespace Deveel.Data {
23  public class Block : IBlock {
24  private IQuery query;
25 
26  public Block(IRequest request) {
27  if (request == null)
28  throw new ArgumentNullException("request");
29 
30  query = request as IQuery;
31 
32  Context = request.CreateBlockContext();
33  Context.UnregisterService<IBlock>();
34  Context.RegisterInstance<IBlock>(this);
35 
36  Parent = request as IBlock;
37  }
38 
39  ~Block() {
40  Dispose(false);
41  }
42 
43  public IBlock Parent { get; private set; }
44 
45  public IBlock Next { get; set; }
46 
47  public IQuery Query {
48  get {
49  if (query != null)
50  return query;
51 
52  return Parent.Query;
53  }
54  }
55 
57  get { return Parent; }
58  }
59 
60  IEnumerable<KeyValuePair<string, object>> IEventSource.Metadata {
61  get { return GetEventMetadata(); }
62  }
63 
64  protected virtual IEnumerable<KeyValuePair<string, object>> GetEventMetadata() {
65  return new KeyValuePair<string, object>[0];
66  }
67 
68  public void Dispose() {
69  Dispose(true);
70  GC.SuppressFinalize(this);
71  }
72 
73  protected virtual void Dispose(bool disposing) {
74  if (disposing) {
75  if (Context != null)
76  Context.Dispose();
77  }
78 
79  Context = null;
80  query = null;
81  }
82 
83  public IBlockContext Context { get; private set; }
84 
86  get { return Context; }
87  }
88 
90  return Context.CreateBlockContext();
91  }
92 
94  ExecuteBlock(context);
95  }
96 
97  protected virtual void ExecuteBlock(BlockExecuteContext context) {
98  // default implementation is an empty block, that does nothing
99  }
100 
101  public virtual IBlock CreateBlock() {
102  return new Block(this);
103  }
104  }
105 }
IBlockContext CreateBlockContext()
virtual IBlock CreateBlock()
Definition: Block.cs:101
virtual void Dispose(bool disposing)
Definition: Context.cs:63
virtual void ExecuteBlock(BlockExecuteContext context)
Definition: Block.cs:97
IEventSource ParentSource
Gets an optional parent source.
Definition: IEventSource.cs:49
virtual void Dispose(bool disposing)
Definition: Block.cs:73
IEnumerable< KeyValuePair< string, object > > Metadata
Gets the list of metadata associated to the source.
Definition: IEventSource.cs:57
virtual IEnumerable< KeyValuePair< string, object > > GetEventMetadata()
Definition: Block.cs:64
void Execute(BlockExecuteContext context)
void Dispose()
Definition: Block.cs:68
Block(IRequest request)
Definition: Block.cs:26
IQuery query
Definition: Block.cs:24
Represents the origin of system events, providing a mechanism to fill the metadata before dispatching...
Definition: IEventSource.cs:40