Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Metadata / ObjectLayer / ObjectItemAssemblyLoader.cs / 1305376 / ObjectItemAssemblyLoader.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Data.Entity; namespace System.Data.Metadata.Edm { internal abstract class ObjectItemAssemblyLoader { protected const BindingFlags PropertyReflectionBindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; private readonly ObjectItemLoadingSessionData _sessionData; private Assembly _assembly; private AssemblyCacheEntry _cacheEntry; protected ObjectItemAssemblyLoader(Assembly assembly, AssemblyCacheEntry cacheEntry, ObjectItemLoadingSessionData sessionData) { _assembly = assembly; _cacheEntry = cacheEntry; _sessionData = sessionData; } internal virtual void Load() { AddToAssembliesLoaded(); LoadTypesFromAssembly(); AddToKnownAssemblies(); LoadClosureAssemblies(); } protected abstract void AddToAssembliesLoaded(); protected abstract void LoadTypesFromAssembly(); protected virtual void LoadClosureAssemblies() { LoadAssemblies(CacheEntry.ClosureAssemblies, SessionData); } internal virtual void OnLevel1SessionProcessing() { } internal virtual void OnLevel2SessionProcessing() { } internal static ObjectItemAssemblyLoader CreateLoader(Assembly assembly, ObjectItemLoadingSessionData sessionData) { ImmutableAssemblyCacheEntry cacheEntry; // KnownAssembly -> NoOp // Inside the LockedAssemblyCache means it is an attribute based assembly -> Cachedassembly // Inside the OcCache on EdmItemCollection -> cachedassembly // If none of above, setup the LoaderFactory based on the current assembly and EdmItemCollection if (sessionData.KnownAssemblies.Contains(assembly, sessionData.ObjectItemAssemblyLoaderFactory, sessionData.EdmItemCollection)) { return new ObjectItemNoOpAssemblyLoader(assembly, sessionData); } else if (sessionData.LockedAssemblyCache.TryGetValue(assembly, out cacheEntry)) { if (sessionData.ObjectItemAssemblyLoaderFactory == null) { if (cacheEntry.TypesInAssembly.Count != 0) { // we are loading based on attributes now sessionData.ObjectItemAssemblyLoaderFactory = ObjectItemAttributeAssemblyLoader.Create; } // if types in assembly are 0, don't commit to any loader yet } else if (sessionData.ObjectItemAssemblyLoaderFactory != ObjectItemAttributeAssemblyLoader.Create) { // we were loading in convention mode, and ran into an assembly that can't be loaded by convention // we know this because all cached assemblies are attribute based at the moment. sessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_OSpace_Convention_AttributeAssemblyReferenced(assembly.FullName), null)); } return new ObjectItemCachedAssemblyLoader(assembly, cacheEntry, sessionData); } else if (sessionData.EdmItemCollection != null && sessionData.EdmItemCollection.ConventionalOcCache.TryGetConventionalOcCacheFromAssemblyCache( assembly, out cacheEntry)) { sessionData.ObjectItemAssemblyLoaderFactory = ObjectItemConventionAssemblyLoader.Create; return new ObjectItemCachedAssemblyLoader(assembly, cacheEntry, sessionData); } else if (sessionData.ObjectItemAssemblyLoaderFactory == null) { if (ObjectItemAttributeAssemblyLoader.IsSchemaAttributePresent(assembly)) { sessionData.ObjectItemAssemblyLoaderFactory = ObjectItemAttributeAssemblyLoader.Create; } else if (ObjectItemConventionAssemblyLoader.SessionContainsConventionParameters(sessionData)) { sessionData.ObjectItemAssemblyLoaderFactory = ObjectItemConventionAssemblyLoader.Create; } } if (sessionData.ObjectItemAssemblyLoaderFactory != null) { return sessionData.ObjectItemAssemblyLoaderFactory(assembly, sessionData); } return new ObjectItemNoOpAssemblyLoader(assembly, sessionData); } internal static bool IsAttributeLoader(object loaderCookie) { Debug.Assert(loaderCookie == null || loaderCookie is Func, "Non loader cookie passed in"); return IsAttributeLoader(loaderCookie as Func ); } internal static bool IsAttributeLoader(Func loaderFactory) { if (loaderFactory == null) { return false; } return loaderFactory == ObjectItemAttributeAssemblyLoader.Create; } internal static bool IsConventionLoader(Func loaderFactory) { if (loaderFactory == null) { return false; } return loaderFactory == ObjectItemConventionAssemblyLoader.Create; } protected virtual void AddToKnownAssemblies() { Debug.Assert(!_sessionData.KnownAssemblies.Contains(_assembly, SessionData.ObjectItemAssemblyLoaderFactory, _sessionData.EdmItemCollection), "This assembly must not be present in the list of known assemblies"); _sessionData.KnownAssemblies.Add(_assembly, new KnownAssemblyEntry(CacheEntry, SessionData.EdmItemCollection != null)); } protected static void LoadAssemblies(IEnumerable assemblies, ObjectItemLoadingSessionData sessionData) { foreach (Assembly assembly in assemblies) { ObjectItemAssemblyLoader loader = ObjectItemAssemblyLoader.CreateLoader(assembly, sessionData); loader.Load(); } } protected bool TryGetPrimitiveType(Type type, out bool nullable, out PrimitiveType primitiveType) { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) { nullable = true; type = type.GetGenericArguments()[0]; } else { nullable = !type.IsValueType; } return ClrProviderManifest.Instance.TryGetPrimitiveType(type, out primitiveType); } protected ObjectItemLoadingSessionData SessionData { get { return _sessionData; } } protected Assembly SourceAssembly { get { return _assembly; } } protected AssemblyCacheEntry CacheEntry { get { return _cacheEntry; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Data.Entity; namespace System.Data.Metadata.Edm { internal abstract class ObjectItemAssemblyLoader { protected const BindingFlags PropertyReflectionBindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; private readonly ObjectItemLoadingSessionData _sessionData; private Assembly _assembly; private AssemblyCacheEntry _cacheEntry; protected ObjectItemAssemblyLoader(Assembly assembly, AssemblyCacheEntry cacheEntry, ObjectItemLoadingSessionData sessionData) { _assembly = assembly; _cacheEntry = cacheEntry; _sessionData = sessionData; } internal virtual void Load() { AddToAssembliesLoaded(); LoadTypesFromAssembly(); AddToKnownAssemblies(); LoadClosureAssemblies(); } protected abstract void AddToAssembliesLoaded(); protected abstract void LoadTypesFromAssembly(); protected virtual void LoadClosureAssemblies() { LoadAssemblies(CacheEntry.ClosureAssemblies, SessionData); } internal virtual void OnLevel1SessionProcessing() { } internal virtual void OnLevel2SessionProcessing() { } internal static ObjectItemAssemblyLoader CreateLoader(Assembly assembly, ObjectItemLoadingSessionData sessionData) { ImmutableAssemblyCacheEntry cacheEntry; // KnownAssembly -> NoOp // Inside the LockedAssemblyCache means it is an attribute based assembly -> Cachedassembly // Inside the OcCache on EdmItemCollection -> cachedassembly // If none of above, setup the LoaderFactory based on the current assembly and EdmItemCollection if (sessionData.KnownAssemblies.Contains(assembly, sessionData.ObjectItemAssemblyLoaderFactory, sessionData.EdmItemCollection)) { return new ObjectItemNoOpAssemblyLoader(assembly, sessionData); } else if (sessionData.LockedAssemblyCache.TryGetValue(assembly, out cacheEntry)) { if (sessionData.ObjectItemAssemblyLoaderFactory == null) { if (cacheEntry.TypesInAssembly.Count != 0) { // we are loading based on attributes now sessionData.ObjectItemAssemblyLoaderFactory = ObjectItemAttributeAssemblyLoader.Create; } // if types in assembly are 0, don't commit to any loader yet } else if (sessionData.ObjectItemAssemblyLoaderFactory != ObjectItemAttributeAssemblyLoader.Create) { // we were loading in convention mode, and ran into an assembly that can't be loaded by convention // we know this because all cached assemblies are attribute based at the moment. sessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_OSpace_Convention_AttributeAssemblyReferenced(assembly.FullName), null)); } return new ObjectItemCachedAssemblyLoader(assembly, cacheEntry, sessionData); } else if (sessionData.EdmItemCollection != null && sessionData.EdmItemCollection.ConventionalOcCache.TryGetConventionalOcCacheFromAssemblyCache( assembly, out cacheEntry)) { sessionData.ObjectItemAssemblyLoaderFactory = ObjectItemConventionAssemblyLoader.Create; return new ObjectItemCachedAssemblyLoader(assembly, cacheEntry, sessionData); } else if (sessionData.ObjectItemAssemblyLoaderFactory == null) { if (ObjectItemAttributeAssemblyLoader.IsSchemaAttributePresent(assembly)) { sessionData.ObjectItemAssemblyLoaderFactory = ObjectItemAttributeAssemblyLoader.Create; } else if (ObjectItemConventionAssemblyLoader.SessionContainsConventionParameters(sessionData)) { sessionData.ObjectItemAssemblyLoaderFactory = ObjectItemConventionAssemblyLoader.Create; } } if (sessionData.ObjectItemAssemblyLoaderFactory != null) { return sessionData.ObjectItemAssemblyLoaderFactory(assembly, sessionData); } return new ObjectItemNoOpAssemblyLoader(assembly, sessionData); } internal static bool IsAttributeLoader(object loaderCookie) { Debug.Assert(loaderCookie == null || loaderCookie is Func, "Non loader cookie passed in"); return IsAttributeLoader(loaderCookie as Func ); } internal static bool IsAttributeLoader(Func loaderFactory) { if (loaderFactory == null) { return false; } return loaderFactory == ObjectItemAttributeAssemblyLoader.Create; } internal static bool IsConventionLoader(Func loaderFactory) { if (loaderFactory == null) { return false; } return loaderFactory == ObjectItemConventionAssemblyLoader.Create; } protected virtual void AddToKnownAssemblies() { Debug.Assert(!_sessionData.KnownAssemblies.Contains(_assembly, SessionData.ObjectItemAssemblyLoaderFactory, _sessionData.EdmItemCollection), "This assembly must not be present in the list of known assemblies"); _sessionData.KnownAssemblies.Add(_assembly, new KnownAssemblyEntry(CacheEntry, SessionData.EdmItemCollection != null)); } protected static void LoadAssemblies(IEnumerable assemblies, ObjectItemLoadingSessionData sessionData) { foreach (Assembly assembly in assemblies) { ObjectItemAssemblyLoader loader = ObjectItemAssemblyLoader.CreateLoader(assembly, sessionData); loader.Load(); } } protected bool TryGetPrimitiveType(Type type, out bool nullable, out PrimitiveType primitiveType) { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) { nullable = true; type = type.GetGenericArguments()[0]; } else { nullable = !type.IsValueType; } return ClrProviderManifest.Instance.TryGetPrimitiveType(type, out primitiveType); } protected ObjectItemLoadingSessionData SessionData { get { return _sessionData; } } protected Assembly SourceAssembly { get { return _assembly; } } protected AssemblyCacheEntry CacheEntry { get { return _cacheEntry; } } } } // 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
- XsdDataContractExporter.cs
- UnaryNode.cs
- TabItemWrapperAutomationPeer.cs
- IsolatedStorageFile.cs
- StringArrayConverter.cs
- GridPattern.cs
- PngBitmapDecoder.cs
- SelectionProviderWrapper.cs
- ColumnWidthChangingEvent.cs
- OracleConnection.cs
- Environment.cs
- ObjectDataSourceView.cs
- CurrencyManager.cs
- FilterException.cs
- ProvidePropertyAttribute.cs
- PolicyManager.cs
- BuilderInfo.cs
- GlyphInfoList.cs
- PropertyChangedEventArgs.cs
- ADMembershipProvider.cs
- CallContext.cs
- HtmlGenericControl.cs
- FrameworkElementAutomationPeer.cs
- Invariant.cs
- Site.cs
- cryptoapiTransform.cs
- CultureInfoConverter.cs
- SmtpSection.cs
- DescendantOverDescendantQuery.cs
- TriggerActionCollection.cs
- BaseAsyncResult.cs
- DefaultPropertyAttribute.cs
- DateTime.cs
- _ReceiveMessageOverlappedAsyncResult.cs
- VisualBrush.cs
- XmlWrappingReader.cs
- panel.cs
- NetDispatcherFaultException.cs
- HttpModuleActionCollection.cs
- DataGridColumnReorderingEventArgs.cs
- MSAANativeProvider.cs
- Action.cs
- DataShape.cs
- RSACryptoServiceProvider.cs
- Win32KeyboardDevice.cs
- CryptoStream.cs
- SHA1CryptoServiceProvider.cs
- RelationshipManager.cs
- WinFormsUtils.cs
- XmlSerializerNamespaces.cs
- SHA512Managed.cs
- BitmapSizeOptions.cs
- ServiceElementCollection.cs
- ContextMenuAutomationPeer.cs
- HttpsHostedTransportConfiguration.cs
- TextEditorTyping.cs
- RectConverter.cs
- VectorAnimationBase.cs
- DataGridViewAdvancedBorderStyle.cs
- TableItemPatternIdentifiers.cs
- ListViewGroupItemCollection.cs
- PropertyToken.cs
- Base64Encoding.cs
- PropertyTabChangedEvent.cs
- AVElementHelper.cs
- ClassHandlersStore.cs
- CancellationState.cs
- ClientRequest.cs
- QuestionEventArgs.cs
- InstanceDataCollectionCollection.cs
- Rect.cs
- RegexFCD.cs
- SortQueryOperator.cs
- cryptoapiTransform.cs
- PropertyTab.cs
- VerticalAlignConverter.cs
- Helpers.cs
- SmtpCommands.cs
- SessionSymmetricMessageSecurityProtocolFactory.cs
- OutgoingWebRequestContext.cs
- FutureFactory.cs
- ThreadStateException.cs
- Message.cs
- SmtpMail.cs
- TableLayoutSettingsTypeConverter.cs
- PersonalizationProvider.cs
- WindowsListViewItemCheckBox.cs
- DbUpdateCommandTree.cs
- DockPanel.cs
- NativeMethodsCLR.cs
- RectangleHotSpot.cs
- FontNamesConverter.cs
- XmlFormatWriterGenerator.cs
- HttpContextServiceHost.cs
- ArgumentOutOfRangeException.cs
- CallTemplateAction.cs
- ObjectNavigationPropertyMapping.cs
- Memoizer.cs
- GlyphTypeface.cs
- HtmlTableCell.cs