Пример #1
0
        /// <summary>
        /// Add the user and any provided children to the database.
        /// </summary>
        /// <param name="item">The user.</param>
        /// <exception cref="ArgumentNullException"><paramref name="item"/> is null</exception>
        public virtual void Add(TAccount item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            var userColumns = _utilities.GetColumnIdentifiers <TAccount>();
            var userParams  = _utilities.GetColumnParameters <TAccount>();

            using (var trx = new AutoDbTransaction(Connection)) {
                var sql = $"insert into {QSchema}.{Q(UserAccountTable)} ({userColumns}) values ({userParams}); select SCOPE_IDENTITY() as id;";

                int key = -1;
                using (var multi = Connection.QueryMultiple(sql, item, trx.Trx)) {
                    key = multi.ReadSingle <int>();
                }

                if (key <= 0)
                {
                    throw new Exception($"Received invalid identity key for new User: {key}");
                }

                var childrenProps = _utilities.GetChildCollectionProperties <TAccount>();
                foreach (var prop in childrenProps)
                {
                    var value = prop.GetValue(item);
                    AddChildren(value as ICollection, key, trx.Trx);
                }

                trx.Commit();
            }
        }
Пример #2
0
        /// <summary>
        /// Removes the provided user from the database, along with all of its children.
        /// </summary>
        /// <param name="item">The user to remove.</param>
        /// <exception cref="ArgumentNullException"><paramref name="item"/> is null</exception>
        public virtual void Remove(TAccount item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            using (var trx = new AutoDbTransaction(Connection)) {
                var sql =
                    $@"{DeleteChildren()}
delete from {QSchema}.{Q(UserAccountTable)} where {Q("Key")} = @key;";
                Connection.Execute(sql, new { key = item.Key }, trx.Trx);

                trx.Commit();
            }
        }
Пример #3
0
        /// <summary>
        /// Updates the provided user in the database. Any children in the database that no longer
        /// exist in the user's child collections will be deleted. Any previously existing children,
        /// will be updated, and any new children will be added.
        /// </summary>
        /// <param name="item">The user to update.</param>
        /// <exception cref="ArgumentNullException"><paramref name="item"/> is null</exception>
        public virtual void Update(TAccount item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            var columnAssign = _utilities.GetColumnAssignment <TAccount>();

            using (var trx = new AutoDbTransaction(Connection)) {
                var sql = $"update {QSchema}.{Q(UserAccountTable)} set {columnAssign} where {Q("Key")} = @key;";
                Connection.Execute(sql, item, trx.Trx);

                var childrenProps = _utilities.GetChildCollectionProperties <TAccount>();
                foreach (var prop in childrenProps)
                {
                    var value = prop.GetValue(item);
                    UpdateChildren(value as ICollection, item.Key, trx.Trx);
                }

                trx.Commit();
            }
        }