comparison Source/DataAccess/DataAccessorBase.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections;
3 using System.Data;
4
5 using BLToolkit.Aspects;
6 using BLToolkit.Data;
7 using BLToolkit.Mapping;
8 using BLToolkit.Properties;
9 using BLToolkit.Reflection.Extension;
10
11 namespace BLToolkit.DataAccess
12 {
13 public abstract class DataAccessorBase
14 {
15 #region Constructors
16
17 [System.Diagnostics.DebuggerStepThrough]
18 protected DataAccessorBase()
19 {
20 }
21
22 [System.Diagnostics.DebuggerStepThrough]
23 protected DataAccessorBase(DbManager dbManager)
24 {
25 SetDbManager(dbManager, false);
26 }
27
28 [System.Diagnostics.DebuggerStepThrough]
29 protected DataAccessorBase(DbManager dbManager, bool dispose)
30 {
31 SetDbManager(dbManager, dispose);
32 }
33
34 #endregion
35
36 #region Public Members
37
38 [NoInterception, System.Diagnostics.DebuggerStepThrough]
39 public virtual DbManager GetDbManager()
40 {
41 return _dbManager ?? CreateDbManager();
42 }
43
44 [NoInterception]
45 protected virtual DbManager CreateDbManager()
46 {
47 return new DbManager();
48 }
49
50 [NoInterception]
51 public virtual void BeginTransaction()
52 {
53 if (_dbManager == null)
54 throw new InvalidOperationException(Resources.DataAccessorBase_NoDbManager);
55
56 _dbManager.BeginTransaction();
57 }
58
59 [NoInterception]
60 public virtual void BeginTransaction(IsolationLevel il)
61 {
62 if (_dbManager == null)
63 throw new InvalidOperationException(Resources.DataAccessorBase_NoDbManager);
64
65 _dbManager.BeginTransaction(il);
66 }
67
68 [NoInterception]
69 public virtual void CommitTransaction()
70 {
71 if (_dbManager == null)
72 throw new InvalidOperationException(Resources.DataAccessorBase_NoDbManager);
73
74 _dbManager.CommitTransaction();
75 }
76
77 [NoInterception]
78 public virtual void RollbackTransaction()
79 {
80 if (_dbManager == null)
81 throw new InvalidOperationException(Resources.DataAccessorBase_NoDbManager);
82
83 _dbManager.RollbackTransaction();
84 }
85
86 private ExtensionList _extensions;
87 public ExtensionList Extensions
88 {
89 get { return _extensions ?? (_extensions = MappingSchema.Extensions); }
90 set { _extensions = value; }
91 }
92
93 private bool _disposeDbManager = true;
94 [NoInterception]
95 public virtual bool DisposeDbManager
96 {
97 get { return _disposeDbManager; }
98 set { _disposeDbManager = value; }
99 }
100
101 private MappingSchema _mappingSchema;
102 public MappingSchema MappingSchema
103 {
104 get { return _mappingSchema ?? (_mappingSchema = _dbManager != null? _dbManager.MappingSchema: Map.DefaultSchema); }
105 set { _mappingSchema = value; }
106 }
107
108 #endregion
109
110 #region Protected Members
111
112 private DbManager _dbManager;
113 protected DbManager DbManager
114 {
115 get { return _dbManager; }
116 }
117
118 protected internal void SetDbManager(DbManager dbManager, bool dispose)
119 {
120 _dbManager = dbManager;
121 _disposeDbManager = dispose;
122 }
123
124 [NoInterception]
125 protected virtual string GetDefaultSpName(string typeName, string actionName)
126 {
127 return typeName == null?
128 actionName:
129 string.Format("{0}_{1}", typeName, actionName);
130 }
131
132 private static readonly Hashtable _actionSproc = new Hashtable();
133
134 [NoInterception]
135 protected virtual string GetSpName(Type type, string actionName)
136 {
137 if (type == null)
138 return GetDefaultSpName(null, actionName);
139
140 string key = type.FullName + "$" + actionName;
141 string sprocName = (string)_actionSproc[key];
142
143 if (sprocName == null)
144 {
145 object[] attrs = type.GetCustomAttributes(typeof(ActionSprocNameAttribute), true);
146
147 foreach (ActionSprocNameAttribute attr in attrs)
148 {
149 if (attr.ActionName == actionName)
150 {
151 sprocName = attr.ProcedureName;
152 break;
153 }
154 }
155
156 if (sprocName == null)
157 sprocName = GetDefaultSpName(GetTableName(type), actionName);
158
159 _actionSproc[key] = sprocName;
160 }
161
162 return sprocName;
163 }
164
165 [NoInterception]
166 protected virtual string GetDatabaseName(Type type)
167 {
168 bool isSet;
169 return MappingSchema.MetadataProvider.GetDatabaseName(type, Extensions, out isSet);
170 }
171
172 [NoInterception]
173 protected virtual string GetOwnerName(Type type)
174 {
175 bool isSet;
176 return MappingSchema.MetadataProvider.GetOwnerName(type, Extensions, out isSet);
177 }
178
179 [NoInterception]
180 protected virtual string GetTableName(Type type)
181 {
182 bool isSet;
183 return MappingSchema.MetadataProvider.GetTableName(type, Extensions, out isSet);
184 }
185
186 [NoInterception]
187 protected virtual void Dispose(DbManager dbManager)
188 {
189 if (dbManager != null && DisposeDbManager)
190 dbManager.Dispose();
191 }
192
193 #endregion
194 }
195 }