Beispiel #1
0
        private object TypedFactoryCall(MethodInvocation call, FactoryMethodAttribute attrib, IContext context)
        {
            Type factoryType = GetFactoryType(call, attrib);


            //get from context cache
            if (attrib.InstanceMode == InstanceMode.PerContext && context.State.typedPerContextObjects.ContainsKey(factoryType))
            {
                return(context.State.typedPerContextObjects[factoryType]);
            }

            //get from graph cache
            if (attrib.InstanceMode == InstanceMode.PerGraph && context.State.typedPerGraphObjects.ContainsKey(factoryType))
            {
                return(context.State.typedPerGraphObjects[factoryType]);
            }

            //get from thread cache
            if (attrib.InstanceMode == InstanceMode.PerThread && context.State.typedPerThreadObjects.ContainsKey(factoryType))
            {
                return(context.State.typedPerThreadObjects[factoryType]);
            }

            object res = call.Proceed();

            if (res is IRunnable)
            {
                RunnableEngine.RunRunnable(res as IRunnable);
            }

            //add to context cache
            if (attrib.InstanceMode == InstanceMode.PerContext)
            {
                context.State.typedPerContextObjects.Add(factoryType, res);
            }

            //add to graph cache
            if (attrib.InstanceMode == InstanceMode.PerGraph)
            {
                context.State.typedPerGraphObjects.Add(factoryType, res);
            }

            //add to thread cache
            if (attrib.InstanceMode == InstanceMode.PerThread)
            {
                context.State.typedPerThreadObjects.Add(factoryType, res);
            }

            return(res);
        }
Beispiel #2
0
        private T InternalGetObject <T>(string factoryId)
        {
            lock (syncRoot)
            {
                if (state.NamedObjectFactories.ContainsKey(factoryId))
                {
                    ObjectFactoryInfo config = state.NamedObjectFactories[factoryId];
                    try
                    {
                        //get from context cache
                        if (config.InstanceMode == InstanceMode.PerContext && state.namedPerContextObjects.ContainsKey(factoryId))
                        {
                            return((T)state.namedPerContextObjects[factoryId]);
                        }

                        //get from graph cache
                        if (config.InstanceMode == InstanceMode.PerGraph && state.namedPerGraphObjects.ContainsKey(factoryId))
                        {
                            return((T)state.namedPerGraphObjects[factoryId]);
                        }

                        //get from thread cache
                        if (config.InstanceMode == InstanceMode.PerThread && state.namedPerThreadObjects.ContainsKey(factoryId))
                        {
                            return((T)state.namedPerThreadObjects[factoryId]);
                        }

                        object res = config.FactoryDelegate();

                        if (res is IRunnable)
                        {
                            RunnableEngine.RunRunnable(res as IRunnable);
                        }

                        //add to context cache
                        if (config.InstanceMode == InstanceMode.PerContext)
                        {
                            state.namedPerContextObjects.Add(factoryId, res);
                        }

                        //add to graph cache
                        if (config.InstanceMode == InstanceMode.PerGraph)
                        {
                            state.namedPerGraphObjects.Add(factoryId, res);
                        }

                        //add to thread cache
                        if (config.InstanceMode == InstanceMode.PerThread)
                        {
                            state.namedPerThreadObjects.Add(factoryId, res);
                        }

                        return((T)res);
                    }
                    finally
                    {
                    }
                }
            }

            throw ExceptionHelper.NamedFactoryNotFoundException(factoryId);
        }