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.
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 [Obsolete("Use dependency injection", false)]
33 public class ConnectionManager<C> : IDisposable, Core.IUseApplicationContext
34 where C : IDbConnection, new()
35 {
36 private static object _lock = new object();
37 private C _connection;
38 private string _connectionString;
39 private string _label;
40
41 private ApplicationContext ApplicationContext { get; set; }
42 ApplicationContext Core.IUseApplicationContext.ApplicationContext { get => ApplicationContext; set => ApplicationContext = value; }
43
51 public ConnectionManager<C> GetManager(string database)
52 {
53 return GetManager(database, true);
54 }
55
64 public ConnectionManager<C> GetManager(string database, string label)
65 {
66 return GetManager(database, true, label);
67 }
68
83 public ConnectionManager<C> GetManager(string database, bool isDatabaseName)
84 {
85 return GetManager(database, isDatabaseName, "default");
86 }
87
103 public ConnectionManager<C> GetManager(string database, bool isDatabaseName, string label)
104 {
105 if (isDatabaseName)
106 {
107#if NETSTANDARD2_0 || NET5_0 || NET6_0
108 throw new NotSupportedException("isDatabaseName==true");
109#else
110 var connection = ConfigurationManager.ConnectionStrings[database];
111 if (connection == null)
112 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
113
114 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
115 if (string.IsNullOrEmpty(conn))
116 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
117 database = conn;
118#endif
119 }
120
121 ConnectionManager<C> mgr = null;
122 var ctxName = GetContextName(database, label);
123 lock (_lock)
124 {
125 var cached = ApplicationContext.LocalContext.GetValueOrNull(ctxName);
126 if (cached != null)
127 {
128 mgr = (ConnectionManager<C>)cached;
129 mgr.AddRef();
130 }
131 }
132
133 if (mgr == null)
134 {
135 mgr = new ConnectionManager<C>(database, label);
136 lock (_lock)
137 {
138 ApplicationContext.LocalContext[ctxName] = mgr;
139 mgr.AddRef();
140 }
141 }
142 return mgr;
143 }
144
145 private ConnectionManager(string connectionString, string label)
146 {
147 _label = label;
148 _connectionString = connectionString;
149
150 // open connection
151 _connection = new C { ConnectionString = connectionString };
152 _connection.Open();
153 }
154
155 private static string GetContextName(string connectionString, string label)
156 {
157 return "__db:" + label + "-" + connectionString;
158 }
159
163 public C Connection
164 {
165 get
166 {
167 return _connection;
168 }
169 }
170
171 private int _refCount;
172
177 public int RefCount
178 {
179 get { return _refCount; }
180 }
181
182 private void AddRef()
183 {
184 _refCount += 1;
185 }
186
187 private void DeRef()
188 {
189 _refCount -= 1;
190 if (_refCount == 0)
191 {
192 _connection.Close();
193 _connection.Dispose();
194 lock (_lock)
195 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
196 }
197 }
198
204 public void Dispose()
205 {
206 DeRef();
207 }
208 }
209}
Provides consistent context information between the client and server DataPortal objects.
ApplicationContext(ApplicationContextAccessor applicationContextAccessor)
Creates a new instance of the type
ContextDictionary LocalContext
Returns the application-specific context data that is local to the current AppDomain.
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.
ConnectionManager< C > GetManager(string database, bool isDatabaseName, string label)
Gets the ConnectionManager object for the specified database.
ConnectionManager< C > GetManager(string database, bool isDatabaseName)
Gets the ConnectionManager object for the specified database.
ConnectionManager GetManager(string database)
Gets the ConnectionManager object for the specified database.
ConnectionManager< C > GetManager(string database, string label)
Gets the ConnectionManager object for the specified database.
ConnectionManager< C > 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}).