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.
ContextManager.cs
Go to the documentation of this file.
1#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
2//-----------------------------------------------------------------------
3// <copyright file="ContextManager.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 </summary>
8//-----------------------------------------------------------------------
9using System;
11using System.Data.Linq;
12
13using Csla.Properties;
14
15namespace Csla.Data
16{
35 [Obsolete("Use dependency injection", false)]
36 public class ContextManager<C> : IDisposable, Core.IUseApplicationContext
37 where C : DataContext
38 {
39 private static object _lock = new object();
40 private C _context;
41 private string _connectionString;
42 private string _label;
43
44 private ApplicationContext ApplicationContext { get; set; }
45 ApplicationContext Core.IUseApplicationContext.ApplicationContext { get => ApplicationContext; set => ApplicationContext = value; }
46
54 public ContextManager<C> GetManager(string database)
55 {
56 return GetManager(database, true);
57 }
58
67 public ContextManager<C> GetManager(string database, string label)
68 {
69 return GetManager(database, true, label);
70 }
71
86 public ContextManager<C> GetManager(string database, bool isDatabaseName)
87 {
88 return GetManager(database, isDatabaseName, "default");
89 }
90
106 public ContextManager<C> GetManager(string database, bool isDatabaseName, string label)
107 {
108 if (isDatabaseName)
109 {
110 var connection = ConfigurationManager.ConnectionStrings[database];
111 if (connection == null)
112 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
113 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
114 if (string.IsNullOrEmpty(conn))
115 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
116 database = conn;
117 }
118
119 lock (_lock)
120 {
121 var contextLabel = GetContextName(database, label);
122 ContextManager<C> mgr = null;
123 if (ApplicationContext.LocalContext.Contains(contextLabel))
124 {
126
127 }
128 else
129 {
130 mgr = new ContextManager<C>(database, label);
131 ApplicationContext.LocalContext[contextLabel] = mgr;
132 }
133 mgr.AddRef();
134 return mgr;
135 }
136 }
137
138 private ContextManager(string connectionString, string label)
139 {
140 _label = label;
141 _connectionString = connectionString;
142
143 _context = (C)(ApplicationContext.CreateInstanceDI(typeof(C), connectionString));
144
145 }
146
147 private static string GetContextName(string connectionString, string label)
148 {
149 return "__ctx:" + label + "-" + connectionString;
150 }
151
155 public C DataContext
156 {
157 get
158 {
159 return _context;
160 }
161 }
162
163#region Reference counting
164
165 private int _refCount;
166
171 public int RefCount
172 {
173 get { return _refCount; }
174 }
175
176 private void AddRef()
177 {
178 _refCount += 1;
179 }
180
181 private void DeRef()
182 {
183
184 lock (_lock)
185 {
186 _refCount -= 1;
187 if (_refCount == 0)
188 {
189 _context.Dispose();
190 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
191 }
192 }
193
194 }
195
196#endregion
197
198#region IDisposable
199
205 public void Dispose()
206 {
207 DeRef();
208 }
209
210#endregion
211
212 }
213}
214#endif
Provides consistent context information between the client and server DataPortal objects.
object CreateInstanceDI(Type objectType, params object[] parameters)
Creates an object using 'Activator.CreateInstance' using service provider (if one is available) to po...
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 LINQ data context objects within the context of a single data port...
int RefCount
Gets the current reference count for this object.
ContextManager< C > GetManager(string database, bool isDatabaseName, string label)
Gets the ContextManager object for the specified database.
void Dispose()
Dispose object, dereferencing or disposing the context it is managing.
C DataContext
Gets the LINQ data context object.
ContextManager< C > GetManager(string database, string label)
Gets the ContextManager object for the specified database.
ContextManager< C > GetManager(string database)
Gets the ContextManager object for the specified database.
ContextManager< C > GetManager(string database, bool isDatabaseName)
Gets the ContextManager 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}).