Exemple #1
0
        public static void WithContext(this IDbContextFactory contextFactory, Action <IDbContext> action)
        {
            if (contextFactory == null)
            {
                throw new ArgumentNullException(nameof(contextFactory));
            }
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var retryPolicy = contextFactory.Configuration?.RetryPolicy;

            if (retryPolicy != null)
            {
                DbRetryer.Retry(retryPolicy, PerformAction);
            }
            else
            {
                PerformAction();
            }

            void PerformAction()
            {
                using (var context = contextFactory.OpenContext())
                {
                    action(context);

                    if (context.CommitState != DbContextCommitState.Rollback)
                    {
                        context.Commit();
                    }
                }
            }
        }
Exemple #2
0
        public static T WithContext <T>(this IDbContextFactory contextFactory, Func <IDbContext, T> action)
        {
            if (contextFactory == null)
            {
                throw new ArgumentNullException(nameof(contextFactory));
            }
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var retryPolicy = contextFactory.Configuration?.RetryPolicy;

            if (retryPolicy != null)
            {
                return(DbRetryer.Retry(retryPolicy, PerformAction));
            }
            else
            {
                return(PerformAction());
            }

            T PerformAction()
            {
                T result;

                using (var context = contextFactory.OpenContext())
                {
                    result = action(context);

                    if (context.CommitState != DbContextCommitState.Rollback)
                    {
                        context.Commit();
                    }
                }

                return(result);
            }
        }