Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Objects / ObjectSet.cs / 1305376 / ObjectSet.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] // // ObjectSetis a wrapper around ObjectQuery and CUD // methods on the ObjectContext. It provides a single source that can // be used to build queries over an EntitySet as well as perform CUD operations. // // devnote: This methods on this class are expected to behave exactly // like the ObjectContext methods to which they delegate. Therefore, // they should do very little, if any, extra work aside from a // single call to delegate to the appropriate ObjectContext method. // The only exceptions to this are cases where we need to do // additional validation that is specific to ObjectSet, such as // verifying that an object is in the appropriate EntitySet during // DeleteObject and Detach operations. We also should not override // or otherwise change ObjectQuery behavior. Users should be able to // use an ObjectSet in exactly the same ways they can use an // ObjectQuery , except for the fact that the CommandText is // determined by the EntitySet name, and cannot be controlled by the user. //--------------------------------------------------------------------- using System.Collections; using System.Collections.Generic; using System.Data.Metadata.Edm; using System.Globalization; using System.Diagnostics; namespace System.Data.Objects { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")] public class ObjectSet : ObjectQuery , IObjectSet where TEntity : class { private readonly EntitySet _entitySet; #region Internal Constructors /// /// Creates a new ObjectSet that has a base ObjectQuery with the CommandText that represents /// all of the entities in the specified EntitySet. /// Sets the query's command text to the fully-qualified, quoted, EntitySet name, i.e. [EntityContainerName].[EntitySetName] /// Explicitly set MergeOption to AppendOnly in order to mirror CreateQuery behavior /// /// Metadata EntitySet on which to base the ObjectSet. /// ObjectContext to be used for the query and data modification operations. internal ObjectSet(EntitySet entitySet, ObjectContext context) : base( String.Format( CultureInfo.InvariantCulture, "{0}.{1}", EntityUtil.QuoteIdentifier(entitySet.EntityContainer.Name), EntityUtil.QuoteIdentifier(entitySet.Name)), context, MergeOption.AppendOnly) { Debug.Assert(entitySet != null, "ObjectSet constructor requires a non-null EntitySet"); Debug.Assert(context != null, "ObjectSet constructor requires a non-null ObjectContext"); _entitySet = entitySet; } #endregion #region Public Properties ////// Provides metadata for the EntitySet that is represented by the ObjectSet /// public EntitySet EntitySet { get { return _entitySet; } } #endregion #region Public Methods ////// Adds an object to the ObjectContext using the EntitySet referenced by this ObjectSet. /// See ObjectContext.AddObject for more details. /// /// Entity to be added public void AddObject(TEntity entity) { // this method is expected to behave exactly like ObjectContext.AddObject -- see devnote at the top of this class this.Context.AddObject(FullyQualifiedEntitySetName, entity); } ////// Attaches an object to the ObjectContext using the EntitySet referenced by this ObjectSet. /// See ObjectContext.AttachTo for more details. /// /// Entity to be attached public void Attach(TEntity entity) { // this method is expected to behave exactly like ObjectContext.AttachTo -- see devnote at the top of this class this.Context.AttachTo(FullyQualifiedEntitySetName, entity); } ////// Deletes an object from the ObjectContext. Validates that the object is in the referenced EntitySet in the context. /// See ObjectContext.DeleteObject for more details. /// /// Entity to be deleted. ///Throws if the specified object is not in the EntitySet. public void DeleteObject(TEntity entity) { // this method is expected to behave exactly like ObjectContext.DeleteObject -- see devnote at the top of this class // Note that in this case we use an internal DeleteObject overload so we can have the context validate // the EntitySet after it verifies that the specified object is in the context at all. this.Context.DeleteObject(entity, EntitySet); } ////// Detaches an object from the ObjectContext. Validates that the object is in the referenced EntitySet in the context. /// See ObjectContext.Detach for more details. /// /// Entity to be detached. ///Throws if the specified object is not in the EntitySet. public void Detach(TEntity entity) { // this method is expected to behave exactly like ObjectContext.Detach -- see devnote at the top of this class // Note that in this case we use an internal Detach overload so we can have the context validate // the EntitySet after it verifies that the specified object is in the context at all. this.Context.Detach(entity, EntitySet); } ////// Applies changes from one object to another with the same key in the ObjectContext. /// See ObjectContext.ApplyCurrentValues for more details. /// /// Entity that contains changes to be applied. public TEntity ApplyCurrentValues(TEntity currentEntity) { // this method is expected to behave exactly like ObjectContext.ApplyCurrentValues -- see devnote at the top of this class return this.Context.ApplyCurrentValues(FullyQualifiedEntitySetName, currentEntity); } /// /// Apply modified properties to the original object with the same key in the ObjectContext. /// See ObjectContext.ApplyOriginalValues for more details. /// /// Entity that contains values to be applied. public TEntity ApplyOriginalValues(TEntity originalEntity) { // this method is expected to behave exactly like ObjectContext.ApplyOriginalValues -- see devnote at the top of this class return this.Context.ApplyOriginalValues(FullyQualifiedEntitySetName, originalEntity); } /// /// Create an instance of the type ///. /// /// An instance of an object of type public TEntity CreateObject() { return this.Context.CreateObject. /// The object will either be an instance of the exact type , /// or possibly an instance of the proxy type that corresponds to . /// (); } /// /// Create an instance of the type ///. /// /// An instance of an object of type public T CreateObject. /// The object will either be an instance of the exact type , /// or possibly an instance of the proxy type that corresponds to . /// () where T : class, TEntity { return this.Context.CreateObject (); } #endregion #region Private Properties // Used private string FullyQualifiedEntitySetName { get { // Fully-qualified name is used to ensure the ObjectContext can always resolve the EntitySet name // The identifiers used here should not be escaped with brackets ("[]") because the ObjectContext does not allow escaping for the EntitySet name return string.Format(CultureInfo.InvariantCulture, "{0}.{1}", _entitySet.EntityContainer.Name, _entitySet.Name); } } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] // // ObjectSetis a wrapper around ObjectQuery and CUD // methods on the ObjectContext. It provides a single source that can // be used to build queries over an EntitySet as well as perform CUD operations. // // devnote: This methods on this class are expected to behave exactly // like the ObjectContext methods to which they delegate. Therefore, // they should do very little, if any, extra work aside from a // single call to delegate to the appropriate ObjectContext method. // The only exceptions to this are cases where we need to do // additional validation that is specific to ObjectSet, such as // verifying that an object is in the appropriate EntitySet during // DeleteObject and Detach operations. We also should not override // or otherwise change ObjectQuery behavior. Users should be able to // use an ObjectSet in exactly the same ways they can use an // ObjectQuery , except for the fact that the CommandText is // determined by the EntitySet name, and cannot be controlled by the user. //--------------------------------------------------------------------- using System.Collections; using System.Collections.Generic; using System.Data.Metadata.Edm; using System.Globalization; using System.Diagnostics; namespace System.Data.Objects { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")] public class ObjectSet : ObjectQuery , IObjectSet where TEntity : class { private readonly EntitySet _entitySet; #region Internal Constructors /// /// Creates a new ObjectSet that has a base ObjectQuery with the CommandText that represents /// all of the entities in the specified EntitySet. /// Sets the query's command text to the fully-qualified, quoted, EntitySet name, i.e. [EntityContainerName].[EntitySetName] /// Explicitly set MergeOption to AppendOnly in order to mirror CreateQuery behavior /// /// Metadata EntitySet on which to base the ObjectSet. /// ObjectContext to be used for the query and data modification operations. internal ObjectSet(EntitySet entitySet, ObjectContext context) : base( String.Format( CultureInfo.InvariantCulture, "{0}.{1}", EntityUtil.QuoteIdentifier(entitySet.EntityContainer.Name), EntityUtil.QuoteIdentifier(entitySet.Name)), context, MergeOption.AppendOnly) { Debug.Assert(entitySet != null, "ObjectSet constructor requires a non-null EntitySet"); Debug.Assert(context != null, "ObjectSet constructor requires a non-null ObjectContext"); _entitySet = entitySet; } #endregion #region Public Properties ////// Provides metadata for the EntitySet that is represented by the ObjectSet /// public EntitySet EntitySet { get { return _entitySet; } } #endregion #region Public Methods ////// Adds an object to the ObjectContext using the EntitySet referenced by this ObjectSet. /// See ObjectContext.AddObject for more details. /// /// Entity to be added public void AddObject(TEntity entity) { // this method is expected to behave exactly like ObjectContext.AddObject -- see devnote at the top of this class this.Context.AddObject(FullyQualifiedEntitySetName, entity); } ////// Attaches an object to the ObjectContext using the EntitySet referenced by this ObjectSet. /// See ObjectContext.AttachTo for more details. /// /// Entity to be attached public void Attach(TEntity entity) { // this method is expected to behave exactly like ObjectContext.AttachTo -- see devnote at the top of this class this.Context.AttachTo(FullyQualifiedEntitySetName, entity); } ////// Deletes an object from the ObjectContext. Validates that the object is in the referenced EntitySet in the context. /// See ObjectContext.DeleteObject for more details. /// /// Entity to be deleted. ///Throws if the specified object is not in the EntitySet. public void DeleteObject(TEntity entity) { // this method is expected to behave exactly like ObjectContext.DeleteObject -- see devnote at the top of this class // Note that in this case we use an internal DeleteObject overload so we can have the context validate // the EntitySet after it verifies that the specified object is in the context at all. this.Context.DeleteObject(entity, EntitySet); } ////// Detaches an object from the ObjectContext. Validates that the object is in the referenced EntitySet in the context. /// See ObjectContext.Detach for more details. /// /// Entity to be detached. ///Throws if the specified object is not in the EntitySet. public void Detach(TEntity entity) { // this method is expected to behave exactly like ObjectContext.Detach -- see devnote at the top of this class // Note that in this case we use an internal Detach overload so we can have the context validate // the EntitySet after it verifies that the specified object is in the context at all. this.Context.Detach(entity, EntitySet); } ////// Applies changes from one object to another with the same key in the ObjectContext. /// See ObjectContext.ApplyCurrentValues for more details. /// /// Entity that contains changes to be applied. public TEntity ApplyCurrentValues(TEntity currentEntity) { // this method is expected to behave exactly like ObjectContext.ApplyCurrentValues -- see devnote at the top of this class return this.Context.ApplyCurrentValues(FullyQualifiedEntitySetName, currentEntity); } /// /// Apply modified properties to the original object with the same key in the ObjectContext. /// See ObjectContext.ApplyOriginalValues for more details. /// /// Entity that contains values to be applied. public TEntity ApplyOriginalValues(TEntity originalEntity) { // this method is expected to behave exactly like ObjectContext.ApplyOriginalValues -- see devnote at the top of this class return this.Context.ApplyOriginalValues(FullyQualifiedEntitySetName, originalEntity); } /// /// Create an instance of the type ///. /// /// An instance of an object of type public TEntity CreateObject() { return this.Context.CreateObject. /// The object will either be an instance of the exact type , /// or possibly an instance of the proxy type that corresponds to . /// (); } /// /// Create an instance of the type ///. /// /// An instance of an object of type public T CreateObject. /// The object will either be an instance of the exact type , /// or possibly an instance of the proxy type that corresponds to . /// () where T : class, TEntity { return this.Context.CreateObject (); } #endregion #region Private Properties // Used private string FullyQualifiedEntitySetName { get { // Fully-qualified name is used to ensure the ObjectContext can always resolve the EntitySet name // The identifiers used here should not be escaped with brackets ("[]") because the ObjectContext does not allow escaping for the EntitySet name return string.Format(CultureInfo.InvariantCulture, "{0}.{1}", _entitySet.EntityContainer.Name, _entitySet.Name); } } #endregion } } // 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
- DotAtomReader.cs
- WebPartTransformerAttribute.cs
- ManipulationPivot.cs
- PageAdapter.cs
- OleDbDataAdapter.cs
- FontCollection.cs
- Paragraph.cs
- MediaScriptCommandRoutedEventArgs.cs
- SqlCachedBuffer.cs
- ObjectDataProvider.cs
- XmlSchemaValidator.cs
- Hyperlink.cs
- OrderedDictionaryStateHelper.cs
- Soap12ServerProtocol.cs
- QueryAccessibilityHelpEvent.cs
- ReflectionTypeLoadException.cs
- ZipFileInfo.cs
- InfoCardRSACryptoProvider.cs
- CodeChecksumPragma.cs
- Expr.cs
- FormViewDeleteEventArgs.cs
- HttpProfileBase.cs
- IIS7UserPrincipal.cs
- CacheSection.cs
- ProxyElement.cs
- XmlSchemaChoice.cs
- ContractCodeDomInfo.cs
- DataGridViewColumnTypePicker.cs
- UnsafeNativeMethods.cs
- FileDialog.cs
- DataTableExtensions.cs
- ZoomPercentageConverter.cs
- ActivityExecutorDelegateInfo.cs
- Compiler.cs
- ItemsPresenter.cs
- XmlParserContext.cs
- BitmapEffect.cs
- OleDbException.cs
- AutoResetEvent.cs
- DrawingImage.cs
- SafeEventLogReadHandle.cs
- IxmlLineInfo.cs
- XmlNamespaceMapping.cs
- WebBrowserPermission.cs
- AnonymousIdentificationModule.cs
- AuthStoreRoleProvider.cs
- DesignBinding.cs
- BamlLocalizerErrorNotifyEventArgs.cs
- DbConnectionStringCommon.cs
- DateTime.cs
- SelectedGridItemChangedEvent.cs
- BrushConverter.cs
- HttpModulesSection.cs
- BindingExpressionBase.cs
- SignatureResourcePool.cs
- GeneralTransform3DGroup.cs
- XmlSchemaAny.cs
- PolyLineSegmentFigureLogic.cs
- EventProxy.cs
- ViewBase.cs
- TabletDeviceInfo.cs
- XPathEmptyIterator.cs
- XmlSerializerAssemblyAttribute.cs
- FamilyTypeface.cs
- OverflowException.cs
- List.cs
- DynamicControlParameter.cs
- Metadata.cs
- ISSmlParser.cs
- ApplicationGesture.cs
- HandledEventArgs.cs
- SrgsDocumentParser.cs
- ConsumerConnectionPointCollection.cs
- TextRunCacheImp.cs
- Mapping.cs
- PerformanceCounterLib.cs
- ListenerElementsCollection.cs
- ResourceDescriptionAttribute.cs
- SystemIcmpV4Statistics.cs
- SynchronizationScope.cs
- CollectionAdapters.cs
- ProtocolImporter.cs
- DataSourceHelper.cs
- IndentTextWriter.cs
- MD5CryptoServiceProvider.cs
- CssTextWriter.cs
- TypographyProperties.cs
- QueryContinueDragEvent.cs
- EntityDataSourceValidationException.cs
- SmuggledIUnknown.cs
- TagPrefixAttribute.cs
- SessionStateContainer.cs
- StringArrayConverter.cs
- WebAdminConfigurationHelper.cs
- ItemType.cs
- SmiEventSink.cs
- COM2PropertyPageUITypeConverter.cs
- DataContext.cs
- SqlRecordBuffer.cs
- TargetPerspective.cs