Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Objects / ObjectViewListener.cs / 1305376 / ObjectViewListener.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using System.Diagnostics;
// Dev notes -1
// why we need this class: in order to keep the view alive, we have to listen to evens from entities and
// also EntityCollection/ObjectStateManager they exists in. listening to event will prevent the view to be
// disposed, hence GC'ed due to having a strong reference; and to avoid this situation we have to introduce
// a new layer which will have a weakreference to view (1-so it can go out of scope, 2- this layer will listen to
// the events and notify the view - by calling its APIS- for any change that happens)
// Dev notes -2
// following statement is valid on current existing CLR:
// lets say Customer is an Entity, Array[Customer] is not Array[Entity]; it is not supported
// to do the work around we have to use a non-Generic interface/class so we can pass the view
// to ObjectViewListener safely (IObjectView)
namespace System.Data.Objects
{
internal sealed class ObjectViewListener
{
private WeakReference _viewWeak;
private object _dataSource;
private IList _list;
internal ObjectViewListener(IObjectView view, IList list, object dataSource)
{
_viewWeak = new WeakReference(view);
_dataSource = dataSource;
_list = list;
RegisterCollectionEvents();
RegisterEntityEvents();
}
private void CleanUpListener()
{
UnregisterCollectionEvents();
UnregisterEntityEvents();
}
private void RegisterCollectionEvents()
{
ObjectStateManager cache = _dataSource as ObjectStateManager;
if (cache != null)
{
cache.EntityDeleted += CollectionChanged;
}
else if (null != _dataSource)
{
((RelatedEnd)_dataSource).AssociationChangedForObjectView += CollectionChanged;
}
}
private void UnregisterCollectionEvents()
{
ObjectStateManager cache = _dataSource as ObjectStateManager;
if (cache != null)
{
cache.EntityDeleted -= CollectionChanged;
}
else if (null != _dataSource)
{
((RelatedEnd)_dataSource).AssociationChangedForObjectView -= CollectionChanged;
}
}
internal void RegisterEntityEvents(object entity)
{
Debug.Assert(entity != null, "Entity should not be null");
INotifyPropertyChanged propChanged = entity as INotifyPropertyChanged;
if (propChanged != null)
{
propChanged.PropertyChanged += EntityPropertyChanged;
}
}
private void RegisterEntityEvents()
{
if (null != _list)
{
foreach (object entityObject in _list)
{
INotifyPropertyChanged propChanged = entityObject as INotifyPropertyChanged;
if (propChanged != null)
{
propChanged.PropertyChanged += EntityPropertyChanged;
}
}
}
}
internal void UnregisterEntityEvents(object entity)
{
Debug.Assert(entity != null, "entity should not be null");
INotifyPropertyChanged propChanged = entity as INotifyPropertyChanged;
if (propChanged != null)
{
propChanged.PropertyChanged -= EntityPropertyChanged;
}
}
private void UnregisterEntityEvents()
{
if (null != _list)
{
foreach (object entityObject in _list)
{
INotifyPropertyChanged propChanged = entityObject as INotifyPropertyChanged;
if (propChanged != null)
{
propChanged.PropertyChanged -= EntityPropertyChanged;
}
}
}
}
private void EntityPropertyChanged(object sender, PropertyChangedEventArgs e)
{
IObjectView view = (IObjectView)_viewWeak.Target;
if (view != null)
{
view.EntityPropertyChanged(sender, e);
}
else
{
CleanUpListener();
}
}
private void CollectionChanged(object sender, CollectionChangeEventArgs e)
{
IObjectView view = (IObjectView)_viewWeak.Target;
if (view != null)
{
view.CollectionChanged(sender, e);
}
else
{
CleanUpListener();
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using System.Diagnostics;
// Dev notes -1
// why we need this class: in order to keep the view alive, we have to listen to evens from entities and
// also EntityCollection/ObjectStateManager they exists in. listening to event will prevent the view to be
// disposed, hence GC'ed due to having a strong reference; and to avoid this situation we have to introduce
// a new layer which will have a weakreference to view (1-so it can go out of scope, 2- this layer will listen to
// the events and notify the view - by calling its APIS- for any change that happens)
// Dev notes -2
// following statement is valid on current existing CLR:
// lets say Customer is an Entity, Array[Customer] is not Array[Entity]; it is not supported
// to do the work around we have to use a non-Generic interface/class so we can pass the view
// to ObjectViewListener safely (IObjectView)
namespace System.Data.Objects
{
internal sealed class ObjectViewListener
{
private WeakReference _viewWeak;
private object _dataSource;
private IList _list;
internal ObjectViewListener(IObjectView view, IList list, object dataSource)
{
_viewWeak = new WeakReference(view);
_dataSource = dataSource;
_list = list;
RegisterCollectionEvents();
RegisterEntityEvents();
}
private void CleanUpListener()
{
UnregisterCollectionEvents();
UnregisterEntityEvents();
}
private void RegisterCollectionEvents()
{
ObjectStateManager cache = _dataSource as ObjectStateManager;
if (cache != null)
{
cache.EntityDeleted += CollectionChanged;
}
else if (null != _dataSource)
{
((RelatedEnd)_dataSource).AssociationChangedForObjectView += CollectionChanged;
}
}
private void UnregisterCollectionEvents()
{
ObjectStateManager cache = _dataSource as ObjectStateManager;
if (cache != null)
{
cache.EntityDeleted -= CollectionChanged;
}
else if (null != _dataSource)
{
((RelatedEnd)_dataSource).AssociationChangedForObjectView -= CollectionChanged;
}
}
internal void RegisterEntityEvents(object entity)
{
Debug.Assert(entity != null, "Entity should not be null");
INotifyPropertyChanged propChanged = entity as INotifyPropertyChanged;
if (propChanged != null)
{
propChanged.PropertyChanged += EntityPropertyChanged;
}
}
private void RegisterEntityEvents()
{
if (null != _list)
{
foreach (object entityObject in _list)
{
INotifyPropertyChanged propChanged = entityObject as INotifyPropertyChanged;
if (propChanged != null)
{
propChanged.PropertyChanged += EntityPropertyChanged;
}
}
}
}
internal void UnregisterEntityEvents(object entity)
{
Debug.Assert(entity != null, "entity should not be null");
INotifyPropertyChanged propChanged = entity as INotifyPropertyChanged;
if (propChanged != null)
{
propChanged.PropertyChanged -= EntityPropertyChanged;
}
}
private void UnregisterEntityEvents()
{
if (null != _list)
{
foreach (object entityObject in _list)
{
INotifyPropertyChanged propChanged = entityObject as INotifyPropertyChanged;
if (propChanged != null)
{
propChanged.PropertyChanged -= EntityPropertyChanged;
}
}
}
}
private void EntityPropertyChanged(object sender, PropertyChangedEventArgs e)
{
IObjectView view = (IObjectView)_viewWeak.Target;
if (view != null)
{
view.EntityPropertyChanged(sender, e);
}
else
{
CleanUpListener();
}
}
private void CollectionChanged(object sender, CollectionChangeEventArgs e)
{
IObjectView view = (IObjectView)_viewWeak.Target;
if (view != null)
{
view.CollectionChanged(sender, e);
}
else
{
CleanUpListener();
}
}
}
}
// 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
- OleDbPropertySetGuid.cs
- UserNameSecurityToken.cs
- DocumentPropertiesDialog.cs
- ClientConfigurationHost.cs
- DeviceContexts.cs
- TextBox.cs
- ScriptRegistrationManager.cs
- SettingsPropertyNotFoundException.cs
- ObjectQueryProvider.cs
- ICspAsymmetricAlgorithm.cs
- FontFamily.cs
- ProfileParameter.cs
- AbandonedMutexException.cs
- TextTreeNode.cs
- SerializationInfo.cs
- TextLineResult.cs
- GeometryModel3D.cs
- CodeBinaryOperatorExpression.cs
- ScheduleChanges.cs
- NameValueConfigurationCollection.cs
- CssTextWriter.cs
- QuaternionAnimation.cs
- Shape.cs
- RandomNumberGenerator.cs
- ImageListStreamer.cs
- TypeLoadException.cs
- ApplicationBuildProvider.cs
- ListDependantCardsRequest.cs
- ReadOnlyDataSourceView.cs
- Semaphore.cs
- TextOptionsInternal.cs
- ThumbButtonInfo.cs
- Calendar.cs
- xmlsaver.cs
- ExpressionHelper.cs
- ColorMap.cs
- DisableDpiAwarenessAttribute.cs
- DefaultHttpHandler.cs
- Splitter.cs
- XmlSubtreeReader.cs
- SafeCryptoHandles.cs
- GCHandleCookieTable.cs
- OrderingQueryOperator.cs
- SystemMulticastIPAddressInformation.cs
- Transform3DCollection.cs
- XamlDesignerSerializationManager.cs
- ReflectPropertyDescriptor.cs
- JumpList.cs
- SystemParameters.cs
- SmtpMail.cs
- StringResourceManager.cs
- ScrollProviderWrapper.cs
- CalendarDayButton.cs
- HttpProfileBase.cs
- MethodSet.cs
- IsolatedStorageFileStream.cs
- DateTimeUtil.cs
- SoapSchemaMember.cs
- XmlWellformedWriterHelpers.cs
- DiagnosticTraceSource.cs
- DiscoveryExceptionDictionary.cs
- MimeTypePropertyAttribute.cs
- NestPullup.cs
- AsymmetricKeyExchangeDeformatter.cs
- FilePresentation.cs
- ZipIORawDataFileBlock.cs
- safelinkcollection.cs
- streamingZipPartStream.cs
- AdCreatedEventArgs.cs
- DataBindEngine.cs
- CompositeDispatchFormatter.cs
- QilIterator.cs
- DecoderFallback.cs
- SecurityRuntime.cs
- ControlCachePolicy.cs
- _IPv4Address.cs
- LoadRetryConstantStrategy.cs
- DataViewManager.cs
- ObjectViewFactory.cs
- UpdateCommand.cs
- DataGridSortCommandEventArgs.cs
- ProvidersHelper.cs
- VarInfo.cs
- TypeReference.cs
- AppDomainAttributes.cs
- Encoder.cs
- IResourceProvider.cs
- ToolboxService.cs
- SqlCacheDependencyDatabaseCollection.cs
- BindingObserver.cs
- SourceFilter.cs
- HtmlElementCollection.cs
- SoapAttributeAttribute.cs
- GifBitmapEncoder.cs
- CellTreeNodeVisitors.cs
- BoolLiteral.cs
- ShapingEngine.cs
- SimpleNameService.cs
- MimeMultiPart.cs
- RegexMatchCollection.cs