コード例 #1
0
        public void AddService(Type t, object owner, object instance)
        {
            Guard.ArgumentNotNull(t, nameof(t));
            Guard.ArgumentNotNull(owner, nameof(owner));
            Guard.ArgumentNotNull(instance, nameof(instance));

            string contract = AttributedModelServices.GetContractName(t);

            if (string.IsNullOrEmpty(contract))
            {
                throw new GitHubLogicException("Every type must have a contract name");
            }

            // we want to remove stale instances of a service, if they exist, regardless of who put them there
            RemoveService(t, null);

            var batch = new CompositionBatch();
            var part  = batch.AddExportedValue(contract, instance);

            if (part == null)
            {
                throw new GitHubLogicException("Adding an exported value must return a non-null part");
            }

            tempParts.Add(contract, new OwnedComposablePart {
                Owner = owner, Part = part
            });
            TempContainer.Compose(batch);
        }
コード例 #2
0
        public void AddService(Type t, object owner, object instance)
        {
            string contract = AttributedModelServices.GetContractName(t);

            Debug.Assert(!string.IsNullOrEmpty(contract), "Every type must have a contract name");

            // we want to remove stale instances of a service, if they exist, regardless of who put them there
            RemoveService(t, null);

            var batch = new CompositionBatch();
            var part  = batch.AddExportedValue(contract, instance);

            Debug.Assert(part != null, "Adding an exported value must return a non-null part");
            tempParts.Add(contract, new OwnedComposablePart {
                Owner = owner, Part = part
            });
            TempContainer.Compose(batch);
        }
コード例 #3
0
        /// <summary>
        /// Removes a service from the catalog
        /// </summary>
        /// <param name="t">The type we want to remove</param>
        /// <param name="owner">The owner, which either has to match what was passed to AddService,
        /// or if it's null, the service will be removed without checking for ownership</param>
        public void RemoveService(Type t, [AllowNull] object owner)
        {
            string contract = AttributedModelServices.GetContractName(t);

            Debug.Assert(!string.IsNullOrEmpty(contract), "Every type must have a contract name");

            OwnedComposablePart part;

            if (tempParts.TryGetValue(contract, out part))
            {
                if (owner != null && part.Owner != owner)
                {
                    return;
                }
                tempParts.Remove(contract);
                var batch = new CompositionBatch();
                batch.RemovePart(part.Part);
                TempContainer.Compose(batch);
            }
        }