CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
ConnectionManager.cs
Go to the documentation of this file.
1#if !NETSTANDARD2_0 && !NET5_0 && !NET6_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 [Obsolete("Use dependency injection", false)]
32 public class ConnectionManager : IDisposable, Core.IUseApplicationContext
33 {
34 private static object _lock = new object();
35 private IDbConnection _connection;
36 private string _connectionString;
37 private string _label;
38
39 private ApplicationContext ApplicationContext { get; set; }
40 ApplicationContext Core.IUseApplicationContext.ApplicationContext { get => ApplicationContext; set => ApplicationContext = value; }
41
49 public ConnectionManager GetManager(string database)
50 {
51 return GetManager(database, true);
52 }
53
62 public ConnectionManager GetManager(string database, string label)
63 {
64 return GetManager(database, true, label);
65 }
66
81 public ConnectionManager GetManager(string database, bool isDatabaseName)
82 {
83 return GetManager(database, isDatabaseName, "default");
84 }
85
101 public ConnectionManager GetManager(string database, bool isDatabaseName, string label)
102 {
103 if (isDatabaseName)
104 {
105 var connection = ConfigurationManager.ConnectionStrings[database];
106 if (connection == null)
107 throw new ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
108
109 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
110 if (string.IsNullOrEmpty(conn))
111 throw new ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
112 database = conn;
113 }
114
115 ConnectionManager mgr = null;
116 var ctxName = GetContextName(database, label);
117 lock (_lock)
118 {
119 var cached = ApplicationContext.LocalContext.GetValueOrNull(ctxName);
120 if (cached != null)
121 {
122 mgr = (ConnectionManager)cached;
123 mgr.AddRef();
124 }
125 }
126
127 if (mgr == null)
128 {
129 mgr = new ConnectionManager(database, label);
130 lock (_lock)
131 {
132 ApplicationContext.LocalContext[ctxName] = mgr;
133 mgr.AddRef();
134 }
135 }
136 return mgr;
137 }
138
139#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
140 internal static string DbProvider { get; set; } = "System.Data.SqlClient";
141#endif
142
143 private ConnectionManager(string connectionString, string label)
144 {
145 _label = label;
146 _connectionString = connectionString;
147
148#if NETSTANDARD2_0 || NET5_0 || NET6_0
149 _connection = new System.Data.SqlClient.SqlConnection(connectionString);
150 _connection.Open();
151#else
152
153 DbProviderFactory factory = DbProviderFactories.GetFactory(DbProvider);
154
155 // open connection
156 _connection = factory.CreateConnection();
157 _connection.ConnectionString = connectionString;
158 _connection.Open();
159#endif
160 }
161
162 private static string GetContextName(string connectionString, string label)
163 {
164 return "__db:" + label + "-" + connectionString;
165 }
166
172 public IDbConnection Connection
173 {
174 get
175 {
176 return _connection;
177 }
178 }
179
180 private int _refCount;
181
186 public int RefCount
187 {
188 get { return _refCount; }
189 }
190
191 private void AddRef()
192 {
193 _refCount += 1;
194 }
195
196 private void DeRef()
197 {
198 _refCount -= 1;
199 if (_refCount == 0)
200 {
201 _connection.Close();
202 _connection.Dispose();
203 lock (_lock)
204 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
205 }
206 }
207
213 public void Dispose()
214 {
215 DeRef();
216 }
217 }
218}
219#endif
Provides consistent context information between the client and server DataPortal objects.
ContextDictionary LocalContext
Returns the application-specific context data that is local to the current AppDomain.
Exception thrown due to configuration errors.
Provides an automated way to reuse open database connections within the context of a single data port...
ConnectionManager GetManager(string database, bool isDatabaseName, string label)
Gets the ConnectionManager object for the specified database.
void Dispose()
Dispose object, dereferencing or disposing the connection it is managing.
ConnectionManager GetManager(string database, bool isDatabaseName)
Gets the ConnectionManager object for the specified database.
ConnectionManager GetManager(string database, string label)
Gets the ConnectionManager object for the specified database.
ConnectionManager GetManager(string database)
Gets the ConnectionManager object for the specified database.
int RefCount
Gets the current reference count for this object.
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}).