【发布时间】:2011-07-28 19:20:08
【问题描述】:
当在 Web 应用程序或控制台应用程序的 using 语句中使用实体上下文时,我无法确定实体上下文是否在使用流程中进行了处理。
谢谢!
using System;
using System.Web;
namespace Foo.Model
{
public partial class FooEntities : ObjectContext
{
private const string CurrentContextKey = "FooEntities.Current";
[ThreadStatic]
private static FooEntities _currentOnThreadStatic;
private FooEntities _previousContext;
/// <summary>
/// Gets the current <see cref="FooEntities"/> instance, if an instance can be shared in the current context.
/// </summary>
/// <remarks>
/// The current context is stored in the HTTP context, if it is available (otherwise it is stored in a thread-static instance).
/// Multiple contexts can be stacked.
/// </remarks>
public static FooEntities Current
{
get
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Items[CurrentContextKey] as FooEntities;
}
else
{
return _currentOnThreadStatic;
}
}
private set
{
if (HttpContext.Current != null)
{
HttpContext.Current.Items[CurrentContextKey] = value;
}
else
{
_currentOnThreadStatic = value;
}
}
}
/// <summary>
/// Returns a repository instance bound to this object context.
/// </summary>
/// <typeparam name="TRepository">The type of repository to instantiate.</typeparam>
/// <returns>The repository instance.</returns>
public TRepository GetRepository<TRepository>()
where TRepository: BaseRepository
{
return (TRepository) Activator.CreateInstance(typeof(TRepository), this);
}
/// <summary>
/// Ensures that an ambient context is available through <see cref="Current"/>, throwing an exception otherwise.
/// </summary>
/// <exception type="InvalidOperationException)">
/// Thrown if <see cref="Current"/> is null.
/// </exception>
public static void EnsureContext()
{
if (Current == null)
{
throw new InvalidOperationException("An ambient FooEntities context is expected.");
}
}
/// <summary>
/// Releases the context instance.
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
Current = _previousContext;
base.Dispose(disposing);
}
/// <summary>
/// Is called by all constructors.
/// </summary>
partial void OnContextCreated()
{
_previousContext = Current;
Current = this;
}
}
}
【问题讨论】:
-
您是否有理由尝试将 ObjectContext 存储在您的 HttpContext 中?上下文被认为是轻量级的,通常您在需要时创建一个实例,然后将其丢弃。
-
因此 FooEntities.Current 可以用于不同的类,因此您不必在构造函数中注入实例并在所有这些类中保持相同的实例。
-
我认为您应该阅读这篇文章:stackoverflow.com/questions/3653009/… 这有望促使您重新考虑该线程静态上下文。
-
@Ladislav Mrnka:我认为实现 id c*&p。我不支持它,我也没有写它,也没有想象它。我相信实体上下文应该被注入到需要访问模型的类中,并且顶级请求处理程序应该实例化它。丑丑丑丑!归根结底,我同意你的看法,希望没有人把它作为一个例子来说明该做什么,而是不做什么。
标签: c# entity-framework-4 memory-leaks idisposable disposable