Exemplo n.º 1
0
        /// <summary>
        /// Ensure a container exists for the given label
        /// </summary>
        /// <param name="label">A label to sub-divide containers</param>
        private void CheckContainer(string label)
        {
            RequireThat.IsNotNull(label, "label");

            if (_containers.ContainsKey(label))
            {
                return;
            }

            Monitor.Enter(_mutex);

            try
            {
                if (_containers.ContainsKey(label))
                {
                    return;
                }

                _containers.Add(label, new List <IIoCRegistration>());

                // every container can resolve the IOC parent
                var iocRegistration = new IocRegistration <IPortableIoC>(ioc => this);
                _containers[label].Add(iocRegistration);
            }
            finally
            {
                Monitor.Exit(_mutex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Register a type to be created with an alias to create that includes an
        /// instance of the IoC container
        /// </summary>
        /// <typeparam name="T">The type that is going to be implemented</typeparam>
        /// <param name="label">A unique label that allows multiple implementations of the same type</param>
        /// <param name="creation">An expression to create a new instance of the type</param>
        public void Register <T>(Func <IPortableIoC, T> creation, string label = "")
        {
            label = string.IsNullOrEmpty(label) ? DefaultLabel : label;

            RequireThat.IsNotNull(creation, "creation");

            CheckContainer(label);

            Monitor.Enter(_mutex);

            try
            {
                if (Exists(label, typeof(T)))
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  InvalidRegistration,
                                  label,
                                  typeof(T).FullName));
                }
                var iocRegistration = new IocRegistration <T>(creation);
                _containers[label].Add(iocRegistration);
            }
            finally
            {
                Monitor.Exit(_mutex);
            }
        }