/// <summary> /// Initializes a new instance of the <see cref="UnitOfWork" /> class. /// </summary> /// <param name="repositoryResolveName">The repository resolve name.</param> /// <param name="optimisticConcurrencyStrategy">The optimistic concurrency strategy.</param> /// <param name="repositoryFactory">The repository factory.</param> /// <param name="createTransactionScope">if set to <see langword="true" /> the <see cref="WorkAction" /> and <see cref="WorkFunc" /> will create a transaction scope for the unit of work.</param> /// <param name="transactionScopeFactory">The transaction scope factory if <see langword="null" /> a default factory will be used.</param> public UnitOfWork( string repositoryResolveName = null, OptimisticConcurrencyStrategy optimisticConcurrencyStrategy = OptimisticConcurrencyStrategy.None, Func <OptimisticConcurrencyStrategy, string, IRepository> repositoryFactory = null, bool createTransactionScope = false, Func <TransactionScope> transactionScopeFactory = null) { RepositoryResolveName = repositoryResolveName; OptimisticConcurrencyStrategy = optimisticConcurrencyStrategy; _repositoryFactory = repositoryFactory ?? DefaultRepositoryFactory; CreateTransactionScope = createTransactionScope; _transactionScopeFactory = transactionScopeFactory ?? DefaultTransactionScopeFactory; }
static void Main(string[] args) { try { var sw = new Stopwatch(); OptimisticConcurrencyStrategy = args.Any(a => a.StartsWith("cl")) ? OptimisticConcurrencyStrategy.ClientWins : args.Any(a => a.StartsWith("st")) ? OptimisticConcurrencyStrategy.StoreWins : OptimisticConcurrencyStrategy.None; Register(); StartServices(); Initialize(); sw.Start(); if (args.Any(a => a.StartsWith("a"))) { RunAsync().Wait(); } else { RunSync(); } sw.Stop(); Console.WriteLine($@"Test duration: {sw.Elapsed:d\.hh\.mm\.ss\.fffffff}"); } catch (Exception x) { Debug.WriteLine(x.DumpString()); Console.WriteLine(x.DumpString()); } Console.Write("Press any key to finish..."); Console.ReadKey(true); }
/// <summary> /// Initializes a new instance of the <see cref="RetryUnitOfWork{T}" /> class. /// </summary> /// <param name="work">The delegate implementing the actual unit of work to be invoked between 1 and <c>maxRetries</c> in the method <see cref="Retry{T}.Start(int,int,int)" />.</param> /// <param name="isFailure">A delegate testing if the unit of work has failed. Can be <see langword="null" /> in which case a default implementation will be invoked: /// <code><![CDATA[(r, x, i) => x != null && !(x is RepeatableOperationException) && !x.IsTransient()]]></code></param> /// <param name="isSuccess">A delegate testing if the unit of work has succeeded. Can be <see langword="null" /> in which case a default implementation will be invoked /// <code><![CDATA[x == null]]></code></param> /// <param name="epilogue">A delegate invoked after the unit of work has been tried unsuccessfully <c>maxRetries</c>. Can be <see langword="null" /> in which case a default implementation will be invoked. /// <code><![CDATA[(r, x, i) => { if (x != null) throw x; else return r; }]]></code></param> /// <param name="repositoryResolveName">The resolve name of the repository.</param> /// <param name="optimisticConcurrencyStrategy">The optimistic concurrency strategy for the repository.</param> /// <param name="repositoryFactory">The repository factory.</param> /// <param name="createTransactionScope">if set to <see langword="true" /> [create transaction scope].</param> /// <param name="transactionScopeFactory">The transaction scope factory.</param> public RetryUnitOfWork( Func <IRepository, int, T> work, Func <T, Exception, int, bool> isFailure = null, Func <T, Exception, int, bool> isSuccess = null, Func <T, Exception, int, T> epilogue = null, string repositoryResolveName = null, OptimisticConcurrencyStrategy optimisticConcurrencyStrategy = OptimisticConcurrencyStrategy.None, Func <OptimisticConcurrencyStrategy, string, IRepository> repositoryFactory = null, bool createTransactionScope = false, Func <TransactionScope> transactionScopeFactory = null) : base( i => new UnitOfWork( repositoryResolveName, optimisticConcurrencyStrategy, repositoryFactory, createTransactionScope, transactionScopeFactory) .WorkFunc(r => work(r, i)), isFailure: isFailure ?? RetryUnitOfWorkConstants.IsFailure, isSuccess: isSuccess ?? RetryDefaults.IsSuccessResult, epilogue: epilogue ?? RetryDefaults.Epilogue) { }
/// <summary> /// Initializes a new instance of the <see cref="RetryUnitOfWorkTasks{T}"/> class. /// </summary> /// <param name="work">The work.</param> /// <param name="isFailure">A delegate testing if the unit of work has failed. Can be <see langword="null" /> in which case a default implementation will be invoked: /// <code><![CDATA[(r, x, i) => Task.FromResult(x != null && !(x is RepeatableOperationException) && !x.IsTransient())]]></code> /// </param> /// <param name="isSuccess">A delegate testing if the unit of work has succeeded. Can be <see langword="null" /> in which case a default implementation will be invoked /// <code> /// <![CDATA[(r, x, i) => Task.FromResult(x == null)]]> /// </code></param> /// <param name="epilogue">A delegate invoked after the unit of work has been tried unsuccessfully <c>maxRetries</c>. Can be <see langword="null" /> in which case a default implementation will be invoked. /// <code> /// <![CDATA[(r, x, i) => { if (x != null) throw x; else return Task.FromResult(r); })]]> /// </code></param> /// <param name="repositoryResolveName">The resolve name of the repository.</param> /// <param name="optimisticConcurrencyStrategy">The optimistic concurrency strategy for the repository.</param> /// <param name="repositoryFactory">The repository factory.</param> /// <param name="createTransactionScope">if set to <see langword="true" /> creates a transaction scope.</param> /// <param name="transactionScopeFactory">The transaction scope factory.</param> public RetryUnitOfWorkTasks( Func <IRepository, int, Task <T> > work, Func <T, Exception, int, Task <bool> > isFailure = null, Func <T, Exception, int, Task <bool> > isSuccess = null, Func <T, Exception, int, Task <T> > epilogue = null, string repositoryResolveName = null, // ClientWins probably doesn't make a lot of sense here OptimisticConcurrencyStrategy optimisticConcurrencyStrategy = OptimisticConcurrencyStrategy.None, Func <OptimisticConcurrencyStrategy, string, IRepository> repositoryFactory = null, bool createTransactionScope = false, Func <TransactionScope> transactionScopeFactory = null) : base( async i => await new UnitOfWork( repositoryResolveName, optimisticConcurrencyStrategy, repositoryFactory, createTransactionScope, transactionScopeFactory) .WorkFuncAsync(async r => await work(r, i)), isFailure ?? RetryUnitOfWorkConstants.IsFailureAsync, isSuccess ?? RetryDefaults.IsSuccessResultAsync, epilogue ?? RetryDefaults.EpilogueAsync) { }