Exemple #1
0
        public IRegister Callin(CreationFunc creationFunc)
        {
            //if (!injectionHash.ContainsKey(implementType)) return this;
            //(injectionHash[implementType] as InjectionInfo).CreationFunc = creationFunc;

            injectionHash[implementType.FullName] = GetInjectionInfo(implementType, creationFunc.Method.ReturnType);
            (injectionHash[implementType.FullName] as InjectionInfo).CreationFunc = creationFunc;

            return(this);
        }
Exemple #2
0
        /// <summary>
        /// Register a type mapping with the container.
        /// </summary>
        /// <typeparam name="T">Type to register.</typeparam>
        /// <param name="creation">Delegate method to create a new instance.</param>
        /// <param name="label">A unique label that allows multiple implementations of the same type.</param>
        /// <returns>The current IocContainer instance.</returns>
        public IocContainer Register <T>(CreationFunc creation, string label = null)
        {
            this.CheckDisposed();

            if (creation == null)
            {
                throw new ArgumentNullException("creation");
            }

            if (label == null)
            {
                label = DefaultLabel;
            }

            Type instanceType = typeof(T);

            lock (this._syncRoot)
            {
                if (this._container.ContainsKey(instanceType) &&
                    this._container[instanceType] != null &&
                    this._container[instanceType].ContainsKey(label) &&
                    this._container[instanceType][label] != null)
                {
                    if (this._container[instanceType][label].HasCreation)
                    {
                        throw new InvalidOperationException(string.Format("A definition of type {0} for label {1} already exists. Unregister the definition first.", instanceType.FullName, label));
                    }
                    else
                    {
                        this._container[instanceType][label].Creation = creation;

                        return(this);
                    }
                }
                else
                {
                    if (!this._container.ContainsKey(instanceType) || this._container[instanceType] == null)
                    {
                        this._container.Add(instanceType, new Dictionary <string, IocRegistration>(this._ignoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.CurrentCulture));
                    }

                    if (!this._container[instanceType].ContainsKey(label) || this._container[instanceType][label] == null)
                    {
                        this._container[instanceType].Add(label, new IocRegistration());
                    }

                    this._container[instanceType][label].Creation = creation;

                    return(this);
                }
            }
        }
Exemple #3
0
 public NameableFactory(CreationFunc <T> func, string name = null) : this(name)
 {
     factory = new Factory <T>(func);
 }
Exemple #4
0
 public Factory(Func <T> func)
 {
     this.func = obj => func();
 }
Exemple #5
0
 public Factory(CreationFunc <T> func)
 {
     this.func = func;
 }