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/ObjectContextManager.cs
Go to the documentation of this file.
1#if !NETSTANDARD2_0 && !NET5_0
2//-----------------------------------------------------------------------
3// <copyright file="ObjectContextManager.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.Objects;
12using Csla.Properties;
13
14namespace Csla.Data
15{
35 public class ObjectContextManager<C> : IDisposable where C : ObjectContext
36 {
37 private static object _lock = new object();
38 private C _context;
39 private string _connectionString;
40 private string _label;
41
49 public static ObjectContextManager<C> GetManager(string database)
50 {
51 return GetManager(database, true);
52 }
53
62 public static ObjectContextManager<C> GetManager(string database, string label)
63 {
64 return GetManager(database, true, label);
65 }
66
81 public static ObjectContextManager<C> GetManager(string database, bool isDatabaseName)
82 {
83 return GetManager(database, isDatabaseName, "default");
84 }
85
101 public static ObjectContextManager<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 ObjectContextManager<C> mgr = null;
118 if (ApplicationContext.LocalContext.Contains(contextLabel))
119 {
120 mgr = (ObjectContextManager<C>)(ApplicationContext.LocalContext[contextLabel]);
121
122 }
123 else
124 {
125 mgr = new ObjectContextManager<C>(database, label);
126 ApplicationContext.LocalContext[contextLabel] = mgr;
127 }
128 mgr.AddRef();
129 return mgr;
130 }
131 }
132
133 private ObjectContextManager(string connectionString, string label)
134 {
135 _label = label;
136 _connectionString = connectionString;
137
138 _context = (C)(Reflection.MethodCaller.CreateInstance(typeof(C), connectionString));
139 _context.Connection.Open();
140 }
141
142 private static string GetContextName(string connectionString, string label)
143 {
144 return "__octx:" + label + "-" + connectionString;
145 }
146
147
152 {
153 get
154 {
155 return _context;
156 }
157 }
158
159#region Reference counting
160
161 private int _refCount;
162
167 public int RefCount
168 {
169 get { return _refCount; }
170 }
171
172 private void AddRef()
173 {
174 _refCount += 1;
175 }
176
177 private void DeRef()
178 {
179
180 lock (_lock)
181 {
182 _refCount -= 1;
183 if (_refCount == 0)
184 {
185 _context.Connection.Close();
186 _context.Dispose();
187 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
188 }
189 }
190
191 }
192
193#endregion
194
195#region IDisposable
196
202 public void Dispose()
203 {
204 DeRef();
205 }
206
207#endregion
208
209 }
210}
211#endif
Provides an automated way to reuse Entity Framework object context objects within the context of a si...
void Dispose()
Dispose object, dereferencing or disposing the context it is managing.
C ObjectContext
Gets the EF object context object.
static ObjectContextManager< C > GetManager(string database, bool isDatabaseName)
Gets the ObjectContextManager object for the specified database.
int RefCount
Gets the current reference count for this object.
static ObjectContextManager< C > GetManager(string database, string label)
Gets the ObjectContextManager object for the specified database.
static ObjectContextManager< C > GetManager(string database)
Gets the ObjectContextManager object for the specified database.
static ObjectContextManager< C > GetManager(string database, bool isDatabaseName, string label)
Gets the ObjectContextManager 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}).