Example #1
0
        public void Deregister(Type type, string instanceName = null)
        {
            var           key = new MappingKey(type, default(bool), instanceName);
            Func <object> obj;

            if (Mappings.TryGetValue(key, out obj))
            {
                try
                {
                    Mappings.Remove(Mappings.FirstOrDefault(x => x.Value == obj).Key);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }
Example #2
0
        /// <exception cref="ArgumentNullException">The value of 'type' cannot be null. </exception>
        public void Register(Type type,
                             Func <object> createInstanceDelegate,
                             bool singleton      = false,
                             bool initialize     = false,
                             string instanceName = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

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

            var key = new MappingKey(type, singleton, instanceName);

            if (!Mappings.ContainsKey(key))
            {
                if (initialize)
                {
                    try
                    {
                        if (singleton)
                        {
                            key.Instance = createInstanceDelegate();
                        }
                        else
                        {
                            createInstanceDelegate();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
                Mappings.Add(key, createInstanceDelegate);
            }
        }
Example #3
0
        /// <exception cref="InvalidOperationException">Condition. </exception>
        public object Resolve(Type type, string instanceName = null)
        {
            var           key = new MappingKey(type, default(bool), instanceName);
            Func <object> obj;

            try
            {
                if (Mappings.TryGetValue(key, out obj))
                {
                    var mk = Mappings.FirstOrDefault(x => x.Value == obj).Key;

                    if (mk.Singleton)
                    {
                        return(mk.Instance ?? (mk.Instance = obj()));
                    }
                    return(obj());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            throw new InvalidOperationException(string.Format("Could not find mapping for type '{0}'", type.FullName));
        }