ImportEngine.PartManager.cs :  » 2.6.4-mono-.net-core » System.ComponentModel » System » ComponentModel » Composition » Hosting » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » 2.6.4 mono .net core » System.ComponentModel 
System.ComponentModel » System » ComponentModel » Composition » Hosting » ImportEngine.PartManager.cs
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Primitives;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.Internal;
using Microsoft.Internal.Collections;

namespace System.ComponentModel.Composition.Hosting{
    public partial class ImportEngine
    {
        /// <summary>
        ///     Used by the <see cref="ImportEngine"/> to manage the composition of a given part.
        ///     It stores things like the list of disposable exports used to satisfy the imports as
        ///     well as the caching of the exports discovered during previewing of a part.
        /// </summary>
        private class PartManager
        {
            private Dictionary<ImportDefinition, List<IDisposable>> _importedDisposableExports;
            private Dictionary<ImportDefinition, Export[]> _importCache;
            private string[] _importedContractNames;
            private ComposablePart _part;
            private ImportState _state = ImportState.NoImportsSatisfied;
            private readonly ImportEngine _importEngine;

            public PartManager(ImportEngine importEngine, ComposablePart part)
            {
                this._importEngine = importEngine;
                this._part = part;
            }

            public ComposablePart Part
            {
                get
                {
                    return this._part;
                }
            }

            public ImportState State
            {
                get
                {
                    using (this._importEngine._lock.LockStateForRead())
                    {
                        return this._state;
                    }
                }
                set
                {
                    using (this._importEngine._lock.LockStateForWrite())
                    {
                        this._state = value;
                    }
                }
            }

            public bool TrackingImports { get; set; }

            public IEnumerable<string> GetImportedContractNames()
            {
                if (this.Part == null)
                {
                    return Enumerable.Empty<string>();
                }

                if (this._importedContractNames == null)
                {
                    this._importedContractNames = this.Part.ImportDefinitions.Select(import => import.ContractName ?? ImportDefinition.EmptyContractName).Distinct().ToArray();
                }
                return this._importedContractNames;
            }

            public CompositionResult TrySetImport(ImportDefinition import, IEnumerable<Export> exports)
            {
                try
                {
                    this.Part.SetImport(import, exports);
                    UpdateDisposableDependencies(import, exports);
                    return CompositionResult.SucceededResult;
                }
                catch (CompositionException ex)
                {   // Pulling on one of the exports failed

                    return new CompositionResult(
                        ErrorBuilder.CreatePartCannotSetImport(Part, import, ex));
                }
                catch (ComposablePartException ex)
                {   // Type mismatch between export and import

                    return new CompositionResult(
                        ErrorBuilder.CreatePartCannotSetImport(Part, import, ex));
                }
            }

            public void SetSavedImport(ImportDefinition import, Export[] exports, AtomicComposition atomicComposition)
            {
                if (atomicComposition != null)
                {
                    var savedExports = this.GetSavedImport(import);

                    // Add a revert action to revert the stored exports
                    // in the case that this atomicComposition gets rolled back.
                    atomicComposition.AddRevertAction(() =>
                        this.SetSavedImport(import, savedExports, null));
                }

                if (this._importCache == null)
                {
                    this._importCache = new Dictionary<ImportDefinition, Export[]>();
                }

                this._importCache[import] = exports;
            }

            public Export[] GetSavedImport(ImportDefinition import)
            {
                Export[] exports = null;
                if (this._importCache != null)
                {
                    // We don't care about the return value we just want the exports
                    // and if it isn't present we just return the initialized null value
                    this._importCache.TryGetValue(import, out exports);
                }
                return exports;
            }

            public void ClearSavedImports()
            {
                this._importCache = null;
            }

            public CompositionResult TryOnComposed()
            {
                try
                {
                    this.Part.Activate();
                    return CompositionResult.SucceededResult;
                }
                catch (ComposablePartException ex)
                {   // Type failed to be constructed, imports could not be set, etc
                    return new CompositionResult(
                        ErrorBuilder.CreatePartCannotActivate(this.Part, ex));
                }
            }

            public void UpdateDisposableDependencies(ImportDefinition import, IEnumerable<Export> exports)
            {
                // Determine if there are any new disposable exports, optimizing for the most
                // likely case, which is that there aren't any
                List<IDisposable> disposableExports = null;
                foreach (var disposableExport in exports.OfType<IDisposable>())
                {
                    if (disposableExports == null)
                    {
                        disposableExports = new List<IDisposable>();
                    }
                    disposableExports.Add(disposableExport);
                }

                // Dispose any existing references previously set on this import
                List<IDisposable> oldDisposableExports = null;
                if (this._importedDisposableExports != null &&
                    this._importedDisposableExports.TryGetValue(import, out oldDisposableExports))
                {
                    oldDisposableExports.ForEach(disposable => disposable.Dispose());

                    // If there aren't any replacements, get rid of the old storage
                    if (disposableExports == null)
                    {
                        this._importedDisposableExports.Remove(import);
                        if (!this._importedDisposableExports.FastAny())
                        {
                            this._importedDisposableExports = null;
                        }

                        return;
                    }
                }

                // Record the new collection
                if (disposableExports != null)
                {
                    if (this._importedDisposableExports == null)
                    {
                        this._importedDisposableExports = new Dictionary<ImportDefinition, List<IDisposable>>();
                    }
                    this._importedDisposableExports[import] = disposableExports;
                }
            }

            public void DisposeAllDependencies()
            {
                if (this._importedDisposableExports != null)
                {
                    IEnumerable<IDisposable> dependencies = this._importedDisposableExports.Values
                        .SelectMany(exports => exports);

                    this._importedDisposableExports = null;

                    dependencies.ForEach(disposableExport => disposableExport.Dispose());
                }
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.