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.EFCore.Shared/DbContextManager.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DbContextManager.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//-----------------------------------------------------------------------
8using System;
9using Microsoft.EntityFrameworkCore;
10
12{
32 [Obsolete("Use dependency injection instead", false)]
33 public class DbContextManager<C> : IDisposable where C : DbContext
34 {
35 private static object _lock = new object();
36 private C _context;
37 private string _label;
38 private string _contextLabel;
39
43
45 {
46 return GetManager(string.Empty);
47 }
48
54 public static DbContextManager<C> GetManager(string database)
55 {
56 return GetManager(database, "default");
57 }
58
68 public static DbContextManager<C> GetManager(string database, string label)
69 {
70 DbContextManager<C> mgr = null;
71 var contextLabel = GetContextName(database, label);
72 lock (_lock)
73 {
74 if (ApplicationContext.LocalContext.Contains(contextLabel))
75 {
76 mgr = (DbContextManager<C>)(ApplicationContext.LocalContext[contextLabel]);
77 mgr.AddRef();
78 }
79 }
80
81 if (mgr == null)
82 {
83 mgr = new DbContextManager<C>(database, label);
84 lock (_lock)
85 {
86 Csla.
87 ApplicationContext.LocalContext[contextLabel] = mgr;
88 mgr.AddRef();
89 }
90 }
91 return mgr;
92 }
93
94 private DbContextManager(string database, string label)
95 {
96 _contextLabel = GetContextName(database, label);
97 _label = label;
98 if (string.IsNullOrEmpty(database))
99 _context = (C)(Activator.CreateInstance(typeof(C)));
100 else
101 _context = (C)(Activator.CreateInstance(typeof(C), database));
102 }
103
104 private static string GetContextName(string database, string label)
105 {
106 return "__dbctx:" + label + "-" + database;
107 }
108
109
113 public C DbContext
114 {
115 get { return _context; }
116 }
117
118 private int _refCount;
119
124 public int RefCount
125 {
126 get { return _refCount; }
127 }
128
129 private void AddRef()
130 {
131 _refCount += 1;
132 }
133
134 private void DeRef()
135 {
136 lock (_lock)
137 {
138 _refCount -= 1;
139 if (_refCount == 0)
140 {
141 _context.Dispose();
142 ApplicationContext.LocalContext.Remove(_contextLabel);
143 }
144 }
145 }
146
152 public void Dispose()
153 {
154 Dispose(true);
155 GC.SuppressFinalize(this);
156 }
157
163 protected virtual void Dispose(bool p)
164 {
165 DeRef();
166 }
167 }
168}
Provides an automated way to reuse Entity Framework DbContext objects within the context of a single ...
static DbContextManager< C > GetManager(string database, string label)
Gets the DbContextManager object for the specified database.
void Dispose()
Dispose object, dereferencing or disposing the context it is managing.
static DbContextManager< C > GetManager()
Gets the DbContextManager object for the /// specified database.
virtual void Dispose(bool p)
Dispose object, dereferencing or disposing the context it is managing.
static DbContextManager< C > GetManager(string database)
Gets the DbContextManager object for the specified database.
int RefCount
Gets the current reference count for this object.