Exemplo n.º 1
0
        public object Resolve(Type t, object[] parameters = null, IIocContainerFlags flags = IIocContainerFlags.None)
        {
            var    typeofT      = t;
            object newInstance  = null;
            var    stateManager = StateManager.Current;

            if (SurrogatesDirectory.IsSurrogateRegistered(typeofT))
            {
                //Surrogates are created as top level objects
                //however reference tracking is important because surrogates with no references
                //are not persisted
                if (parameters == null || parameters.Length == 0)
                {
                    //We must create a new instance, instance creation mechanism must be located from
                    //the surrogates directory
                    newInstance = SurrogatesDirectory.CreateInstanceFor(typeofT);
                }
                else
                {
                    newInstance = parameters[0];
                }
                var surrogate = stateManager.surrogateManager.GetSurrogateFor(newInstance, generateIfNotFound: true);
                return(newInstance);
            }
            try
            {
                var isRecoveredFromStorage = (flags & IIocContainerFlags.RecoveredFromStorage) == IIocContainerFlags.RecoveredFromStorage;
                if (!isRecoveredFromStorage)
                {
                    if (t.GetCustomAttributes(typeof(Singleton), false).Length > 0)
                    {
                        flags       = flags | IIocContainerFlags.IsSinglenton;
                        newInstance = StateManager.Current.GetObject(UniqueIDGenerator.GetSinglentonUniqueId(t));
                        if ((flags & IIocContainerFlags.SinglentonNonReturnIfExisting) == IIocContainerFlags.SinglentonNonReturnIfExisting)
                        {
                            if (newInstance != null)
                            {
                                return(null);
                            }
                        }
                        if ((flags & IIocContainerFlags.SinglentonReturnIfExisting) == IIocContainerFlags.SinglentonReturnIfExisting)
                        {
                            if (newInstance != null)
                            {
                                return(newInstance);
                            }
                        }
                        //At this point the object was recovered from storage and it the RecoveredFromStorageFlags must
                        //added to avoid unnecessary further processing of this object
                        isRecoveredFromStorage = (newInstance != null);
                    }
                }
                newInstance = newInstance ?? ResolveUnPrepared(t, isRecoveredFromStorage);
                if (newInstance is IViewManager)
                {
                    //Nothing to do
                }
                else if (newInstance is ILogic)
                {
                    InitializeObject(stateManager, this, (ILogic)newInstance, parameters, t, isRecoveredFromStorage, flags);
                }
                else if (newInstance is IStateObject)
                {
                    if (!isRecoveredFromStorage)
                    {
                        InitializeObject(stateManager, this, (IStateObject)newInstance, parameters, t, flags);
                    }
                }
                else
                {
                    if (!alreadyAssertedTypes.Contains(t))
                    {
                        alreadyAssertedTypes.Add(t);
                        Trace.TraceError("IocContainerImplWithUInity::Resolve WARNING: Type to resolved [" + t.FullName + "] is not an IStateObject, so there is no tracking or serialization available for that object");
                    }
                }
            }
            finally
            {
                if (newInstance is IStateObject)
                {
                    stateManager.RemoveIDInResolution(((IStateObject)newInstance).UniqueID);
                }
            }
            return(newInstance);
        }