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.EF6/ObjectContextManager.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ObjectContextManager.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 </summary>
7//-----------------------------------------------------------------------
8
9#if !MONO
10using System;
12using System.Data.Entity.Core.Objects;
13using Csla.Properties;
14
15namespace Csla.Data.EF6
16{
36 public class ObjectContextManager<C> : IDisposable where C : ObjectContext
37 {
38 private static object _lock = new object();
39 private C _context;
40 private string _connectionString;
41 private string _label;
42
50 public static ObjectContextManager<C> GetManager(string database)
51 {
52 return GetManager(database, true);
53 }
54
63 public static ObjectContextManager<C> GetManager(string database, string label)
64 {
65 return GetManager(database, true, label);
66 }
67
82 public static ObjectContextManager<C> GetManager(string database, bool isDatabaseName)
83 {
84 return GetManager(database, isDatabaseName, "default");
85 }
86
102 public static ObjectContextManager<C> GetManager(string database, bool isDatabaseName, string label)
103 {
104 if (isDatabaseName)
105 {
106 var connection = ConfigurationManager.ConnectionStrings[database];
107 if (connection == null)
108 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
109 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
110 if (string.IsNullOrEmpty(conn))
111 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
112 database = conn;
113 }
114
115 lock (_lock)
116 {
117 var contextLabel = GetContextName(database, label);
118 ObjectContextManager<C> mgr = null;
119 if (ApplicationContext.LocalContext.Contains(contextLabel))
120 {
121 mgr = (ObjectContextManager<C>)(ApplicationContext.LocalContext[contextLabel]);
122
123 }
124 else
125 {
126 mgr = new ObjectContextManager<C>(database, label);
127 ApplicationContext.LocalContext[contextLabel] = mgr;
128 }
129 mgr.AddRef();
130 return mgr;
131 }
132 }
133
134 private ObjectContextManager(string connectionString, string label)
135 {
136 _label = label;
137 _connectionString = connectionString;
138
139 _context = (C)(Activator.CreateInstance(typeof(C), connectionString));
140 _context.Connection.Open();
141 }
142
143 private static string GetContextName(string connectionString, string label)
144 {
145 return "__octx:" + label + "-" + connectionString;
146 }
147
148
153 {
154 get
155 {
156 return _context;
157 }
158 }
159
160#region Reference counting
161
162 private int _refCount;
163
168 public int RefCount
169 {
170 get { return _refCount; }
171 }
172
173 private void AddRef()
174 {
175 _refCount += 1;
176 }
177
178 private void DeRef()
179 {
180
181 lock (_lock)
182 {
183 _refCount -= 1;
184 if (_refCount == 0)
185 {
186 _context.Connection.Close();
187 _context.Dispose();
188 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
189 }
190 }
191
192 }
193
194 #endregion
195
196#region IDisposable
197
203 public void Dispose()
204 {
205 DeRef();
206 }
207
208 #endregion
209
210 }
211}
212#endif
Provides an automated way to reuse Entity Framework object context objects within the context of a si...
static ObjectContextManager< C > GetManager(string database)
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, bool isDatabaseName, string label)
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.
static ObjectContextManager< C > GetManager(string database, bool isDatabaseName)
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}).