diff Source/Data/Linq/QueryContext.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Source/Data/Linq/QueryContext.cs	Thu Mar 27 21:46:09 2014 +0400
@@ -0,0 +1,73 @@
+using System;
+using System.Collections.Generic;
+using System.Linq.Expressions;
+
+namespace BLToolkit.Data.Linq
+{
+	public class QueryContext
+	{
+		public class DataContextContext
+		{
+			public IDataContextInfo DataContextInfo;
+			public bool             InUse;
+		}
+
+		public QueryContext(IDataContextInfo dataContext, Expression expr, object[] compiledParameters)
+		{
+			RootDataContext    = dataContext;
+			Expression         = expr;
+			CompiledParameters = compiledParameters;
+		}
+
+		public IDataContextInfo RootDataContext;
+		public Expression       Expression;
+		public object[]         CompiledParameters;
+		public int              Counter;
+
+		List<DataContextContext> _contexts;
+
+		public DataContextContext GetDataContext()
+		{
+			if (_contexts == null)
+			{
+				RootDataContext.DataContext.OnClosing += OnRootClosing;
+				_contexts = new List<DataContextContext>(1);
+			}
+
+			foreach (var context in _contexts)
+			{
+				if (!context.InUse)
+				{
+					context.InUse = true;
+					return context;
+				}
+			}
+
+			var ctx = new DataContextContext { DataContextInfo = RootDataContext.Clone(true), InUse = true };
+
+			_contexts.Add(ctx);
+
+			return ctx;
+		}
+
+		public void ReleaseDataContext(DataContextContext context)
+		{
+			context.InUse = false;
+		}
+
+		void OnRootClosing(object sender, EventArgs e)
+		{
+			foreach (var context in _contexts)
+				context.DataContextInfo.DataContext.Dispose();
+
+			RootDataContext.DataContext.OnClosing -= OnRootClosing;
+
+			_contexts = null;
+		}
+
+		public void AfterQuery()
+		{
+			Counter++;
+		}
+	}
+}