Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DLinq / Dlinq / DataServices.cs / 1599186 / DataServices.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Linq;
using System.Runtime.CompilerServices;
namespace System.Data.Linq {
using System.Data.Linq.Mapping;
using System.Data.Linq.Provider;
internal class CommonDataServices : IDataServices {
DataContext context;
MetaModel metaModel;
IdentityManager identifier;
ChangeTracker tracker;
ChangeDirector director;
bool hasCachedObjects;
Dictionary factoryMap;
internal CommonDataServices(DataContext context, MetaModel model) {
this.context = context;
this.metaModel = model;
bool asReadOnly = !context.ObjectTrackingEnabled;
this.identifier = IdentityManager.CreateIdentityManager(asReadOnly);
this.tracker = ChangeTracker.CreateChangeTracker(this, asReadOnly);
this.director = ChangeDirector.CreateChangeDirector(context);
this.factoryMap = new Dictionary();
}
public DataContext Context {
get { return this.context; }
}
public MetaModel Model {
get { return this.metaModel; }
}
internal void SetModel(MetaModel model) {
this.metaModel = model;
}
internal IdentityManager IdentityManager {
get { return this.identifier; }
}
internal ChangeTracker ChangeTracker {
get { return this.tracker; }
}
internal ChangeDirector ChangeDirector {
get { return this.director; }
}
internal IEnumerable GetParents(MetaType type, object item) {
return this.GetRelations(type, item, true);
}
internal IEnumerable GetChildren(MetaType type, object item) {
return this.GetRelations(type, item, false);
}
private IEnumerable GetRelations(MetaType type, object item, bool isForeignKey) {
foreach (MetaDataMember mm in type.PersistentDataMembers) {
if (mm.IsAssociation) {
MetaType otherType = mm.Association.OtherType;
if (mm.Association.IsForeignKey == isForeignKey) {
object value = null;
if (mm.IsDeferred) {
value = mm.DeferredValueAccessor.GetBoxedValue(item);
}
else {
value = mm.StorageAccessor.GetBoxedValue(item);
}
if (value != null) {
if (mm.Association.IsMany) {
IEnumerable list = (IEnumerable)value;
foreach (object otherItem in list) {
yield return new RelatedItem(otherType.GetInheritanceType(otherItem.GetType()), otherItem);
}
}
else {
yield return new RelatedItem(otherType.GetInheritanceType(value.GetType()), value);
}
}
}
}
}
}
internal void ResetServices() {
hasCachedObjects = false;
bool asReadOnly = !context.ObjectTrackingEnabled;
this.identifier = IdentityManager.CreateIdentityManager(asReadOnly);
this.tracker = ChangeTracker.CreateChangeTracker(this, asReadOnly);
this.factoryMap = new Dictionary();
}
internal static object[] GetKeyValues(MetaType type, object instance) {
List