CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
ConnectionManagerT.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ConnectionManagerT.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Provides an automated way to reuse open</summary>
7//-----------------------------------------------------------------------
8using System;
10using System.Data;
11using Csla.Properties;
12
13namespace Csla.Data
14{
32 public class ConnectionManager<C> : IDisposable where C : IDbConnection, new()
33 {
34 private static object _lock = new object();
35 private C _connection;
36 private string _connectionString;
37 private string _label;
38
46 public static ConnectionManager<C> GetManager(string database)
47 {
48 return GetManager(database, true);
49 }
50
59 public static ConnectionManager<C> GetManager(string database, string label)
60 {
61 return GetManager(database, true, label);
62 }
63
78 public static ConnectionManager<C> GetManager(string database, bool isDatabaseName)
79 {
80 return GetManager(database, isDatabaseName, "default");
81 }
82
98 public static ConnectionManager<C> GetManager(string database, bool isDatabaseName, string label)
99 {
100 if (isDatabaseName)
101 {
102#if NETSTANDARD2_0 || NET5_0
103 throw new NotSupportedException("isDatabaseName==true");
104#else
105 var connection = ConfigurationManager.ConnectionStrings[database];
106 if (connection == null)
107 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
108
109 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
110 if (string.IsNullOrEmpty(conn))
111 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
112 database = conn;
113#endif
114 }
115
116 ConnectionManager<C> mgr = null;
117 var ctxName = GetContextName(database, label);
118 lock (_lock)
119 {
120 var cached = ApplicationContext.LocalContext.GetValueOrNull(ctxName);
121 if (cached != null)
122 {
123 mgr = (ConnectionManager<C>)cached;
124 mgr.AddRef();
125 }
126 }
127
128 if (mgr == null)
129 {
130 mgr = new ConnectionManager<C>(database, label);
131 lock (_lock)
132 {
133 ApplicationContext.LocalContext[ctxName] = mgr;
134 mgr.AddRef();
135 }
136 }
137 return mgr;
138 }
139
140 private ConnectionManager(string connectionString, string label)
141 {
142 _label = label;
143 _connectionString = connectionString;
144
145 // open connection
146 _connection = new C { ConnectionString = connectionString };
147 _connection.Open();
148 }
149
150 private static string GetContextName(string connectionString, string label)
151 {
152 return "__db:" + label + "-" + connectionString;
153 }
154
158 public C Connection
159 {
160 get
161 {
162 return _connection;
163 }
164 }
165
166 private int _refCount;
167
172 public int RefCount
173 {
174 get { return _refCount; }
175 }
176
177 private void AddRef()
178 {
179 _refCount += 1;
180 }
181
182 private void DeRef()
183 {
184 _refCount -= 1;
185 if (_refCount == 0)
186 {
187 _connection.Close();
188 _connection.Dispose();
189 lock (_lock)
190 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
191 }
192 }
193
199 public void Dispose()
200 {
201 DeRef();
202 }
203 }
204}
Provides an automated way to reuse open database connections within the context of a single data port...
void Dispose()
Dispose object, dereferencing or disposing the connection it is managing.
static ConnectionManager< C > GetManager(string database, string label)
Gets the ConnectionManager object for the specified database.
static ConnectionManager GetManager(string database)
Gets the ConnectionManager object for the specified database.
int RefCount
Gets the current reference count for this object.
static ConnectionManager< C > GetManager(string database, bool isDatabaseName)
Gets the ConnectionManager object for the specified database.
static ConnectionManager< C > GetManager(string database)
Gets the ConnectionManager object for the specified database.
IDbConnection Connection
Dispose object, dereferencing or disposing the connection it is managing.
static ConnectionManager< C > GetManager(string database, bool isDatabaseName, string label)
Gets the ConnectionManager object for the specified database.
A strongly-typed resource class, for looking up localized strings, etc.
static string DatabaseNameNotFound
Looks up a localized string similar to Database name not found in config file ({0}).