1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.DataAccess.Interfaces;
|
---|
27 | using HeuristicLab.PluginInfrastructure;
|
---|
28 | using System.Data.Common;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.DataAccess.ADOHelper {
|
---|
31 | public class Session: ISession {
|
---|
32 | private static DiscoveryService discoveryService =
|
---|
33 | new DiscoveryService();
|
---|
34 |
|
---|
35 | private SessionFactory factory;
|
---|
36 |
|
---|
37 | private ITransactionManager transManager;
|
---|
38 |
|
---|
39 | private DbConnection connection;
|
---|
40 |
|
---|
41 | private IDictionary<Guid, object> adapters =
|
---|
42 | new Dictionary<Guid, object>();
|
---|
43 |
|
---|
44 | public Session(SessionFactory factory,
|
---|
45 | ITransactionManager transManager) {
|
---|
46 | this.factory = factory;
|
---|
47 | this.transManager = transManager;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public DbConnection Connection {
|
---|
51 | get {
|
---|
52 | if (connection == null) {
|
---|
53 | connection = factory.CreateConnection();
|
---|
54 | }
|
---|
55 |
|
---|
56 | return connection;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | #region ISession Members
|
---|
61 | public ITransaction BeginTransaction() {
|
---|
62 | ITransaction trans = transManager.BeginTransaction();
|
---|
63 | if(trans is Transaction)
|
---|
64 | ((Transaction)trans).Connection = Connection;
|
---|
65 |
|
---|
66 | return trans;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public ITransaction GetTransactionForCurrentThread() {
|
---|
70 | ITransaction trans = transManager.GetTransactionForCurrentThread();
|
---|
71 | if (trans != null && trans is Transaction)
|
---|
72 | ((Transaction)trans).Connection = Connection;
|
---|
73 |
|
---|
74 | return trans;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public IDataAdapter<ObjT> GetDataAdapter<ObjT>()
|
---|
78 | where ObjT : IPersistableObject {
|
---|
79 | lock(this) {
|
---|
80 | Guid adapterId = typeof(IDataAdapter<ObjT>).GUID;
|
---|
81 |
|
---|
82 | if (!adapters.ContainsKey(adapterId)) {
|
---|
83 | IDataAdapter<ObjT> adapter =
|
---|
84 | discoveryService.GetInstances<IDataAdapter<ObjT>>()[0];
|
---|
85 |
|
---|
86 | adapter.Session = this;
|
---|
87 |
|
---|
88 | adapters.Add(adapterId, adapter);
|
---|
89 | }
|
---|
90 |
|
---|
91 | return adapters[adapterId] as IDataAdapter<ObjT>;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | public T GetDataAdapter<ObjT, T>()
|
---|
96 | where ObjT : IPersistableObject
|
---|
97 | where T : class, IDataAdapter<ObjT>
|
---|
98 | {
|
---|
99 | lock (this) {
|
---|
100 | Guid adapterId = typeof(T).GUID;
|
---|
101 |
|
---|
102 | if (!adapters.ContainsKey(adapterId)) {
|
---|
103 | T adapter =
|
---|
104 | discoveryService.GetInstances<T>()[0];
|
---|
105 |
|
---|
106 | adapter.Session = this;
|
---|
107 |
|
---|
108 | adapters.Add(adapterId, adapter);
|
---|
109 | }
|
---|
110 |
|
---|
111 | return adapters[adapterId] as T;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void EndSession() {
|
---|
116 | if(connection.State == System.Data.ConnectionState.Open)
|
---|
117 | connection.Close();
|
---|
118 | factory.EndSession(this);
|
---|
119 | }
|
---|
120 |
|
---|
121 | #endregion
|
---|
122 | }
|
---|
123 | }
|
---|