Exemplo n.º 1
0
        /// <summary>
        /// Gets the value associated with the specified key.
        /// </summary>
        /// <param name="key">The key whose value to get.</param>
        /// <param name="value">
        /// When this method returns, the value associated with the specified key, if the key is found;
        /// otherwise, the default value for the type of the value parameter.
        /// This parameter is passed uninitialised.</param>
        /// <returns>Returns true if an entry with the specified key exists; otherwise, false.</returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="key"/> is null.</exception>
        public Boolean TryGetValue(String key, out String value)
        {
            // key can't be null
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            using (EtilicContext context = new EtilicContext())
            {
                // try to find the entry with the specified key
                BundleConfigEntry entry = context.BundleConfiguration.SingleOrDefault(
                    x => x.BundleID.Equals(this.bundleID) && x.Key.Equals(key));

                // return the default String value/false if it doesn't exist
                if (entry == null)
                {
                    value = default(String);
                    return(false);
                }

                // return the entry's value if it does
                value = entry.Value;

                // indicate success
                return(true);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Determines whether the current bundle's configuration contains
        /// an entry with matching key and value.
        /// </summary>
        /// <param name="item">The entry to find.</param>
        /// <returns>Returns true if the entry exists; otherwise, false.</returns>
        public Boolean Contains(KeyValuePair <String, String> item)
        {
            using (EtilicContext context = new EtilicContext())
            {
                BundleConfigEntry entry = context.BundleConfiguration.SingleOrDefault(
                    x => x.BundleID.Equals(this.bundleID) && x.Key.Equals(item.Key) && x.Value.Equals(item.Value));

                return(entry != null);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(String key, String value)
        {
            // the key cannot be null
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            // initialise a database connection
            using (EtilicContext context = new EtilicContext())
            {
                // initialise a transaction to avoid a race condition which would allow
                // a configuration entry with the specified key to be added between
                // testing whether an entry for the specified key exists and adding
                // the new entry
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        // test whether the database contains an entry for this key
                        if (context.BundleConfiguration.Any(x => x.BundleID.Equals(this.bundleID) && x.Key.Equals(key)))
                        {
                            throw new ArgumentException("The specified key already exists.");
                        }

                        // initialise a new entry for this key, with the specified initial value
                        BundleConfigEntry entry = new BundleConfigEntry();
                        entry.BundleID = this.bundleID;
                        entry.Key      = key;
                        entry.Value    = value;

                        // add the key to the local collection
                        context.BundleConfiguration.Add(entry);

                        // save changes to the database and commit all changes
                        context.SaveChanges();
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        // roll back the transaction
                        transaction.Rollback();

                        // throw an exception
                        throw new EtilicException(
                                  @"Unable to add key to the bundle's configuration because the database transaction failed. See the inner exception for more details.", ex);
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Removes the entry with the specified key from the current bundle's configuration.
        /// </summary>
        /// <param name="key">The key of the entry to remove.</param>
        /// <returns>Returns true if the entry is successfully removed; otherwise, false.</returns>
        public Boolean Remove(String key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            using (EtilicContext context = new EtilicContext())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        // try to find the entry with the specified key
                        BundleConfigEntry entry = context.BundleConfiguration.SingleOrDefault(
                            x => x.BundleID.Equals(this.bundleID) && x.Key.Equals(key));

                        // return false if it doesn't exist
                        if (entry == null)
                        {
                            return(false);
                        }

                        // remove the entry from the local set
                        context.BundleConfiguration.Remove(entry);

                        // save all changes
                        context.SaveChanges();
                        transaction.Commit();

                        // the entry has successfully been removed
                        return(true);
                    }
                    catch (Exception)
                    {
                        // roll-back all changes
                        transaction.Rollback();

                        // indicate failure (the IDictionary specification doesn't want us to throw an exception)
                        return(false);
                    }
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets or sets the value of the entry with the specified key.
        /// </summary>
        /// <param name="key">The key of the entry whose value should be looked up.</param>
        /// <returns>Returns the value of the entry with the specified key.</returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="key"/> is null.</exception>
        /// <exception cref="System.Collections.Generic.KeyNotFoundException">No entry whose key is <paramref name="key"/> exists.</exception>
        public String this[String key]
        {
            get
            {
                // key can't be null
                if (key == null)
                {
                    throw new ArgumentNullException("key");
                }

                using (EtilicContext context = new EtilicContext())
                {
                    // try to get a configuration entry with the specified key
                    BundleConfigEntry entry = context.BundleConfiguration.SingleOrDefault(
                        x => x.BundleID.Equals(this.bundleID) && x.Key.Equals(key));

                    // throw an exception if it does not exist
                    if (entry == null)
                    {
                        throw new KeyNotFoundException();
                    }

                    // otherwise return the value
                    return(entry.Value);
                }
            }
            set
            {
                // key can't be null
                if (key == null)
                {
                    throw new ArgumentNullException("key");
                }

                using (EtilicContext context = new EtilicContext())
                {
                    using (var transaction = context.Database.BeginTransaction())
                    {
                        try
                        {
                            // try to get a configuration entry with the specified key
                            BundleConfigEntry entry = context.BundleConfiguration.SingleOrDefault(
                                x => x.BundleID.Equals(this.bundleID) && x.Key.Equals(key));

                            // create it if it doesn't exist
                            if (entry == null)
                            {
                                entry          = new BundleConfigEntry();
                                entry.BundleID = this.bundleID;
                                entry.Key      = key;

                                // add the entry to the local set
                                context.BundleConfiguration.Add(entry);
                            }

                            // update the entry
                            entry.Value = value;

                            // save all changes
                            context.SaveChanges();
                            transaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            // roll-back all changes
                            transaction.Rollback();

                            // re-throw the exception
                            throw ex;
                        }
                    }
                }
            }
        }