Beispiel #1
0
        /// <summary>
        /// Inserts the provided object to the cache of instantiated objects.
        /// </summary>
        /// <param name="objectToInsert">The object to insert into the cache.</param>
        public void InsertObjectToCache(InstantiatedObject objectToInsert)
        {
            if (objectToInsert == null)
            {
                throw new ArgumentNullException(nameof(objectToInsert), $"Cannot add a NULL {nameof(InstantiatedObject)} to the {nameof(InstantiatedObjectsRepository)}");
            }

            objectToInsert.Validate();

            // Replace the existing object if multiples not allowed and it exists already
            if (!objectToInsert.AllowMultiple && DoesObjectInterfaceAndClassExist(objectToInsert.InterfaceType, objectToInsert.ClassType))
            {
                InstantiatedObject objectToRemove = ObjectCache.First(obj => obj.InterfaceType == objectToInsert.InterfaceType && obj.ClassType == objectToInsert.ClassType);
                if (objectToRemove == null)
                {
                    throw new NullReferenceException($"The object mapped to \"{objectToInsert.InterfaceType.Name}\" is marked to not allow Multiple, was found in the Repository, but could not be retrieved.");
                }

                ObjectCache.Remove(objectToRemove);
            }

            ObjectCache.Add(objectToInsert);
        }