0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Linq.Expressions;
|
|
4
|
|
5 namespace BLToolkit.Data.Linq
|
|
6 {
|
|
7 public class QueryContext
|
|
8 {
|
|
9 public class DataContextContext
|
|
10 {
|
|
11 public IDataContextInfo DataContextInfo;
|
|
12 public bool InUse;
|
|
13 }
|
|
14
|
|
15 public QueryContext(IDataContextInfo dataContext, Expression expr, object[] compiledParameters)
|
|
16 {
|
|
17 RootDataContext = dataContext;
|
|
18 Expression = expr;
|
|
19 CompiledParameters = compiledParameters;
|
|
20 }
|
|
21
|
|
22 public IDataContextInfo RootDataContext;
|
|
23 public Expression Expression;
|
|
24 public object[] CompiledParameters;
|
|
25 public int Counter;
|
|
26
|
|
27 List<DataContextContext> _contexts;
|
|
28
|
|
29 public DataContextContext GetDataContext()
|
|
30 {
|
|
31 if (_contexts == null)
|
|
32 {
|
|
33 RootDataContext.DataContext.OnClosing += OnRootClosing;
|
|
34 _contexts = new List<DataContextContext>(1);
|
|
35 }
|
|
36
|
|
37 foreach (var context in _contexts)
|
|
38 {
|
|
39 if (!context.InUse)
|
|
40 {
|
|
41 context.InUse = true;
|
|
42 return context;
|
|
43 }
|
|
44 }
|
|
45
|
|
46 var ctx = new DataContextContext { DataContextInfo = RootDataContext.Clone(true), InUse = true };
|
|
47
|
|
48 _contexts.Add(ctx);
|
|
49
|
|
50 return ctx;
|
|
51 }
|
|
52
|
|
53 public void ReleaseDataContext(DataContextContext context)
|
|
54 {
|
|
55 context.InUse = false;
|
|
56 }
|
|
57
|
|
58 void OnRootClosing(object sender, EventArgs e)
|
|
59 {
|
|
60 foreach (var context in _contexts)
|
|
61 context.DataContextInfo.DataContext.Dispose();
|
|
62
|
|
63 RootDataContext.DataContext.OnClosing -= OnRootClosing;
|
|
64
|
|
65 _contexts = null;
|
|
66 }
|
|
67
|
|
68 public void AfterQuery()
|
|
69 {
|
|
70 Counter++;
|
|
71 }
|
|
72 }
|
|
73 }
|