Exemple #1
0
        /// <summary>
        ///     Initializes a new instance of the MikValSor.Immutable.ImmutablCollection`1 class that is a immutable wrapper around the specified list.
        /// </summary>
        /// <param name="list">
        ///     The list to wrap.
        ///</param>
        ///<exception cref="ArgumentNullException">
        ///		List is null.
        /// </exception>
        ///<exception cref="ArgumentException">
        ///		List contains mutable element.
        /// </exception>
        public ImmutableCollection(IList <T> list) : base()
        {
            if (list == null)
            {
                throw new ArgumentNullException(nameof(list));
            }

            m_array = Enumerable.ToArray(list);

            var validator = new ImmutableValidator();

            if (!validator.IsImmutable(typeof(T)))
            {
                for (var i = 0; i < m_array.Length; i++)
                {
                    try
                    {
                        validator.EnsureImmutable(m_array[i]);
                    }
                    catch (NotImmutableException e)
                    {
                        throw new ArgumentException($"List element at index {i}, was not immutable.", nameof(list), e);
                    }
                }
            }
        }
Exemple #2
0
        public void ImmutableCollection_Immutable_object_charArray()
        {
            //Arrange
            var    validator = new MikValSor.Immutable.ImmutableValidator();
            object target    = new ImmutableCollection <char[]>(new char[0][]);

            //Act
            var actual = validator.IsImmutable(target);

            //Assert
            Assert.IsFalse(actual);
        }
Exemple #3
0
        public void ImmutableCollection_Immutable_Type_charArray()
        {
            //Arrange
            var validator = new MikValSor.Immutable.ImmutableValidator();

            System.Type target = typeof(ImmutableCollection <char[]>);

            //Act
            var actual = validator.IsImmutable(target);

            //Assert
            Assert.IsFalse(actual);
        }
Exemple #4
0
        /// <summary>Initializes a new instance of the <see cref="Store"/> class.</summary>
        /// <param name="storages">Storages what values will be presisted to.</param>
        /// <param name="immutableValidator"><see cref="ImmutableValidator"/> used to validate immutablity of values.</param>
        /// <param name="serializableValidator"><see cref="SerializableValidator"/> used to validate serializability of values.</param>
        /// <param name="cache"><see cref="IStoreResultCache"/> used by store.</param>
        /// <param name="formatter"><see cref="IFormatProvider"/> used to serialize and deserialize values.</param>
        /// <exception cref="ArgumentNullException">Throws <see cref="ArgumentNullException"/> is storages is null.</exception>
        /// <exception cref="ArgumentException">Throws <see cref="ArgumentException"/> if storage is empty or has null values.</exception>
        public Store(
            IEnumerable <IStorage> storages,
            ImmutableValidator immutableValidator       = null,
            SerializableValidator serializableValidator = null,
            IStoreResultCache cache = null,
            IFormatter formatter    = null)
        {
            if (storages == null)
            {
                throw new ArgumentNullException(nameof(storages));
            }

            this.storages = storages.ToArray();
            if (this.storages.Length == 0)
            {
                throw new ArgumentException("Did not contain any values.", nameof(storages));
            }

            if (this.storages.Any(s => s == null))
            {
                throw new ArgumentException("Contained a null value.", nameof(storages));
            }

            if (immutableValidator == null)
            {
                immutableValidator = new ImmutableValidator();
            }

            this.immutableValidator = immutableValidator;

            if (serializableValidator == null)
            {
                serializableValidator = new SerializableValidator();
            }

            this.serializableValidator = serializableValidator;

            if (cache == null)
            {
                cache = new StoreResultCache();
            }

            this.cache = cache;

            if (formatter == null)
            {
                formatter = new BinaryFormatter();
            }

            this.formatter = formatter;
        }
Exemple #5
0
        /// <summary>Initializes a new instance of the <see cref="Store"/> class.</summary>
        /// <param name="storage">Storage what values will be presisted to.</param>
        /// <param name="immutableValidator"><see cref="ImmutableValidator"/> used to validate immutablity of values.</param>
        /// <param name="serializableValidator"><see cref="SerializableValidator"/> used to validate serializability of values.</param>
        /// <param name="cache"><see cref="IStoreResultCache"/> used by store.</param>
        /// <param name="formatter"><see cref="IFormatProvider"/> used to serialize and deserialize values.</param>
        /// <exception cref="ArgumentNullException">Throws <see cref="ArgumentNullException"/> is storage is null.</exception>
        public Store(
            IStorage storage,
            ImmutableValidator immutableValidator       = null,
            SerializableValidator serializableValidator = null,
            IStoreResultCache cache = null,
            IFormatter formatter    = null)
        {
            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }

            this.storages = new[] { storage };

            if (immutableValidator == null)
            {
                immutableValidator = new ImmutableValidator();
            }

            this.immutableValidator = immutableValidator;

            if (serializableValidator == null)
            {
                serializableValidator = new SerializableValidator();
            }

            this.serializableValidator = serializableValidator;

            if (cache == null)
            {
                cache = new StoreResultCache();
            }

            this.cache = cache;

            if (formatter == null)
            {
                formatter = new BinaryFormatter();
            }

            this.formatter = formatter;
        }