コード例 #1
0
        /// <inheritdoc />
        public void AddAdapter(IRegionAdapter regionAdapter)
        {
            if (regionAdapter == null)
            {
                throw new ArgumentNullException(nameof(regionAdapter));
            }

            if (this.Adapters.Contains(regionAdapter))
            {
                return;
            }

            Trace.TraceInformation($"Adding region adapter: {regionAdapter.GetType().Name}");

            this.Adapters.Add(regionAdapter);
        }
コード例 #2
0
        /// <inheritdoc />
        public void RemoveAdapter(IRegionAdapter regionAdapter)
        {
            if (regionAdapter == null)
            {
                throw new ArgumentNullException(nameof(regionAdapter));
            }

            Trace.TraceInformation($"Removing region adapter: {regionAdapter.GetType().Name}");

            foreach (KeyValuePair <string, Tuple <object, IRegionAdapter> > region in this.RegionDictionary)
            {
                if (ReferenceEquals(regionAdapter, region.Value.Item2))
                {
                    throw new InvalidOperationException("The specified region adapter is still in use.");
                }
            }

            this.Adapters.Remove(regionAdapter);
        }
コード例 #3
0
        /// <inheritdoc />
        public void AddRegion(string region, object container)
        {
            if (region == null)
            {
                throw new ArgumentNullException(nameof(region));
            }

            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            if (string.IsNullOrWhiteSpace(region))
            {
                throw new ArgumentException("Parameter is an empty string.", nameof(region));
            }

            Trace.TraceInformation($"Adding region {region} to: {container.GetType().Name}");

            List <Tuple <int, IRegionAdapter> > adapters = new List <Tuple <int, IRegionAdapter> >();
            Type containerType = container.GetType();

            foreach (IRegionAdapter currentAdapter in this.Adapters)
            {
                if (currentAdapter.IsCompatibleContainer(containerType, out int inheritanceDepth))
                {
                    adapters.Add(new Tuple <int, IRegionAdapter>(inheritanceDepth, currentAdapter));
                }
            }

            adapters.Sort((x, y) => x.Item1.CompareTo(y.Item1));

            if (adapters.Count == 0)
            {
                throw new NotSupportedException($"No region adapter supports the container type {containerType.Name}");
            }

            IRegionAdapter adapter = adapters[0]
                                     .Item2;

            Trace.TraceInformation($"Used region adapter for region{region} at {container.GetType().Name}: {adapter.GetType().Name}");

            if (this.RegionDictionary.ContainsKey(region))
            {
                if (ReferenceEquals(container, this.RegionDictionary[region]
                                    .Item1) && adapter.Equals(this.RegionDictionary[region]
                                                              .Item2))
                {
                    return;
                }

                this.RegionDictionary.Remove(region);
            }

            this.RegionDictionary.Add(region, new Tuple <object, IRegionAdapter>(container, adapter));
        }