ImportEngine.RecompositionManager.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.RecompositionManager.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 effiecently store and retrieve the list of parts
        ///     that will be affected by changes to exports. This allows the <see cref="ImportEngine"/> to properly
        ///     block breaking changes and also recompose imports as appropriate.
        /// </summary>
        private class RecompositionManager
        {
            private WeakReferenceCollection<PartManager> _partsToIndex = new WeakReferenceCollection<PartManager>();
            private WeakReferenceCollection<PartManager> _partsToUnindex = new WeakReferenceCollection<PartManager>();
            private Dictionary<string, WeakReferenceCollection<PartManager>> _partManagerIndex = new Dictionary<string, WeakReferenceCollection<PartManager>>();

            public void AddPartToIndex(PartManager partManager)
            {
                this._partsToIndex.Add(partManager);
            }

            public void AddPartToUnindex(PartManager partManager)
            {
                this._partsToUnindex.Add(partManager);
            }

            public IEnumerable<PartManager> GetAffectedParts(IEnumerable<string> changedContractNames)
            {
                this.UpdateImportIndex();

                List<PartManager> parts = new List<PartManager>();

                parts.AddRange(GetPartsImporting(ImportDefinition.EmptyContractName));

                foreach (string contractName in changedContractNames)
                {
                    parts.AddRange(GetPartsImporting(contractName));
                }

                return parts;
            }

            public static IEnumerable<ImportDefinition> GetAffectedImports(ComposablePart part, IEnumerable<ExportDefinition> changedExports)
            {
                return part.ImportDefinitions.Where(import => IsAffectedImport(import, changedExports));
            }

            private static bool IsAffectedImport(ImportDefinition import, IEnumerable<ExportDefinition> changedExports)
            {
                // This could be more efficient still if the export definitions were indexed by contract name,
                // only worth revisiting if we need to squeeze more performance out of recomposition
                foreach (var export in changedExports)
                {
                    if (import.IsConstraintSatisfiedBy(export))
                    {
                        return true;
                    }
                }
               
                return false;
            }

            public IEnumerable<PartManager> GetPartsImporting(string contractName)
            {
                WeakReferenceCollection<PartManager> partManagerList;
                if (!this._partManagerIndex.TryGetValue(contractName, out partManagerList))
                {
                    return Enumerable.Empty<PartManager>();
                }

                return partManagerList.AliveItemsToList();
            }

            private void AddIndexEntries(PartManager partManager)
            {
                foreach (string contractName in partManager.GetImportedContractNames())
                {
                    WeakReferenceCollection<PartManager> indexEntries;
                    if (!this._partManagerIndex.TryGetValue(contractName, out indexEntries))
                    {
                        indexEntries = new WeakReferenceCollection<PartManager>();
                        this._partManagerIndex.Add(contractName, indexEntries);
                    }

                    if (!indexEntries.Contains(partManager))
                    {
                        indexEntries.Add(partManager);
                    }
                }
            }

            private void RemoveIndexEntries(PartManager partManager)
            {
                foreach (string contractName in partManager.GetImportedContractNames())
                {
                    WeakReferenceCollection<PartManager> indexEntries;
                    if (this._partManagerIndex.TryGetValue(contractName, out indexEntries))
                    {
                        indexEntries.Remove(partManager);
                        var aliveItems = indexEntries.AliveItemsToList();

                        if (aliveItems.Count == 0)
                        {
                            this._partManagerIndex.Remove(contractName);
                        }
                    }
                }
            }

            private void UpdateImportIndex()
            {
                var partsToIndex = this._partsToIndex.AliveItemsToList();
                this._partsToIndex.Clear();

                var partsToUnindex = this._partsToUnindex.AliveItemsToList();
                this._partsToUnindex.Clear();

                if (partsToIndex.Count == 0 && partsToUnindex.Count == 0)
                {
                    return;
                }

                foreach (var partManager in partsToIndex)
                {
                    var index = partsToUnindex.IndexOf(partManager);

                    // If the same part is being added and removed we can ignore both
                    if (index >= 0)
                    {
                        partsToUnindex[index] = null;
                    }
                    else
                    {
                        AddIndexEntries(partManager);
                    }
                }

                foreach (var partManager in partsToUnindex)
                {
                    if (partManager != null)
                    {
                        RemoveIndexEntries(partManager);
                    }
                }
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.