public DataSet GetDataSet() { ConstDataSetPoolEntry poolEntry = null; if (!globalPool.TryGetValue(DataSetXml, out poolEntry)) { poolEntry = new ConstDataSetPoolEntry() { OriginalDataSet = LoadDataSetFromXml(), Pool = new ConcurrentBag <ConstDataSet>() }; globalPool[DataSetXml] = poolEntry; } if (ReadOnly) { ConstDataSet constDs = null; if (!poolEntry.Pool.TryTake(out constDs)) { constDs = (ConstDataSet)poolEntry.OriginalDataSet.Copy(); newInstanceCount++; constDs.MakeReadOnlyColumns(); constDs.SubscribeOnChangeEvents(); } localInstances.Add(constDs); return(constDs); } else { // global pool is not used in this case var ds = poolEntry.OriginalDataSet.Copy(); newInstanceCount++; return(ds); } }
public void Dispose() { if (localInstances.Count > 0) { ConstDataSetPoolEntry poolEntry = null; if (globalPool.TryGetValue(DataSetXml, out poolEntry)) { foreach (var ds in localInstances) { if (!ds.IsDirty) { poolEntry.Pool.Add(ds); } } localInstances.Clear(); } } }