DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DatabaseContext.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 
20 using Deveel.Data.Services;
21 using Deveel.Data.Store;
23 
24 namespace Deveel.Data {
25  public sealed class DatabaseContext : Context, IDatabaseContext {
26  internal DatabaseContext(ISystemContext systemContext, IConfiguration configuration)
27  : base(systemContext) {
28  if (systemContext == null)
29  throw new ArgumentNullException("systemContext");
30  if (configuration == null)
31  throw new ArgumentNullException("configuration");
32 
33  ContextScope.Unregister<IConfiguration>();
34  ContextScope.RegisterInstance<IConfiguration>(configuration);
35  ContextScope.RegisterInstance<IDatabaseContext>(this);
36 
37  SystemContext = systemContext;
38 
39  Configuration = configuration;
40 
41  InitStorageSystem();
42  }
43 
44  protected override string ContextName {
45  get { return ContextNames.Database; }
46  }
47 
48  private static IConfiguration CreateSimpleConfig(ISystemContext systemContext, string dbName) {
49  if (String.IsNullOrEmpty(dbName))
50  throw new ArgumentNullException("dbName");
51 
52  var config = new Configuration.Configuration(systemContext.Configuration);
53  config.SetValue("database.name", dbName);
54  return config;
55  }
56 
57  protected override void Dispose(bool disposing) {
58  if (disposing) {
59  if (StoreSystem != null)
60  StoreSystem.Dispose();
61  }
62 
63  StoreSystem = null;
64 
65  base.Dispose(disposing);
66  }
67 
68  public IConfiguration Configuration { get; private set; }
69 
70  public ISystemContext SystemContext { get; private set; }
71 
72  public IStoreSystem StoreSystem { get; private set; }
73 
74  private void InitStorageSystem() {
75  try {
76  var storageTypeName = Configuration.GetString("database.storageSystem", DefaultStorageSystemNames.Heap);
77  StoreSystem = this.ResolveService<IStoreSystem>(storageTypeName);
78 
79  if (StoreSystem == null)
80  throw new DatabaseConfigurationException("The storage system for the database was not set.");
82  throw;
83  } catch (Exception ex) {
84  throw new DatabaseConfigurationException("Could not initialize the storage system", ex);
85  }
86  }
87 
89  return CreateTransactionContext();
90  }
91 
93  return new TransactionContext(this);
94  }
95  }
96 }
The context of a single database within a system.
The execution context of a database system, that is defining the configurations and the components us...
An object that creates and manages the IStore objects that the database engine uses to represent itse...
Definition: IStoreSystem.cs:31
This is the context of a database system, that handles the configurations and services used by all th...
Defines the contract for the configuration node of a component within the system or of the system its...
override void Dispose(bool disposing)
ITransactionContext CreateTransactionContext()
Creates a context to handle services and variables in the scope of a transaction. ...
DatabaseContext(ISystemContext systemContext, IConfiguration configuration)
TransactionContext CreateTransactionContext()
Creates a context to handle services and variables in the scope of a transaction. ...
static IConfiguration CreateSimpleConfig(ISystemContext systemContext, string dbName)
Configuration(bool isRoot)
Constructs the Configuration.