<commit_before>﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace SimplePersistence.UoW.Helper
{
    /// <summary>
    /// Extension classes for the Unit of Work pattern
    /// </summary>
    public static class UnitOfWorkExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uow"></param>
        /// <param name="toExecute"></param>
        /// <param name="ct"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Task<T> ExecuteAndCommitAsync<T>(this IUnitOfWork uow, Func<Task<T>> toExecute, CancellationToken ct)
        {
            var tcs = new TaskCompletionSource<T>();
            uow.BeginAsync(ct).ContinueWith(t01 =>
            {
                if (t01.IsFaulted)
                {
                    tcs.SetException(t01.Exception);
                }
                else if (t01.IsCompleted)
                {
                    toExecute().ContinueWith(t02 =>
                    {
                        if (t02.IsFaulted)
                        {
                            tcs.SetException(t02.Exception);
                        }
                        else if (t02.IsCompleted)
                        {
                            uow.CommitAsync(ct).ContinueWith(t03 =>
                            {
                                if (t03.IsFaulted)
                                {
                                    tcs.SetException(t03.Exception);
                                }
                                else if (t03.IsCompleted)
                                {
                                    tcs.SetResult(t02.Result);
                                }
                                else
                                {
                                    tcs.SetCanceled();
                                }
                            }, ct);
                        }
                        else
                        {
                            tcs.SetCanceled();
                        }
                    }, ct);
                }
                else
                {
                    tcs.SetCanceled();
                }
            }, ct);

            return tcs.Task;
        }
    }
}<commit_msg>New extension methods for the UoW<commit_after>﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace SimplePersistence.UoW.Helper
{
    /// <summary>
    /// Extension classes for the Unit of Work pattern
    /// </summary>
    public static class UnitOfWorkExtensions
    {
        #region ExecuteAndCommitAsync

        /// <summary>
        /// Executes the given asynchronous function inside an <see cref="IUnitOfWork.BeginAsync"/> 
        /// and <see cref="IUnitOfWork.CommitAsync"/> scope
        /// </summary>
        /// <param name="uow">The <see cref="IUnitOfWork"/> to be used</param>
        /// <param name="toExecute">The code to be executed inside the scope</param>
        /// <typeparam name="T">The return type</typeparam>
        /// <returns>A task to be awaited</returns>
        /// <exception cref="ArgumentNullException"/>
        public static Task<T> ExecuteAndCommitAsync<T>(this IUnitOfWork uow, Func<Task<T>> toExecute)
        {
            return uow.ExecuteAndCommitAsync(toExecute, CancellationToken.None);
        }

        /// <summary>
        /// Executes the given asynchronous function inside an <see cref="IUnitOfWork.BeginAsync"/> 
        /// and <see cref="IUnitOfWork.CommitAsync"/> scope
        /// </summary>
        /// <param name="uow">The <see cref="IUnitOfWork"/> to be used</param>
        /// <param name="toExecute">The code to be executed inside the scope</param>
        /// <param name="ct">The cancellation token</param>
        /// <typeparam name="T">The return type</typeparam>
        /// <returns>A task to be awaited</returns>
        /// <exception cref="ArgumentNullException"/>
        public static Task<T> ExecuteAndCommitAsync<T>(this IUnitOfWork uow, Func<Task<T>> toExecute, CancellationToken ct)
        {
            return uow.ExecuteAndCommitAsync((w, c) => toExecute(), ct);
        }

        /// <summary>
        /// Executes the given asynchronous function inside an <see cref="IUnitOfWork.BeginAsync"/> 
        /// and <see cref="IUnitOfWork.CommitAsync"/> scope
        /// </summary>
        /// <param name="uow">The <see cref="IUnitOfWork"/> to be used</param>
        /// <param name="toExecute">The code to be executed inside the scope</param>
        /// <typeparam name="T">The return type</typeparam>
        /// <returns>A task to be awaited</returns>
        /// <exception cref="ArgumentNullException"/>
        public static Task<T> ExecuteAndCommitAsync<T>(this IUnitOfWork uow, Func<IUnitOfWork, CancellationToken, Task<T>> toExecute)
        {
            return uow.ExecuteAndCommitAsync(toExecute, CancellationToken.None);
        }

        /// <summary>
        /// Executes the given asynchronous function inside an <see cref="IUnitOfWork.BeginAsync"/> 
        /// and <see cref="IUnitOfWork.CommitAsync"/> scope
        /// </summary>
        /// <param name="uow">The <see cref="IUnitOfWork"/> to be used</param>
        /// <param name="toExecute">The code to be executed inside the scope</param>
        /// <param name="ct">The cancellation token</param>
        /// <typeparam name="T">The return type</typeparam>
        /// <returns>A task to be awaited</returns>
        /// <exception cref="ArgumentNullException"/>
        public static Task<T> ExecuteAndCommitAsync<T>(this IUnitOfWork uow, Func<IUnitOfWork, CancellationToken, Task<T>> toExecute, CancellationToken ct)
        {
            if (uow == null) throw new ArgumentNullException("uow");
            if (toExecute == null) throw new ArgumentNullException("toExecute");

            var tcs = new TaskCompletionSource<T>();
            uow.BeginAsync(ct).ContinueWith(t01 =>
            {
                if (t01.IsFaulted)
                {
                    tcs.SetException(t01.Exception);
                }
                else if (t01.IsCompleted)
                {
                    toExecute(uow, ct).ContinueWith(t02 =>
                    {
                        if (t02.IsFaulted)
                        {
                            tcs.SetException(t02.Exception);
                        }
                        else if (t02.IsCompleted)
                        {
                            uow.CommitAsync(ct).ContinueWith(t03 =>
                            {
                                if (t03.IsFaulted)
                                {
                                    tcs.SetException(t03.Exception);
                                }
                                else if (t03.IsCompleted)
                                {
                                    tcs.SetResult(t02.Result);
                                }
                                else
                                {
                                    tcs.SetCanceled();
                                }
                            }, ct);
                        }
                        else
                        {
                            tcs.SetCanceled();
                        }
                    }, ct);
                }
                else
                {
                    tcs.SetCanceled();
                }
            }, ct);

            return tcs.Task;
        }

        #endregion

        #region ExecuteAndCommit

        /// <summary>
        /// Executes the given lambda inside an <see cref="IUnitOfWork.Begin"/> 
        /// and <see cref="IUnitOfWork.Commit"/> scope
        /// </summary>
        /// <param name="uow">The <see cref="IUnitOfWork"/> to be used</param>
        /// <param name="toExecute">The code to be executed inside the scope</param>
        /// <typeparam name="T">The return type</typeparam>
        /// <returns>A task to be awaited</returns>
        /// <exception cref="ArgumentNullException"/>
        public static T ExecuteAndCommit<T>(this IUnitOfWork uow, Func<T> toExecute)
        {
            return uow.ExecuteAndCommit(u => toExecute());
        }

        /// <summary>
        /// Executes the given lambda inside an <see cref="IUnitOfWork.Begin"/> 
        /// and <see cref="IUnitOfWork.Commit"/> scope
        /// </summary>
        /// <param name="uow">The <see cref="IUnitOfWork"/> to be used</param>
        /// <param name="toExecute">The code to be executed inside the scope</param>
        public static void ExecuteAndCommit(this IUnitOfWork uow, Action toExecute)
        {
            uow.ExecuteAndCommit(u => toExecute());
        }

        /// <summary>
        /// Executes the given lambda inside an <see cref="IUnitOfWork.Begin"/> 
        /// and <see cref="IUnitOfWork.Commit"/> scope
        /// </summary>
        /// <param name="uow">The <see cref="IUnitOfWork"/> to be used</param>
        /// <param name="toExecute">The code to be executed inside the scope</param>
        /// <typeparam name="T">The return type</typeparam>
        /// <returns>A task to be awaited</returns>
        /// <exception cref="ArgumentNullException"/>
        public static T ExecuteAndCommit<T>(this IUnitOfWork uow, Func<IUnitOfWork, T> toExecute)
        {
            if (uow == null) throw new ArgumentNullException("uow");
            if (toExecute == null) throw new ArgumentNullException("toExecute");

            uow.Begin();

            var result = toExecute(uow);

            uow.Commit();
            return result;
        }

        /// <summary>
        /// Executes the given lambda inside an <see cref="IUnitOfWork.Begin"/> 
        /// and <see cref="IUnitOfWork.Commit"/> scope
        /// </summary>
        /// <param name="uow">The <see cref="IUnitOfWork"/> to be used</param>
        /// <param name="toExecute">The code to be executed inside the scope</param>
        public static void ExecuteAndCommit(this IUnitOfWork uow, Action<IUnitOfWork> toExecute)
        {
            if (uow == null) throw new ArgumentNullException("uow");
            if (toExecute == null) throw new ArgumentNullException("toExecute");

            uow.Begin();

            toExecute(uow);

            uow.Commit();
        }

        #endregion
    }
}
{'fraction_non_alphanumeric': 0.11894909055913772, 'fraction_numerical': 0.005004330670772784, 'mean_word_length': 1.3379077615298087, 'pattern_counts': {'":': 0, '<': 152, '<?xml version=': 0, '>': 161, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '983016', 'n_tokens_mistral': 2730, 'n_tokens_neox': 2626, 'n_words': 604}