Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DLinq / Dlinq / ChangeDirector.cs / 1305376 / ChangeDirector.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Text;
using System.Reflection;
using System.Linq;
using System.Security.Permissions;
using System.Security;
namespace System.Data.Linq {
using System.Data.Linq.Mapping;
using System.Data.Linq.Provider;
using System.Diagnostics.CodeAnalysis;
///
/// Controls how inserts, updates and deletes are performed.
///
internal abstract class ChangeDirector {
internal abstract int Insert(TrackedObject item);
internal abstract int DynamicInsert(TrackedObject item);
internal abstract void AppendInsertText(TrackedObject item, StringBuilder appendTo);
internal abstract int Update(TrackedObject item);
internal abstract int DynamicUpdate(TrackedObject item);
internal abstract void AppendUpdateText(TrackedObject item, StringBuilder appendTo);
internal abstract int Delete(TrackedObject item);
internal abstract int DynamicDelete(TrackedObject item);
internal abstract void AppendDeleteText(TrackedObject item, StringBuilder appendTo);
internal abstract void RollbackAutoSync();
internal abstract void ClearAutoSyncRollback();
internal static ChangeDirector CreateChangeDirector(DataContext context) {
return new StandardChangeDirector(context);
}
///
/// Implementation of ChangeDirector which calls user code if possible
/// and othewise falls back to creating SQL for 'INSERT', 'UPDATE' and 'DELETE'.
///
internal class StandardChangeDirector : ChangeDirector {
private enum UpdateType { Insert, Update, Delete };
private enum AutoSyncBehavior { ApplyNewAutoSync, RollbackSavedValues }
DataContext context;
[SuppressMessage("Microsoft.MSInternal", "CA908:AvoidTypesThatRequireJitCompilationInPrecompiledAssemblies", Justification="[....]: FxCop bug Dev10:423110 -- List> is not supposed to be flagged as a violation.")]
List> syncRollbackItems;
internal StandardChangeDirector(DataContext context) {
this.context = context;
}
[SuppressMessage("Microsoft.MSInternal", "CA908:AvoidTypesThatRequireJitCompilationInPrecompiledAssemblies", Justification="[....]: FxCop bug Dev10:423110 -- List> is not supposed to be flagged as a violation.")]
private List> SyncRollbackItems {
get {
if (syncRollbackItems == null) {
syncRollbackItems = new List>();
}
return syncRollbackItems;
}
}
internal override int Insert(TrackedObject item) {
if (item.Type.Table.InsertMethod != null) {
try {
item.Type.Table.InsertMethod.Invoke(this.context, new object[] { item.Current });
}
catch (TargetInvocationException tie) {
if (tie.InnerException != null) {
throw tie.InnerException;
}
throw;
}
return 1;
}
else {
return DynamicInsert(item);
}
}
internal override int DynamicInsert(TrackedObject item) {
Expression cmd = this.GetInsertCommand(item);
if (cmd.Type == typeof(int)) {
return (int)this.context.Provider.Execute(cmd).ReturnValue;
}
else {
IEnumerable