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/DbContextManager.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;
11using System.Data.Entity;
12using System.Data.Entity.Infrastructure;
13using System.Data.Entity.Core.Objects;
14
16{
36 public class DbContextManager<C> : IDisposable where C : DbContext
37 {
38 private static object _lock = new object();
39 private C _context;
40 private string _label;
41 private string ContextLabel { get; set; }
42
46
48 {
49 return GetManager(string.Empty);
50 }
51
57 public static DbContextManager<C> GetManager(string database)
58 {
59 return GetManager(database, "default", null);
60 }
61
68 public static DbContextManager<C> GetManager(string database, string label)
69 {
70 return GetManager(database, label, null);
71 }
72
79 public static DbContextManager<C> GetManager(string database, DbCompiledModel model)
80 {
81 return GetManager(database, "default", model);
82 }
83
84
95 public static DbContextManager<C> GetManager(string database, string label, DbCompiledModel model)
96 {
97 lock (_lock)
98 {
99 var contextLabel = GetContextName(database, label);
100 DbContextManager<C> mgr = null;
101 if (ApplicationContext.LocalContext.Contains(contextLabel))
102 {
103 mgr = (DbContextManager<C>)(ApplicationContext.LocalContext[contextLabel]);
104 }
105 else
106 {
107 mgr = new DbContextManager<C>(database, label, model, null);
108 mgr.ContextLabel = contextLabel;
109 ApplicationContext.LocalContext[contextLabel] = mgr;
110 }
111 mgr.AddRef();
112 return mgr;
113 }
114 }
115
123 public static DbContextManager<C> GetManager(ObjectContext context)
124 {
125 return GetManager(context, "default");
126 }
127
136 public static DbContextManager<C> GetManager(ObjectContext context, string label)
137 {
138 lock (_lock)
139 {
140 var contextLabel = GetContextName(context.DefaultContainerName, label);
141 DbContextManager<C> mgr = null;
142 if (ApplicationContext.LocalContext.Contains(contextLabel))
143 {
144 mgr = (DbContextManager<C>)(ApplicationContext.LocalContext[contextLabel]);
145 }
146 else
147 {
148 mgr = new DbContextManager<C>(null, label, null, context);
149 mgr.ContextLabel = contextLabel;
150 ApplicationContext.LocalContext[contextLabel] = mgr;
151 }
152 mgr.AddRef();
153 return mgr;
154 }
155 }
156
157 private DbContextManager(string database, string label, DbCompiledModel model, ObjectContext context)
158 {
159 _label = label;
160
161 if (model != null)
162 {
163 if (!string.IsNullOrEmpty(database))
164 _context = (C)(Activator.CreateInstance(typeof(C), database, model));
165 else
166 _context = (C) (Activator.CreateInstance(typeof (C), model));
167 }
168 else if (context != null)
169 {
170 _context = (C) (Activator.CreateInstance(typeof (C), context, true));
171 }
172 else if (string.IsNullOrEmpty(database))
173 {
174 _context = (C) (Activator.CreateInstance(typeof (C)));
175 }
176 else
177 {
178 _context = (C)(Activator.CreateInstance(typeof(C), database));
179 }
180 }
181
182 private static string GetContextName(string database, string label)
183 {
184 return "__dbctx:" + label + "-" + database;
185 }
186
187
191 public C DbContext
192 {
193 get
194 {
195 return _context;
196 }
197 }
198
199 #region Reference counting
200
201 private int _refCount;
202
207 public int RefCount
208 {
209 get { return _refCount; }
210 }
211
212 private void AddRef()
213 {
214 _refCount += 1;
215 }
216
217 private void DeRef()
218 {
219
220 lock (_lock)
221 {
222 _refCount -= 1;
223 if (_refCount == 0)
224 {
225 _context.Dispose();
226 ApplicationContext.LocalContext.Remove(ContextLabel);
227 }
228 }
229
230 }
231
232 #endregion
233
234 #region IDisposable
235
241 public void Dispose()
242 {
243 DeRef();
244 }
245
246 #endregion
247
248 }
249}
250#endif
Provides an automated way to reuse Entity Framework DbContext objects within the context of a single ...
static DbContextManager< C > GetManager(ObjectContext context)
Gets the ObjectContextManager object for the specified database.
static DbContextManager< C > GetManager(string database, DbCompiledModel model)
Gets the ObjectContextManager object for the specified database.
static DbContextManager< C > GetManager(string database, string label)
Gets the ObjectContextManager object for the specified database.
static DbContextManager< C > GetManager(ObjectContext context, string label)
Gets the ObjectContextManager object for the specified database.
int RefCount
Gets the current reference count for this object.
static DbContextManager< C > GetManager()
Gets the ObjectContextManager object for the /// specified database.
static DbContextManager< C > GetManager(string database)
Gets the ObjectContextManager object for the specified database.
static DbContextManager< C > GetManager(string database, string label, DbCompiledModel model)
Gets the ObjectContextManager object for the specified database.
void Dispose()
Dispose object, dereferencing or disposing the context it is managing.