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.
Csla.Data.SqlClient.Fx/ConnectionManager.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ConnectionManager.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;
11#if NETFX
12using System.Data.SqlClient;
13#else
14using Microsoft.Data.SqlClient;
15#endif
16using Csla.Properties;
17
19{
34 [Obsolete("Use dependency injection instead", false)]
35 public class ConnectionManager : IDisposable
36 {
37 private static readonly object _lock = new object();
38 private readonly string _connectionString;
39 private readonly string _label;
40
48 public static ConnectionManager GetManager(string database)
49 {
50 return GetManager(database, true);
51 }
52
61 public static ConnectionManager GetManager(string database, string label)
62 {
63 return GetManager(database, true, label);
64 }
65
80 public static ConnectionManager GetManager(string database, bool isDatabaseName)
81 {
82 return GetManager(database, isDatabaseName, "default");
83 }
84
100 public static ConnectionManager GetManager(string database, bool isDatabaseName, string label)
101 {
102 if (isDatabaseName)
103 {
104 var connection = ConfigurationManager.ConnectionStrings[database];
105 if (connection == null)
106 throw new ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
107
108 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
109 if (string.IsNullOrEmpty(conn))
110 throw new ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
111 database = conn;
112 }
113
114 ConnectionManager mgr = null;
115 var ctxName = GetContextName(database, label);
116 lock (_lock)
117 {
118 var cached = ApplicationContext.LocalContext.GetValueOrNull(ctxName);
119 if (cached != null)
120 {
121 mgr = (ConnectionManager)cached;
122 mgr.AddRef();
123 }
124 }
125
126 if (mgr == null)
127 {
128 mgr = new ConnectionManager(database, label);
129 lock (_lock)
130 {
131 ApplicationContext.LocalContext[ctxName] = mgr;
132 mgr.AddRef();
133 }
134 }
135 return mgr;
136 }
137
138 private ConnectionManager(string connectionString, string label)
139 {
140 _label = label;
141 _connectionString = connectionString;
142
143 Connection = new SqlConnection(connectionString);
144 Connection.Open();
145 }
146
147 private static string GetContextName(string connectionString, string label)
148 {
149 return "__db:" + label + "-" + connectionString;
150 }
151
157 public IDbConnection Connection { get; }
158
163 public int RefCount { get; private set; }
164
165 private void AddRef()
166 {
167 RefCount += 1;
168 }
169
170 private void DeRef()
171 {
172 RefCount -= 1;
173 if (RefCount == 0)
174 {
175 Connection.Close();
176 Connection.Dispose();
177 lock (_lock)
178 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
179 }
180 }
181
187 public void Dispose()
188 {
189 Dispose(true);
190 GC.SuppressFinalize(this);
191 }
192
198 protected virtual void Dispose(bool p)
199 {
200 DeRef();
201 }
202 }
203}
Exception thrown due to configuration errors.
Provides an automated way to reuse open database connections within the context of a single data port...
static ConnectionManager GetManager(string database, bool isDatabaseName, string label)
Gets the ConnectionManager object for the specified database.
static ConnectionManager GetManager(string database, bool isDatabaseName)
Gets the ConnectionManager object for the specified database.
static ConnectionManager GetManager(string database)
Gets the ConnectionManager object for the specified database.
static ConnectionManager GetManager(string database, string label)
Gets the ConnectionManager object for the specified database.
int RefCount
Gets the current reference count for this object.
void Dispose()
Dispose object, dereferencing or disposing the connection it is managing.
IDbConnection Connection
Dispose object, dereferencing or disposing the connection it is managing.
virtual void Dispose(bool p)
Dispose object, dereferencing or disposing the context it is managing.
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}).