Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Objects / Internal / PocoPropertyAccessorStrategy.cs / 1305376 / PocoPropertyAccessorStrategy.cs
using System; using System.Collections.Generic; using System.Text; using System.Data.Objects.DataClasses; using System.Diagnostics; using System.Reflection; using System.Data.Metadata.Edm; using System.Linq.Expressions; using System.Data.Common.Utils; using System.Data.Mapping; namespace System.Data.Objects.Internal { ////// Implementation of the property accessor strategy that gets and sets values on POCO entities. That is, /// entities that do not implement IEntityWithRelationships. /// internal sealed class PocoPropertyAccessorStrategy : IPropertyAccessorStrategy { private static readonly MethodInfo s_AddToCollectionGeneric = typeof(PocoPropertyAccessorStrategy).GetMethod("AddToCollection", BindingFlags.NonPublic | BindingFlags.Static); private static readonly MethodInfo s_RemoveFromCollectionGeneric = typeof(PocoPropertyAccessorStrategy).GetMethod("RemoveFromCollection", BindingFlags.NonPublic | BindingFlags.Static); private object _entity; ////// Constructs a strategy object to work with the given entity. /// /// The entity to use public PocoPropertyAccessorStrategy(object entity) { _entity = entity; } #region Navigation Property Accessors #region GetNavigationPropertyValue // See IPropertyAccessorStrategy public object GetNavigationPropertyValue(RelatedEnd relatedEnd) { object navPropValue = null; if (relatedEnd != null) { if (relatedEnd.TargetAccessor.ValueGetter == null) { Type type = GetDeclaringType(relatedEnd); PropertyInfo propertyInfo = EntityUtil.GetTopProperty(ref type, relatedEnd.TargetAccessor.PropertyName); if (propertyInfo == null) { throw new EntityException(System.Data.Entity.Strings.PocoEntityWrapper_UnableToSetFieldOrProperty(relatedEnd.TargetAccessor.PropertyName, type.FullName)); } EntityProxyFactory factory = new EntityProxyFactory(); relatedEnd.TargetAccessor.ValueGetter = factory.CreateBaseGetter(type, propertyInfo); } try { navPropValue = relatedEnd.TargetAccessor.ValueGetter(_entity); } catch (Exception ex) { throw new EntityException(System.Data.Entity.Strings.PocoEntityWrapper_UnableToSetFieldOrProperty(relatedEnd.TargetAccessor.PropertyName, _entity.GetType().FullName), ex); } } return navPropValue; } #endregion #region SetNavigationPropertyValue // See IPropertyAccessorStrategy public void SetNavigationPropertyValue(RelatedEnd relatedEnd, object value) { if (relatedEnd != null) { if (relatedEnd.TargetAccessor.ValueSetter == null) { Type type = GetDeclaringType(relatedEnd); PropertyInfo propertyInfo = EntityUtil.GetTopProperty(ref type, relatedEnd.TargetAccessor.PropertyName); if (propertyInfo == null) { throw new EntityException(System.Data.Entity.Strings.PocoEntityWrapper_UnableToSetFieldOrProperty(relatedEnd.TargetAccessor.PropertyName, type.FullName)); } EntityProxyFactory factory = new EntityProxyFactory(); relatedEnd.TargetAccessor.ValueSetter = factory.CreateBaseSetter(type, propertyInfo); } try { relatedEnd.TargetAccessor.ValueSetter(_entity, value); } catch (Exception ex) { throw new EntityException(System.Data.Entity.Strings.PocoEntityWrapper_UnableToSetFieldOrProperty(relatedEnd.TargetAccessor.PropertyName, _entity.GetType().FullName), ex); } } } private static Type GetDeclaringType(RelatedEnd relatedEnd) { if (relatedEnd.NavigationProperty != null) { EntityType declaringEntityType = (EntityType)relatedEnd.NavigationProperty.DeclaringType; ObjectTypeMapping mapping = System.Data.Common.Internal.Materialization.Util.GetObjectMapping(declaringEntityType, relatedEnd.WrappedOwner.Context.MetadataWorkspace); return mapping.ClrType.ClrType; } else { return relatedEnd.WrappedOwner.IdentityType; } } private static Type GetNavigationPropertyType(Type entityType, string propertyName) { Type navPropType; PropertyInfo property = EntityUtil.GetTopProperty(entityType, propertyName); if (property != null) { navPropType = property.PropertyType; } else { FieldInfo field = entityType.GetField(propertyName); if (field != null) { navPropType = field.FieldType; } else { throw new EntityException(System.Data.Entity.Strings.PocoEntityWrapper_UnableToSetFieldOrProperty(propertyName, entityType.FullName)); } } return navPropType; } #endregion #endregion #region Collection Navigation Property Accessors #region CollectionAdd // See IPropertyAccessorStrategy public void CollectionAdd(RelatedEnd relatedEnd, object value) { object entity = _entity; try { object collection = GetNavigationPropertyValue(relatedEnd); if (collection == null) { collection = CollectionCreate(relatedEnd); SetNavigationPropertyValue(relatedEnd, collection); } Debug.Assert(collection != null, "Collection is null"); // do not call Add if the collection is a RelatedEnd instance if (collection == relatedEnd) { return; } if (relatedEnd.TargetAccessor.CollectionAdd == null) { relatedEnd.TargetAccessor.CollectionAdd = CreateCollectionAddFunction(entity.GetType(), relatedEnd.TargetAccessor.PropertyName); } relatedEnd.TargetAccessor.CollectionAdd(collection, value); } catch (Exception ex) { throw new EntityException(System.Data.Entity.Strings.PocoEntityWrapper_UnableToSetFieldOrProperty(relatedEnd.TargetAccessor.PropertyName, entity.GetType().FullName), ex); } } // Helper method to create delegate with property setter private static Action
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- FlowDocumentPageViewerAutomationPeer.cs
- AutoResizedEvent.cs
- ToolStripDropDownButton.cs
- Wildcard.cs
- securitycriticaldataClass.cs
- SettingsSection.cs
- CompilerParameters.cs
- entityreference_tresulttype.cs
- BackStopAuthenticationModule.cs
- DefaultAutoFieldGenerator.cs
- DigitShape.cs
- ServicesUtilities.cs
- InternalCache.cs
- SectionVisual.cs
- Utility.cs
- BoolExpression.cs
- FixedSOMImage.cs
- URLMembershipCondition.cs
- FindCriteria.cs
- SqlReferenceCollection.cs
- ThousandthOfEmRealPoints.cs
- ImageMapEventArgs.cs
- AppSecurityManager.cs
- StringConverter.cs
- TargetControlTypeAttribute.cs
- SrgsDocument.cs
- Transform.cs
- BaseComponentEditor.cs
- TypeDescriptor.cs
- SettingsPropertyCollection.cs
- odbcmetadatafactory.cs
- StylusPointProperties.cs
- SoapBinding.cs
- SmuggledIUnknown.cs
- ScriptingScriptResourceHandlerSection.cs
- DesignSurface.cs
- ChangePassword.cs
- ContainerFilterService.cs
- MatrixConverter.cs
- AvTraceFormat.cs
- OleServicesContext.cs
- DocumentOrderQuery.cs
- CellPartitioner.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- FixedHighlight.cs
- ReachFixedDocumentSerializer.cs
- Message.cs
- SecurityHeader.cs
- XPathAncestorIterator.cs
- _DisconnectOverlappedAsyncResult.cs
- ClonableStack.cs
- AtomContentProperty.cs
- DataSvcMapFile.cs
- objectresult_tresulttype.cs
- MeasureData.cs
- FontWeights.cs
- MDIControlStrip.cs
- OutputChannelBinder.cs
- SqlLiftWhereClauses.cs
- SapiGrammar.cs
- VScrollBar.cs
- GridViewCommandEventArgs.cs
- SingleBodyParameterMessageFormatter.cs
- HttpApplicationFactory.cs
- XsdDuration.cs
- SqlDataSourceCommandEventArgs.cs
- CodeArgumentReferenceExpression.cs
- IItemProperties.cs
- SamlSubjectStatement.cs
- ServiceOperation.cs
- XmlSchemaComplexType.cs
- ColorContextHelper.cs
- IISMapPath.cs
- safex509handles.cs
- PropertyGridView.cs
- storepermission.cs
- ActivityInstance.cs
- PropertyDescriptorGridEntry.cs
- FindResponse.cs
- ProfilePropertySettingsCollection.cs
- IsolatedStorageFilePermission.cs
- DataBinding.cs
- ProcessHostMapPath.cs
- BitmapFrameDecode.cs
- HttpCapabilitiesSectionHandler.cs
- NumericUpDown.cs
- FramingFormat.cs
- PersonalizationDictionary.cs
- TypedTableBaseExtensions.cs
- NumericUpDown.cs
- ApplicationFileCodeDomTreeGenerator.cs
- BuildProvider.cs
- DisplayNameAttribute.cs
- FamilyCollection.cs
- XsdDataContractImporter.cs
- ModelTreeEnumerator.cs
- NotifyInputEventArgs.cs
- TextViewBase.cs
- WorkflowInstanceQuery.cs
- StyleBamlTreeBuilder.cs