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.
ObjectContextManager.cs
Go to the documentation of this file.
1#if !NETSTANDARD2_0 && !NET5_0 && !NET6_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, Core.IUseApplicationContext
36 where C : ObjectContext
37 {
38 private static object _lock = new object();
39 private C _context;
40 private string _connectionString;
41 private string _label;
42
43 ApplicationContext Core.IUseApplicationContext.ApplicationContext { get => ApplicationContext; set => ApplicationContext = value; }
44 private ApplicationContext ApplicationContext { get; set; }
45
53 public ObjectContextManager<C> GetManager(string database)
54 {
55 return GetManager(database, true);
56 }
57
66 public ObjectContextManager<C> GetManager(string database, string label)
67 {
68 return GetManager(database, true, label);
69 }
70
85 public ObjectContextManager<C> GetManager(string database, bool isDatabaseName)
86 {
87 return GetManager(database, isDatabaseName, "default");
88 }
89
105 public ObjectContextManager<C> GetManager(string database, bool isDatabaseName, string label)
106 {
107 if (isDatabaseName)
108 {
109 var connection = ConfigurationManager.ConnectionStrings[database];
110 if (connection == null)
111 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
112 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
113 if (string.IsNullOrEmpty(conn))
114 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
115 database = conn;
116 }
117
118 lock (_lock)
119 {
120 var contextLabel = GetContextName(database, label);
121 ObjectContextManager<C> mgr = null;
122 if (ApplicationContext.LocalContext.Contains(contextLabel))
123 {
125
126 }
127 else
128 {
129 mgr = new ObjectContextManager<C>(database, label);
130 ApplicationContext.LocalContext[contextLabel] = mgr;
131 }
132 mgr.AddRef();
133 return mgr;
134 }
135 }
136
137 private ObjectContextManager(string connectionString, string label)
138 {
139 _label = label;
140 _connectionString = connectionString;
141
142 _context = (C)(ApplicationContext.CreateInstanceDI(typeof(C), connectionString));
143 _context.Connection.Open();
144 }
145
146 private static string GetContextName(string connectionString, string label)
147 {
148 return "__octx:" + label + "-" + connectionString;
149 }
150
151
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.Connection.Close();
190 _context.Dispose();
191 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
192 }
193 }
194
195 }
196
197#endregion
198
199#region IDisposable
200
206 public void Dispose()
207 {
208 DeRef();
209 }
210
211#endregion
212
213 }
214}
215#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 Entity Framework object context objects within the context of a si...
ObjectContextManager< C > GetManager(string database, string label)
Gets the ObjectContextManager object for the specified database.
ObjectContextManager< C > GetManager(string database, bool isDatabaseName)
Gets the ObjectContextManager object for the specified database.
void Dispose()
Dispose object, dereferencing or disposing the context it is managing.
C ObjectContext
Gets the EF object context object.
ObjectContextManager< C > GetManager(string database)
Gets the ObjectContextManager object for the specified database.
int RefCount
Gets the current reference count for this object.
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}).