1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
//---------------------------------------------------------------------
// <copyright file="ObjectViewQueryResultData.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.Metadata;
using System.Data.Metadata.Edm;
using System.Data.Objects.DataClasses;
using System.Diagnostics;
namespace System.Data.Objects
{
/// <summary>
/// Manages a binding list constructed from query results.
/// </summary>
/// <typeparam name="TElement">
/// Type of the elements in the binding list.
/// </typeparam>
/// <remarks>
/// The binding list is initialized from query results.
/// If the binding list can be modified,
/// objects are added or removed from the ObjectStateManager (via the ObjectContext).
/// </remarks>
internal sealed class ObjectViewQueryResultData<TElement> : IObjectViewData<TElement>
{
private List<TElement> _bindingList;
/// <summary>
/// ObjectContext used to add or delete objects when the list can be modified.
/// </summary>
private ObjectContext _objectContext;
/// <summary>
/// If the TElement type is an Entity type of some kind,
/// this field specifies the entity set to add entity objects.
/// </summary>
private EntitySet _entitySet;
private bool _canEditItems;
private bool _canModifyList;
/// <summary>
/// Construct a new instance of the ObjectViewQueryResultData class using the supplied query results.
/// </summary>
/// <param name="queryResults">
/// Result of object query execution used to populate the binding list.
/// </param>
/// <param name="objectContext">
/// ObjectContext used to add or remove items.
/// If the binding list can be modified, this parameter should not be null.
/// </param>
/// <param name="forceReadOnlyList">
/// <b>True</b> if items should not be allowed to be added or removed from the binding list.
/// Note that other conditions may prevent the binding list from being modified, so a value of <b>false</b>
/// supplied for this parameter doesn't necessarily mean that the list will be writable.
/// </param>
/// <param name="entitySet">
/// If the TElement type is an Entity type of some kind,
/// this field specifies the entity set to add entity objects.
/// </param>
internal ObjectViewQueryResultData(IEnumerable queryResults, ObjectContext objectContext, bool forceReadOnlyList, EntitySet entitySet)
{
bool canTrackItemChanges = IsEditable(typeof(TElement));
_objectContext = objectContext;
_entitySet = entitySet;
_canEditItems = canTrackItemChanges;
_canModifyList = !forceReadOnlyList && canTrackItemChanges && _objectContext != null;
_bindingList = new List<TElement>();
foreach (TElement element in queryResults)
{
_bindingList.Add(element);
}
}
/// <summary>
/// Cannot be a DbDataRecord or a derivative of DbDataRecord
/// </summary>
/// <param name="elementType"></param>
/// <returns></returns>
private bool IsEditable(Type elementType)
{
return !((elementType == typeof(DbDataRecord)) ||
((elementType != typeof(DbDataRecord)) && elementType.IsSubclassOf(typeof(DbDataRecord))));
}
/// <summary>
/// Throw an exception is an entity set was not specified for this instance.
/// </summary>
private void EnsureEntitySet()
{
if (_entitySet == null)
{
throw EntityUtil.CannotResolveTheEntitySetforGivenEntity(typeof(TElement));
}
}
#region IObjectViewData<T> Members
public IList<TElement> List
{
get { return _bindingList; }
}
public bool AllowNew
{
get { return _canModifyList && _entitySet != null; }
}
public bool AllowEdit
{
get { return _canEditItems; }
}
public bool AllowRemove
{
get { return _canModifyList; }
}
public bool FiresEventOnAdd
{
get { return false; }
}
public bool FiresEventOnRemove
{
get { return true; }
}
public bool FiresEventOnClear
{
get { return false; }
}
public void EnsureCanAddNew()
{
EnsureEntitySet();
}
public int Add(TElement item, bool isAddNew)
{
EnsureEntitySet();
Debug.Assert(_objectContext != null, "ObjectContext is null.");
// If called for AddNew operation, add item to binding list, pending addition to ObjectContext.
if (!isAddNew)
{
_objectContext.AddObject(TypeHelpers.GetFullName(_entitySet), item);
}
_bindingList.Add(item);
return _bindingList.Count - 1;
}
public void CommitItemAt(int index)
{
EnsureEntitySet();
Debug.Assert(_objectContext != null, "ObjectContext is null.");
TElement item = _bindingList[index];
_objectContext.AddObject(TypeHelpers.GetFullName(_entitySet), item);
}
public void Clear()
{
while (0 < _bindingList.Count)
{
TElement entity = _bindingList[_bindingList.Count - 1];
Remove(entity, false);
}
}
public bool Remove(TElement item, bool isCancelNew)
{
bool removed;
Debug.Assert(_objectContext != null, "ObjectContext is null.");
if (isCancelNew)
{
// Item was previously added to binding list, but not ObjectContext.
removed = _bindingList.Remove(item);
}
else
{
EntityEntry stateEntry = _objectContext.ObjectStateManager.FindEntityEntry(item);
if (stateEntry != null)
{
stateEntry.Delete();
// OnCollectionChanged event will be fired, where the binding list will be updated.
removed = true;
}
else
{
removed = false;
}
}
return removed;
}
public ListChangedEventArgs OnCollectionChanged(object sender, CollectionChangeEventArgs e, ObjectViewListener listener)
{
ListChangedEventArgs changeArgs = null;
// Since event is coming from cache and it might be shared amoung different queries
// we have to check to see if correct event is being handled.
if (e.Element.GetType().IsAssignableFrom(typeof(TElement)) &&
_bindingList.Contains((TElement)(e.Element)))
{
TElement item = (TElement)e.Element;
int itemIndex = _bindingList.IndexOf(item);
if (itemIndex >= 0) // Ignore entities that we don't know about.
{
// Only process "remove" events.
Debug.Assert(e.Action != CollectionChangeAction.Refresh, "Cache should never fire with refresh, it does not have clear");
if (e.Action == CollectionChangeAction.Remove)
{
_bindingList.Remove(item);
listener.UnregisterEntityEvents(item);
changeArgs = new ListChangedEventArgs(ListChangedType.ItemDeleted, itemIndex /* newIndex*/, -1 /* oldIndex*/);
}
}
}
return changeArgs;
}
#endregion
}
}
|