comparison Source/Data/Linq/CompiledTableT.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Linq.Expressions;
5
6 namespace BLToolkit.Data.Linq
7 {
8 using Mapping;
9 using Builder;
10
11 class CompiledTable<T>
12 {
13 public CompiledTable(LambdaExpression lambda, Expression expression)
14 {
15 _lambda = lambda;
16 _expression = expression;
17 }
18
19 readonly LambdaExpression _lambda;
20 readonly Expression _expression;
21 readonly object _sync = new object();
22
23 string _lastContextID;
24 MappingSchema _lastMappingSchema;
25 Query<T> _lastQuery;
26
27 readonly Dictionary<object,Query<T>> _infos = new Dictionary<object, Query<T>>();
28
29 Query<T> GetInfo(IDataContext dataContext)
30 {
31 var dataContextInfo = DataContextInfo.Create(dataContext);
32
33 string lastContextID;
34 MappingSchema lastMappingSchema;
35 Query<T> query;
36
37 lock (_sync)
38 {
39 lastContextID = _lastContextID;
40 lastMappingSchema = _lastMappingSchema;
41 query = _lastQuery;
42 }
43
44 var contextID = dataContextInfo.ContextID;
45 var mappingSchema = dataContextInfo.MappingSchema;
46
47 if (lastContextID != contextID || lastMappingSchema != mappingSchema)
48 query = null;
49
50 if (query == null)
51 {
52 var key = new { contextID, mappingSchema };
53
54 lock (_sync)
55 _infos.TryGetValue(key, out query);
56
57 if (query == null)
58 {
59 lock (_sync)
60 {
61 _infos.TryGetValue(key, out query);
62
63 if (query == null)
64 {
65 query = new ExpressionBuilder(new Query<T>(), dataContextInfo, _expression, _lambda.Parameters.ToArray())
66 .Build<T>();
67
68 _infos.Add(key, query);
69
70 _lastContextID = contextID;
71 _lastMappingSchema = mappingSchema;
72 _lastQuery = query;
73 }
74 }
75 }
76 }
77
78 return query;
79 }
80
81 public IQueryable<T> Create(object[] parameters)
82 {
83 var db = (IDataContext)parameters[0];
84 return new Table<T>(db, _expression) { Info = GetInfo(db), Parameters = parameters };
85 }
86
87 public T Execute(object[] parameters)
88 {
89 var db = (IDataContext)parameters[0];
90 var ctx = DataContextInfo.Create(db);
91 var query = GetInfo(db);
92
93 return (T)query.GetElement(null, ctx, _expression, parameters);
94 }
95 }
96 }