Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / DataWebControls / System / Data / WebControls / EntityDataSourceWrapper.cs / 1 / EntityDataSourceWrapper.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.ComponentModel;
using System.Data.Common;
using System.Data.Metadata.Edm;
using System.Reflection;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Collections.ObjectModel;
using System.Text;
using System.Web.UI;
namespace System.Web.UI.WebControls
{
///
/// Wraps an entity displayed in the data source in an ICustomTypeDescriptor
/// implementation that flattens complex types and exposes references.
///
internal class EntityDataSourceWrapper : ICustomTypeDescriptor
{
private readonly EntityDataSourceWrapperCollection _collection;
private readonly ObjectStateEntry _stateEntry;
internal EntityDataSourceWrapper(EntityDataSourceWrapperCollection collection, object trackedEntity)
{
EntityDataSourceUtil.CheckArgumentNull(collection, "collection");
EntityDataSourceUtil.CheckArgumentNull(trackedEntity, "trackedEntity");
this._collection = collection;
// retrieve state entry
if (!this._collection.Context.ObjectStateManager.TryGetObjectStateEntry(trackedEntity, out _stateEntry))
{
throw new ArgumentException(Strings.ComponentNotFromProperCollection, "trackedEntity");
}
}
///
/// Gets entity wrapped by this type descriptor.
///
internal object WrappedEntity
{
get
{
return this._stateEntry.Entity;
}
}
///
/// Gets collection containing this wrapper.
///
internal EntityDataSourceWrapperCollection Collection
{
get { return this._collection; }
}
#region ICustomTypeDescriptor Implementation
System.ComponentModel.AttributeCollection System.ComponentModel.ICustomTypeDescriptor.GetAttributes() { return new System.ComponentModel.AttributeCollection(); }
string ICustomTypeDescriptor.GetClassName() { return null; }
string ICustomTypeDescriptor.GetComponentName() { return null; }
TypeConverter ICustomTypeDescriptor.GetConverter() { return null; }
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() { return null; }
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() { return null; }
object ICustomTypeDescriptor.GetEditor(Type editorBaseType) { return null; }
EventDescriptorCollection ICustomTypeDescriptor.GetEvents() { return null; }
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) { return null; }
public PropertyDescriptorCollection GetProperties()
{
return ((ITypedList)this._collection).GetItemProperties(null);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
return ((ICustomTypeDescriptor)this).GetProperties();
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return this.WrappedEntity;
}
#endregion ICustomTypeDescriptor Implementation
///
/// Use this method to set the properties on the wrapped entity
///
///
///
///
internal void SetAllProperties(Dictionary propertiesFromViewState, bool overwriteSameValue,
ref Dictionary propertySettingExceptionsCaught)
{
// We aggregate the reference descriptors rather than setting them directly
// to account for compound keys (we need all components of the key to create
// an EntityKey that can be set on the EntityReference)
var referenceList = new List>();
foreach (EntityDataSourceWrapperPropertyDescriptor descriptor in _collection.AllPropertyDescriptors)
{
// figure out which display name to match for this descriptor
string displayName = descriptor.Column.DisplayName;
// if we have a controlling column, use its display name instead
if (descriptor.Column.ControllingColumn != null)
{
displayName = descriptor.Column.ControllingColumn.DisplayName;
}
object value;
if (propertiesFromViewState.TryGetValue(displayName, out value))
{
// get all changed ReferencePropertyDescriptor from ViewState
EntityDataSourceReferenceKeyColumn referenceColumn = descriptor.Column as EntityDataSourceReferenceKeyColumn;
// convert the value as needed
object adjustedValue = EntityDataSourceUtil.ConvertType(value, descriptor.PropertyType, descriptor.DisplayName);
if (null != referenceColumn)
{
referenceList.Add(new KeyValuePair(
referenceColumn, adjustedValue));
continue;
}
if (overwriteSameValue || adjustedValue != descriptor.GetValue(this))
{
if (EntityDataSourceUtil.NullCanBeAssignedTo(descriptor.PropertyType) || null != adjustedValue)
{
try
{
descriptor.SetValue(this, adjustedValue);
}
catch (Exception e)
{
// The property descriptor uses reflection to set the property. Therefore, the inner exception contains the actual message.
Exception exceptionToThrow = e;
if (e.InnerException != null)
{
exceptionToThrow = e.InnerException;
}
if (null == propertySettingExceptionsCaught)
{
propertySettingExceptionsCaught = new Dictionary();
}
propertySettingExceptionsCaught.Add(descriptor.DisplayName, exceptionToThrow);
}
}
}
}
}
// aggregate setting for EntityKey
SetEntityKeyProperties(referenceList, overwriteSameValue);
}
private void SetEntityKeyProperties(
List> referenceList, bool overwriteSameValue)
{
EntityDataSourceUtil.CheckArgumentNull(referenceList, "referenceList");
var groups = referenceList.GroupBy(r => r.Key.Group);
foreach (var group in groups)
{
Dictionary partialKeys = new Dictionary();
foreach (KeyValuePair reference in group)
{
// convert the value as needed
EntityDataSourceReferenceKeyColumn column = reference.Key;
object keyValue = reference.Value;
if (null == keyValue)
{
partialKeys = null;
break;
}
partialKeys.Add(column.KeyMember.Name, keyValue);
}
// we only set the entitykey for once, although there might be more than one
// properties descriptor associated with the same entitykey
group.Key.SetKeyValues(this, partialKeys);
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.ComponentModel;
using System.Data.Common;
using System.Data.Metadata.Edm;
using System.Reflection;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Collections.ObjectModel;
using System.Text;
using System.Web.UI;
namespace System.Web.UI.WebControls
{
///
/// Wraps an entity displayed in the data source in an ICustomTypeDescriptor
/// implementation that flattens complex types and exposes references.
///
internal class EntityDataSourceWrapper : ICustomTypeDescriptor
{
private readonly EntityDataSourceWrapperCollection _collection;
private readonly ObjectStateEntry _stateEntry;
internal EntityDataSourceWrapper(EntityDataSourceWrapperCollection collection, object trackedEntity)
{
EntityDataSourceUtil.CheckArgumentNull(collection, "collection");
EntityDataSourceUtil.CheckArgumentNull(trackedEntity, "trackedEntity");
this._collection = collection;
// retrieve state entry
if (!this._collection.Context.ObjectStateManager.TryGetObjectStateEntry(trackedEntity, out _stateEntry))
{
throw new ArgumentException(Strings.ComponentNotFromProperCollection, "trackedEntity");
}
}
///
/// Gets entity wrapped by this type descriptor.
///
internal object WrappedEntity
{
get
{
return this._stateEntry.Entity;
}
}
///
/// Gets collection containing this wrapper.
///
internal EntityDataSourceWrapperCollection Collection
{
get { return this._collection; }
}
#region ICustomTypeDescriptor Implementation
System.ComponentModel.AttributeCollection System.ComponentModel.ICustomTypeDescriptor.GetAttributes() { return new System.ComponentModel.AttributeCollection(); }
string ICustomTypeDescriptor.GetClassName() { return null; }
string ICustomTypeDescriptor.GetComponentName() { return null; }
TypeConverter ICustomTypeDescriptor.GetConverter() { return null; }
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() { return null; }
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() { return null; }
object ICustomTypeDescriptor.GetEditor(Type editorBaseType) { return null; }
EventDescriptorCollection ICustomTypeDescriptor.GetEvents() { return null; }
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) { return null; }
public PropertyDescriptorCollection GetProperties()
{
return ((ITypedList)this._collection).GetItemProperties(null);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
return ((ICustomTypeDescriptor)this).GetProperties();
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return this.WrappedEntity;
}
#endregion ICustomTypeDescriptor Implementation
///
/// Use this method to set the properties on the wrapped entity
///
///
///
///
internal void SetAllProperties(Dictionary propertiesFromViewState, bool overwriteSameValue,
ref Dictionary propertySettingExceptionsCaught)
{
// We aggregate the reference descriptors rather than setting them directly
// to account for compound keys (we need all components of the key to create
// an EntityKey that can be set on the EntityReference)
var referenceList = new List>();
foreach (EntityDataSourceWrapperPropertyDescriptor descriptor in _collection.AllPropertyDescriptors)
{
// figure out which display name to match for this descriptor
string displayName = descriptor.Column.DisplayName;
// if we have a controlling column, use its display name instead
if (descriptor.Column.ControllingColumn != null)
{
displayName = descriptor.Column.ControllingColumn.DisplayName;
}
object value;
if (propertiesFromViewState.TryGetValue(displayName, out value))
{
// get all changed ReferencePropertyDescriptor from ViewState
EntityDataSourceReferenceKeyColumn referenceColumn = descriptor.Column as EntityDataSourceReferenceKeyColumn;
// convert the value as needed
object adjustedValue = EntityDataSourceUtil.ConvertType(value, descriptor.PropertyType, descriptor.DisplayName);
if (null != referenceColumn)
{
referenceList.Add(new KeyValuePair(
referenceColumn, adjustedValue));
continue;
}
if (overwriteSameValue || adjustedValue != descriptor.GetValue(this))
{
if (EntityDataSourceUtil.NullCanBeAssignedTo(descriptor.PropertyType) || null != adjustedValue)
{
try
{
descriptor.SetValue(this, adjustedValue);
}
catch (Exception e)
{
// The property descriptor uses reflection to set the property. Therefore, the inner exception contains the actual message.
Exception exceptionToThrow = e;
if (e.InnerException != null)
{
exceptionToThrow = e.InnerException;
}
if (null == propertySettingExceptionsCaught)
{
propertySettingExceptionsCaught = new Dictionary();
}
propertySettingExceptionsCaught.Add(descriptor.DisplayName, exceptionToThrow);
}
}
}
}
}
// aggregate setting for EntityKey
SetEntityKeyProperties(referenceList, overwriteSameValue);
}
private void SetEntityKeyProperties(
List> referenceList, bool overwriteSameValue)
{
EntityDataSourceUtil.CheckArgumentNull(referenceList, "referenceList");
var groups = referenceList.GroupBy(r => r.Key.Group);
foreach (var group in groups)
{
Dictionary partialKeys = new Dictionary();
foreach (KeyValuePair reference in group)
{
// convert the value as needed
EntityDataSourceReferenceKeyColumn column = reference.Key;
object keyValue = reference.Value;
if (null == keyValue)
{
partialKeys = null;
break;
}
partialKeys.Add(column.KeyMember.Name, keyValue);
}
// we only set the entitykey for once, although there might be more than one
// properties descriptor associated with the same entitykey
group.Key.SetKeyValues(this, partialKeys);
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AssemblyHash.cs
- PauseStoryboard.cs
- MouseDevice.cs
- PointAnimationClockResource.cs
- InstanceContextManager.cs
- SystemTcpConnection.cs
- GeneralTransform3DTo2D.cs
- TextEndOfParagraph.cs
- XmlConvert.cs
- BulletChrome.cs
- WorkflowQueue.cs
- MessagePartProtectionMode.cs
- ExpressionLink.cs
- DataGridClipboardCellContent.cs
- ManagedFilter.cs
- BaseTemplateParser.cs
- EnumerableCollectionView.cs
- XhtmlBasicCommandAdapter.cs
- FileDialogCustomPlace.cs
- XmlException.cs
- AnonymousIdentificationSection.cs
- StorageComplexPropertyMapping.cs
- NavigateEvent.cs
- Stack.cs
- AttributeCollection.cs
- UriWriter.cs
- XmlSchemaObject.cs
- hebrewshape.cs
- FontSourceCollection.cs
- SafeRightsManagementHandle.cs
- FontUnitConverter.cs
- EntityDataSourceState.cs
- EditorPartChrome.cs
- FormParameter.cs
- ExpressionDumper.cs
- StyleBamlRecordReader.cs
- ETagAttribute.cs
- WebPageTraceListener.cs
- MarkupExtensionSerializer.cs
- LayoutExceptionEventArgs.cs
- BaseCollection.cs
- XmlReflectionImporter.cs
- ObjectSet.cs
- sortedlist.cs
- CodeTypeDelegate.cs
- StringUtil.cs
- WindowsBrush.cs
- SystemIcmpV6Statistics.cs
- ValidatingReaderNodeData.cs
- ClientCultureInfo.cs
- XmlILStorageConverter.cs
- CompilerState.cs
- TableRow.cs
- XPathConvert.cs
- PackageRelationshipSelector.cs
- GeneralTransformCollection.cs
- MutexSecurity.cs
- isolationinterop.cs
- WebPartConnectionsCancelVerb.cs
- ControlFilterExpression.cs
- NonBatchDirectoryCompiler.cs
- ConnectionOrientedTransportManager.cs
- XmlAttributeCollection.cs
- OleDbTransaction.cs
- OrderedDictionary.cs
- InputBuffer.cs
- LicenseManager.cs
- ResourceWriter.cs
- SafePEFileHandle.cs
- TogglePatternIdentifiers.cs
- ResourceDisplayNameAttribute.cs
- ScalarConstant.cs
- HijriCalendar.cs
- ValueQuery.cs
- MemberMemberBinding.cs
- StatusBarDrawItemEvent.cs
- DirectionalLight.cs
- StdRegProviderWrapper.cs
- TraceContextRecord.cs
- errorpatternmatcher.cs
- DataSetMappper.cs
- ToolboxItemWrapper.cs
- FixedHyperLink.cs
- LocalsItemDescription.cs
- AsyncCompletedEventArgs.cs
- InternalTypeHelper.cs
- HttpPostedFile.cs
- DataGridViewCellValueEventArgs.cs
- Codec.cs
- MenuItemBinding.cs
- WindowsAuthenticationEventArgs.cs
- FontWeight.cs
- DetailsViewInsertedEventArgs.cs
- Vector3dCollection.cs
- SqlBuilder.cs
- UInt16Storage.cs
- ToolStripGripRenderEventArgs.cs
- GuidConverter.cs
- SharedConnectionListener.cs
- XmlSchemaChoice.cs