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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
//------------------------------------------------------------------------------
// <copyright file="DataSourceView.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI {
using System.Collections;
using System.ComponentModel;
using System.Security.Permissions;
public abstract class DataSourceView {
private static readonly object EventDataSourceViewChanged = new object();
private EventHandlerList _events;
private string _name;
protected DataSourceView(IDataSource owner, string viewName) {
if (owner == null) {
throw new ArgumentNullException("owner");
}
if (viewName == null) {
throw new ArgumentNullException("viewName");
}
_name = viewName;
DataSourceControl dataSourceControl = owner as DataSourceControl;
if (dataSourceControl != null) {
dataSourceControl.DataSourceChangedInternal += new EventHandler(OnDataSourceChangedInternal);
}
else {
owner.DataSourceChanged += new EventHandler(OnDataSourceChangedInternal);
}
}
// CanX properties indicate whether the data source allows each
// operation, and if so, whether it's appropriate to do so.
// For instance, a control may allow Deletion, but if a required Delete
// command isn't set, CanDelete should be false, because a Delete
// operation would fail.
public virtual bool CanDelete {
get {
return false;
}
}
public virtual bool CanInsert {
get {
return false;
}
}
public virtual bool CanPage {
get {
return false;
}
}
public virtual bool CanRetrieveTotalRowCount {
get {
return false;
}
}
public virtual bool CanSort {
get {
return false;
}
}
public virtual bool CanUpdate {
get {
return false;
}
}
/// <devdoc>
/// Indicates the list of event handler delegates for the view. This property is read-only.
/// </devdoc>
protected EventHandlerList Events {
get {
if (_events == null) {
_events = new EventHandlerList();
}
return _events;
}
}
public string Name {
get {
return _name;
}
}
public event EventHandler DataSourceViewChanged {
add {
Events.AddHandler(EventDataSourceViewChanged, value);
}
remove {
Events.RemoveHandler(EventDataSourceViewChanged, value);
}
}
public virtual bool CanExecute(string commandName) {
return false;
}
public virtual void Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) {
if (callback == null) {
throw new ArgumentNullException("callback");
}
int affectedRecords = 0;
bool performedCallback = false;
try {
affectedRecords = ExecuteDelete(keys, oldValues);
}
catch (Exception ex) {
performedCallback = true;
if (!callback(affectedRecords, ex)) {
throw;
}
}
finally {
if (!performedCallback) {
callback(affectedRecords, null);
}
}
}
public virtual void ExecuteCommand(string commandName, IDictionary keys, IDictionary values, DataSourceViewOperationCallback callback) {
if (callback == null) {
throw new ArgumentNullException("callback");
}
int affectedRecords = 0;
bool performedCallback = false;
try {
affectedRecords = ExecuteCommand(commandName, keys, values);
}
catch (Exception ex) {
performedCallback = true;
if (!callback(affectedRecords, ex)) {
throw;
}
}
finally {
if (!performedCallback) {
callback(affectedRecords, null);
}
}
}
protected virtual int ExecuteCommand(string commandName, IDictionary keys, IDictionary values) {
throw new NotSupportedException();
}
/// <summary>
/// Performs a delete operation on the specified list. This is only
/// supported by a DataSourceControl when CanDelete returns true.
/// </summary>
/// <param name="keys">
/// The set of name/value pairs used to filter
/// the items in the list that should be deleted.
/// </param>
/// <param name="oldValues">
/// The complete set of name/value pairs used to filter
/// the items in the list that should be deleted.
/// </param>
/// <returns>
/// The number of items that were affected by the operation.
/// </returns>
protected virtual int ExecuteDelete(IDictionary keys, IDictionary oldValues) {
throw new NotSupportedException();
}
/// <summary>
/// Performs an insert operation on the specified list. This is only
/// supported by a DataControl when CanInsert is true.
/// </summary>
/// <param name="values">
/// The set of name/value pairs to be used to initialize
/// a new item in the list.
/// </param>
/// <returns>
/// The number of items that were affected by the operation.
/// </returns>
protected virtual int ExecuteInsert(IDictionary values) {
throw new NotSupportedException();
}
/// <devdoc>
/// </devdoc>
protected internal abstract IEnumerable ExecuteSelect(DataSourceSelectArguments arguments);
/// <summary>
/// Performs an update operation on the specified list. This is only
/// supported by a DataControl when CanUpdate is true.
/// </summary>
/// <param name="keys">
/// The set of name/value pairs used to filter
/// the items in the list that should be updated.
/// </param>
/// <param name="values">
/// The set of name/value pairs to be used to update the
/// items in the list.
/// </param>
/// <param name="oldValues">
/// The set of name/value pairs to be used to identify the
/// item to be updated.
/// </param>
/// <returns>
/// The number of items that were affected by the operation.
/// </returns>
protected virtual int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) {
throw new NotSupportedException();
}
private void OnDataSourceChangedInternal(object sender, EventArgs e) {
OnDataSourceViewChanged(e);
}
protected virtual void OnDataSourceViewChanged(EventArgs e) {
EventHandler handler = Events[EventDataSourceViewChanged] as EventHandler;
if (handler != null) {
handler(this, e);
}
}
public virtual void Insert(IDictionary values, DataSourceViewOperationCallback callback) {
if (callback == null) {
throw new ArgumentNullException("callback");
}
int affectedRecords = 0;
bool performedCallback = false;
try {
affectedRecords = ExecuteInsert(values);
}
catch (Exception ex) {
performedCallback = true;
if (!callback(affectedRecords, ex)) {
throw;
}
}
finally {
if (!performedCallback) {
callback(affectedRecords, null);
}
}
}
protected internal virtual void RaiseUnsupportedCapabilityError(DataSourceCapabilities capability) {
if (!CanPage && ((capability & DataSourceCapabilities.Page) != 0)) {
throw new NotSupportedException(SR.GetString(SR.DataSourceView_NoPaging));
}
if (!CanSort && ((capability & DataSourceCapabilities.Sort) != 0)) {
throw new NotSupportedException(SR.GetString(SR.DataSourceView_NoSorting));
}
if (!CanRetrieveTotalRowCount && ((capability & DataSourceCapabilities.RetrieveTotalRowCount) != 0)) {
throw new NotSupportedException(SR.GetString(SR.DataSourceView_NoRowCount));
}
}
public virtual void Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) {
if (callback == null) {
throw new ArgumentNullException("callback");
}
callback(ExecuteSelect(arguments));
}
public virtual void Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) {
if (callback == null) {
throw new ArgumentNullException("callback");
}
int affectedRecords = 0;
bool performedCallback = false;
try {
affectedRecords = ExecuteUpdate(keys, values, oldValues);
}
catch (Exception ex) {
performedCallback = true;
if (!callback(affectedRecords, ex)) {
throw;
}
}
finally {
if (!performedCallback) {
callback(affectedRecords, null);
}
}
}
}
}
|