Exemple #1
0
        /// <summary>
        /// Trys to get an instance from the instance store, creating it if it doesnt exist
        /// </summary>
        /// <param name="requestType">The requested type</param>
        /// <param name="instanceStore"></param>
        /// <param name="tempInstanceStore"></param>
        /// <param name="buildStack"></param>
        /// <returns></returns>
        IEnumerable GetOrCreateInstances(Type requestType, IInstanceStore instanceStore, IInstanceStore tempInstanceStore, Stack <Type> buildStack)
        {
            var typesToCreate = this.GetTypesToCreate(requestType, buildStack);

            var instances = new List <Tuple <Registration, object> >();

            if (tempInstanceStore != null && tempInstanceStore.ContainsInstancesFor(requestType))
            {
                instances.AddRange(tempInstanceStore.GetInstances(requestType).Cast <Tuple <Registration, object> >());
            }
            else if (instanceStore.ContainsInstancesFor(requestType))
            {
                instances.AddRange(instanceStore.GetInstances(requestType).Cast <Tuple <Registration, object> >());
            }

            foreach (var registration in typesToCreate)
            {
                if (!instances.Any(i => i != null && i.Item1 == registration))
                {
                    var newinstance = this.GetInstance(registration, tempInstanceStore, new Stack <Type>(buildStack.Reverse()));

                    instanceStore.Insert(registration, requestType, newinstance);

                    instances.Add(new Tuple <Registration, object>(registration, newinstance));
                }
            }

            return(instances.Select(i => i.Item2).ToArray());
        }
Exemple #2
0
        object Resolve(Type type, IInstanceStore tempInstanceStore, Stack <Type> buildStack)
        {
            if (tempInstanceStore != null && tempInstanceStore.ContainsInstancesFor(type))
            {
                return(tempInstanceStore.GetInstances(type).Cast <Tuple <Registration, object> >().First().Item2);
            }

            var registrations = this.GetRegistrationsFor(type, null).ToList();

            if (registrations.Count > 1)
            {
                throw new ContainerException("Cannot return single instance for type `" + type.AssemblyQualifiedName + "`, There are multiple instances stored.", buildStack);
            }

            if (registrations.Count == 1)
            {
                return(this.GetOrCreateInstances(type, registrations[0].Lifecycle, tempInstanceStore, buildStack).First());
            }

            var typesToCreate = this.GetTypesToCreate(type, buildStack);

            return(this.GetInstance(typesToCreate.First(), tempInstanceStore, buildStack));
        }
Exemple #3
0
        object Resolve(Type type, IInstanceStore tempInstanceStore, Stack<Type> buildStack)
        {
            if (tempInstanceStore != null && tempInstanceStore.ContainsInstancesFor(type))
                return tempInstanceStore.GetInstances(type).Cast<Tuple<Registration, object>>().First().Item2;

            lock (this.mutex)
            {
                // dont pass tempinstancestore in here, the If above handles it
                var registrations = this.GetRegistrationsFor(type, tempInstanceStore).ToList();

                if(registrations.Count > 1)
                    throw new ContainerException("Cannot return single instance for type `" + type.AssemblyQualifiedName + "`, There are multiple instances stored.", buildStack);

                if (registrations.Count == 1)
                    return this.GetOrCreateInstances(type, registrations[0].Lifecycle, tempInstanceStore, buildStack).First();

                var typesToCreate = GetTypesToCreate(type, buildStack);
                return this.GetInstance(typesToCreate.First(), tempInstanceStore, buildStack);
            }
        }
Exemple #4
0
        /// <summary>
        /// Trys to get an instance from the instance store, creating it if it doesnt exist
        /// </summary>
        /// <param name="requestType">The requested type</param>
        /// <param name="instanceStore"></param>
        /// <param name="tempInstanceStore"></param>
        /// <param name="buildStack"></param>
        /// <returns></returns>
        IEnumerable GetOrCreateInstances(Type requestType, IInstanceStore instanceStore, IInstanceStore tempInstanceStore, Stack<Type> buildStack)
        {
            var typesToCreate = GetTypesToCreate(requestType, buildStack);

            var instances = new List<Tuple<Registration, object>>();

            if (tempInstanceStore != null && tempInstanceStore.ContainsInstancesFor(requestType))
                instances.AddRange(tempInstanceStore.GetInstances(requestType).Cast<Tuple<Registration, object>>());
            else if (instanceStore.ContainsInstancesFor(requestType))
                instances.AddRange(instanceStore.GetInstances(requestType).Cast<Tuple<Registration, object>>());

            foreach (var registration in typesToCreate)
            {
                if(!instances.Any(i => i != null && i.Item1 == registration))
                {
                    var newinstance = this.GetInstance(registration, tempInstanceStore, new Stack<Type>(buildStack.Reverse()));

                    instanceStore.Insert(registration, requestType, newinstance);

                    instances.Add(new Tuple<Registration, object>(registration, newinstance));
                }
            }

            return instances.Select(i => i.Item2).ToArray();
        }