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.
ContextManager.cs
Go to the documentation of this file.
1#if !NETSTANDARD2_0 && !NET5_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 public class ContextManager<C> : IDisposable where C : DataContext
36 {
37 private static object _lock = new object();
38 private C _context;
39 private string _connectionString;
40 private string _label;
41
49 public static ContextManager<C> GetManager(string database)
50 {
51 return GetManager(database, true);
52 }
53
62 public static ContextManager<C> GetManager(string database, string label)
63 {
64 return GetManager(database, true, label);
65 }
66
81 public static ContextManager<C> GetManager(string database, bool isDatabaseName)
82 {
83 return GetManager(database, isDatabaseName, "default");
84 }
85
101 public static ContextManager<C> 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 System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
108 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
109 if (string.IsNullOrEmpty(conn))
110 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
111 database = conn;
112 }
113
114 lock (_lock)
115 {
116 var contextLabel = GetContextName(database, label);
117 ContextManager<C> mgr = null;
118 if (ApplicationContext.LocalContext.Contains(contextLabel))
119 {
120 mgr = (ContextManager<C>)(ApplicationContext.LocalContext[contextLabel]);
121
122 }
123 else
124 {
125 mgr = new ContextManager<C>(database, label);
126 ApplicationContext.LocalContext[contextLabel] = mgr;
127 }
128 mgr.AddRef();
129 return mgr;
130 }
131 }
132
133 private ContextManager(string connectionString, string label)
134 {
135 _label = label;
136 _connectionString = connectionString;
137
138 _context = (C)(Reflection.MethodCaller.CreateInstance(typeof(C), connectionString));
139
140 }
141
142 private static string GetContextName(string connectionString, string label)
143 {
144 return "__ctx:" + label + "-" + connectionString;
145 }
146
150 public C DataContext
151 {
152 get
153 {
154 return _context;
155 }
156 }
157
158#region Reference counting
159
160 private int _refCount;
161
166 public int RefCount
167 {
168 get { return _refCount; }
169 }
170
171 private void AddRef()
172 {
173 _refCount += 1;
174 }
175
176 private void DeRef()
177 {
178
179 lock (_lock)
180 {
181 _refCount -= 1;
182 if (_refCount == 0)
183 {
184 _context.Dispose();
185 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
186 }
187 }
188
189 }
190
191#endregion
192
193#region IDisposable
194
200 public void Dispose()
201 {
202 DeRef();
203 }
204
205#endregion
206
207 }
208}
209#endif
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.
static ContextManager< C > GetManager(string database, bool isDatabaseName)
Gets the ContextManager object for the specified database.
static ContextManager< C > GetManager(string database, 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.
static ContextManager< C > GetManager(string database)
Gets the ContextManager object for the specified database.
static ContextManager< C > GetManager(string database, bool isDatabaseName, string label)
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}).