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/ConnectionManager.cs
Go to the documentation of this file.
1#if !NETSTANDARD2_0 && !NET5_0
2//-----------------------------------------------------------------------
3// <copyright file="ConnectionManager.cs" company="Marimer LLC">
4// Copyright (c) Marimer LLC. All rights reserved.
5// Website: https://cslanet.com
6// </copyright>
7// <summary>Provides an automated way to reuse open</summary>
8//-----------------------------------------------------------------------
9using System;
11using System.Data;
12using System.Data.Common;
13using Csla.Properties;
14
15namespace Csla.Data
16{
31 public class ConnectionManager : IDisposable
32 {
33 private static object _lock = new object();
34 private IDbConnection _connection;
35 private string _connectionString;
36 private string _label;
37
45 public static ConnectionManager GetManager(string database)
46 {
47 return GetManager(database, true);
48 }
49
58 public static ConnectionManager GetManager(string database, string label)
59 {
60 return GetManager(database, true, label);
61 }
62
77 public static ConnectionManager GetManager(string database, bool isDatabaseName)
78 {
79 return GetManager(database, isDatabaseName, "default");
80 }
81
97 public static ConnectionManager GetManager(string database, bool isDatabaseName, string label)
98 {
99 if (isDatabaseName)
100 {
101 var connection = ConfigurationManager.ConnectionStrings[database];
102 if (connection == null)
103 throw new ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
104
105 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
106 if (string.IsNullOrEmpty(conn))
107 throw new ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
108 database = conn;
109 }
110
111 ConnectionManager mgr = null;
112 var ctxName = GetContextName(database, label);
113 lock (_lock)
114 {
115 var cached = ApplicationContext.LocalContext.GetValueOrNull(ctxName);
116 if (cached != null)
117 {
118 mgr = (ConnectionManager)cached;
119 mgr.AddRef();
120 }
121 }
122
123 if (mgr == null)
124 {
125 mgr = new ConnectionManager(database, label);
126 lock (_lock)
127 {
128 ApplicationContext.LocalContext[ctxName] = mgr;
129 mgr.AddRef();
130 }
131 }
132 return mgr;
133 }
134
135 private ConnectionManager(string connectionString, string label)
136 {
137 _label = label;
138 _connectionString = connectionString;
139
140#if NETSTANDARD2_0 || NET5_0
141 _connection = new System.Data.SqlClient.SqlConnection(connectionString);
142 _connection.Open();
143#else
144 string provider = ConfigurationManager.AppSettings["CslaDbProvider"];
145 if (string.IsNullOrEmpty(provider))
146 provider = "System.Data.SqlClient";
147
148 DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
149
150 // open connection
151 _connection = factory.CreateConnection();
152 _connection.ConnectionString = connectionString;
153 _connection.Open();
154#endif
155 }
156
157 private static string GetContextName(string connectionString, string label)
158 {
159 return "__db:" + label + "-" + connectionString;
160 }
161
167 public IDbConnection Connection
168 {
169 get
170 {
171 return _connection;
172 }
173 }
174
175 private int _refCount;
176
181 public int RefCount
182 {
183 get { return _refCount; }
184 }
185
186 private void AddRef()
187 {
188 _refCount += 1;
189 }
190
191 private void DeRef()
192 {
193 _refCount -= 1;
194 if (_refCount == 0)
195 {
196 _connection.Close();
197 _connection.Dispose();
198 lock (_lock)
199 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
200 }
201 }
202
208 public void Dispose()
209 {
210 DeRef();
211 }
212 }
213}
214#endif
Exception thrown due to configuration errors.
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 GetManager(string database)
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, bool isDatabaseName, string label)
Gets the ConnectionManager object for the specified database.
int RefCount
Gets the current reference count for this object.
static ConnectionManager GetManager(string database, string label)
Gets the ConnectionManager object for the specified database.
IDbConnection Connection
Dispose object, dereferencing or disposing the connection 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}).