protected object ExecuteCommand(IUnitOfWorkCommand command, UnitOfWorkInfo info) { var execution = BeginUnitOfWork(info); try { var result = command.Execute(); return(result); } catch (Exception e) { execution.HandleException(e); if (e is TargetInvocationException) { throw e.GetBaseException(); } throw; } finally { execution.End(); } }
/// <summary> /// Run a command in a new Unit Of Work. /// </summary> /// <seealso cref = "IUnitOfWorkCommand" /> /// <seealso cref = "UnitOfWorkInfo" /> /// <param name = "command">Command to execute.</param> /// <param name = "info">Unit of work information.</param> /// <returns>The return value of the Unit of Work.</returns> public void RunUnitOfWorkAsync(IUnitOfWorkCommand command, UnitOfWorkInfo info) { var worker = new BackgroundWorker(); if (IsUnitOfWorkRunning) { worker.DoWork += delegate { command.Execute(); }; } CallBegin(); worker.DoWork += delegate { try { lock (executionPermit) { ExecuteCommand(command, info); } } catch (Exception e) { CallUnhandledException(e); } finally { CallEnd(); } }; worker.RunWorkerAsync(); }
public UnitOfWorkExecution BeginUnitOfWork(UnitOfWorkInfo unitOfWorkInfo) { var execution = new UnitOfWorkExecution(persistenceContext, unitOfWorkInfo); execution.OnEnd(() => CurrentUnitOfWork = null); CurrentUnitOfWork = execution.Begin(); return(execution); }
public object ApplyTo(MethodInvocationInfo invocation) { var uowinfo = UnitOfWorkInfo.From(invocation.Method); if (uowinfo.IsAsync) { ContextProvider.CurrentContext.RunUnitOfWorkAsync(new UnitOfWorkInterceptorCommand(invocation), uowinfo); return(null); } return(ContextProvider.CurrentContext.RunUnitOfWork(new UnitOfWorkInterceptorCommand(invocation), uowinfo)); }
public UnitOfWorkInfo Build() { var attribute = CustomAttributeProvider.Attribute <UnitOfWorkAttribute>(); var name = attribute != null && !string.IsNullOrWhiteSpace(attribute.Name) ? attribute.Name : GetUowName(); var allow = CustomAttributeProvider.Attribute <AllowAsyncAttribute>(); var notAllow = CustomAttributeProvider.Attribute <NotAsyncAttribute>(); var async = Nails.Configuration.DefaultAsyncMode; if (Nails.Configuration.AllowAsyncExecution) { if (!AllowAsync) { //only void methods can be invoked Async automatically. async = false; } else { //negotiate with the default value. if (allow != null) { async = true; } else if (notAllow != null) { async = false; } } } else { async = false; } var transactionMode = attribute != null ? attribute.TransactionMode : Nails.Configuration.DefaultTransactionMode; var uowinfo = new UnitOfWorkInfo(isTransactional: transactionMode != TransactionMode.NoTransaction, isAsync: async); return(uowinfo); }
/// <summary> /// Constructor. /// </summary> public UnitOfWork(UnitOfWorkInfo info, UnitOfWorkEventSubscriptions subscriptions, IPersistenceContext persistenceContext, bool connectionBoundUnitOfWork) { this.connectionBoundUnitOfWork = connectionBoundUnitOfWork; Subscriptions = subscriptions; this.persistenceContext = persistenceContext; transactionContext = new NullTransactionalContext(); this.info = info; try { LoadTransactionContext(); if (connectionBoundUnitOfWork) { persistenceContext.OpenSession(); } } catch (Exception ex) { Log.Error("Error creating a new Unit of Work", ex); throw; } }
/// <summary> /// Run a block of code in a new Unit Of Work /// </summary> /// <param name = "command">delegate to some block of code</param> /// <param name = "info">Unit of work information.</param> public void RunUnitOfWork(Action command, UnitOfWorkInfo info) { RunUnitOfWork(new UnitOfWorkActionCommand(command), info); }
/// <summary> /// Run a block of code in a new Unit Of Work /// </summary> /// <param name = "command">delegate to some block of code</param> /// <param name = "info">Unit of work information.</param> /// <returns>The return value of the Unit of Work.</returns> public TReturn RunUnitOfWork <TReturn>(Func <TReturn> command, UnitOfWorkInfo info) { return((TReturn)RunUnitOfWork(new UnitOfWorkFunctionCommand <TReturn>(command), info)); }
/// <summary> /// Run a command in a new Unit Of Work. /// </summary> /// <seealso cref = "IUnitOfWorkCommand" /> /// <seealso cref = "UnitOfWorkInfo" /> /// <param name = "command">Command to execute.</param> /// <param name = "info">Unit of work information.</param> /// <returns>The return value of the Unit of Work.</returns> public object RunUnitOfWork(IUnitOfWorkCommand command, UnitOfWorkInfo info) { return(IsUnitOfWorkRunning ? command.Execute() : ExecuteCommand(command, info)); }
public UnitOfWorkExecution(IPersistenceContext persistenceContext, UnitOfWorkInfo info) { this.persistenceContext = persistenceContext; this.info = info; }