![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
using
System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Collections;
6 using System.Data.Linq;
7
8 /// <summary>
9 /// 实现自动抛弃当前数据库上下文的模块。
10 /// </summary>
11 public sealed class DataContextAutoDisposeModule : IHttpModule
12 {
13 #region IHttpModule 成员
14 public void Init(HttpApplication context)
15 {
16 context.PostRequestHandlerExecute += new EventHandler(Enter);
17 }
18
19 public void Dispose()
20 {
21 }
22 #endregion
23
24 private void Enter( object sender, EventArgs e)
25 {
26 IDictionary httpContextItems = HttpContext.Current.Items;
27 List < object > keys = new List < object > (httpContextItems.Count);
28
29 foreach (DictionaryEntry item in httpContextItems)
30 {
31 DataContext dataContext = item.Value as DataContext;
32 if (dataContext != null )
33 {
34 dataContext.Dispose();
35 keys.Add(item.Key);
36 }
37 }
38
39 foreach ( object key in keys)
40 {
41 httpContextItems.Remove(key);
42 }
43 }
44 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Collections;
6 using System.Data.Linq;
7
8 /// <summary>
9 /// 实现自动抛弃当前数据库上下文的模块。
10 /// </summary>
11 public sealed class DataContextAutoDisposeModule : IHttpModule
12 {
13 #region IHttpModule 成员
14 public void Init(HttpApplication context)
15 {
16 context.PostRequestHandlerExecute += new EventHandler(Enter);
17 }
18
19 public void Dispose()
20 {
21 }
22 #endregion
23
24 private void Enter( object sender, EventArgs e)
25 {
26 IDictionary httpContextItems = HttpContext.Current.Items;
27 List < object > keys = new List < object > (httpContextItems.Count);
28
29 foreach (DictionaryEntry item in httpContextItems)
30 {
31 DataContext dataContext = item.Value as DataContext;
32 if (dataContext != null )
33 {
34 dataContext.Dispose();
35 keys.Add(item.Key);
36 }
37 }
38
39 foreach ( object key in keys)
40 {
41 httpContextItems.Remove(key);
42 }
43 }
44 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using
System.Data.Linq;
using System.Web;
public static class DataContextFactory
{
public static TDataContext GetDataContext < TDataContext > ()
where TDataContext : DataContext, new ()
{
if (HttpContext.Current != null )
{
string key = typeof (TDataContext).FullName;
TDataContext dataContext = HttpContext.Current.Items[key] as TDataContext;
if (dataContext == null )
{
dataContext = new TDataContext();
HttpContext.Current.Items[key] = dataContext;
}
return dataContext;
}
return new TDataContext();
}
}
using System.Web;
public static class DataContextFactory
{
public static TDataContext GetDataContext < TDataContext > ()
where TDataContext : DataContext, new ()
{
if (HttpContext.Current != null )
{
string key = typeof (TDataContext).FullName;
TDataContext dataContext = HttpContext.Current.Items[key] as TDataContext;
if (dataContext == null )
{
dataContext = new TDataContext();
HttpContext.Current.Items[key] = dataContext;
}
return dataContext;
}
return new TDataContext();
}
}