Created
January 22, 2014 15:36
-
-
Save dlidstrom/8560862 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class JobBase : IInterruptableJob | |
| { | |
| protected JobBase() | |
| { | |
| Log = LogManager.GetLogger(GetType()); | |
| } | |
| public IKernel Kernel { get; set; } | |
| protected ILog Log { get; private set; } | |
| public void Interrupt() | |
| { | |
| Log.Info("Interrupting job"); | |
| DoInterrupt(); | |
| } | |
| public void Execute(IJobExecutionContext context) | |
| { | |
| Log.Debug("Executing job"); | |
| var stopwatch = Stopwatch.StartNew(); | |
| DoExecute(context); | |
| stopwatch.Stop(); | |
| Log.Debug(string.Format("Done, job took {0}", stopwatch.Elapsed.ToString("c"))); | |
| } | |
| protected abstract void DoInterrupt(); | |
| protected abstract void DoExecute(IJobExecutionContext context); | |
| [DebuggerStepThrough] | |
| protected TResult Transact<TResult>(Func<Database, TResult> action) | |
| { | |
| using (Kernel.BeginScope()) | |
| { | |
| IQueryExecutor queryExecutor = null; | |
| ICommandExecutor commandExecutor = null; | |
| IServiceBusMessages serviceBusMessages = null; | |
| IServiceBus serviceBus = null; | |
| try | |
| { | |
| queryExecutor = Kernel.Resolve<IQueryExecutor>(); | |
| commandExecutor = Kernel.Resolve<ICommandExecutor>(); | |
| serviceBusMessages = Kernel.Resolve<IServiceBusMessages>(); | |
| serviceBus = Kernel.Resolve<IServiceBus>(); | |
| var result = action.Invoke(new Database(queryExecutor, commandExecutor, serviceBusMessages)); | |
| serviceBus.Send(serviceBusMessages); | |
| commandExecutor.SaveChanges(); | |
| return result; | |
| } | |
| finally | |
| { | |
| if (queryExecutor != null) Kernel.ReleaseComponent(queryExecutor); | |
| if (commandExecutor != null) Kernel.ReleaseComponent(commandExecutor); | |
| if (serviceBusMessages != null) Kernel.ReleaseComponent(serviceBusMessages); | |
| if (serviceBus != null) Kernel.ReleaseComponent(serviceBus); | |
| } | |
| } | |
| } | |
| [DebuggerStepThrough] | |
| protected void Transact(Action<Database> action) | |
| { | |
| Transact(db => | |
| { | |
| action.Invoke(db); | |
| return false; | |
| }); | |
| } | |
| protected class Database | |
| { | |
| private readonly IQueryExecutor queryExecutor; | |
| private readonly ICommandExecutor commandExecutor; | |
| private readonly IServiceBusMessages serviceBusMessages; | |
| [DebuggerStepThrough] | |
| public Database(IQueryExecutor queryExecutor, ICommandExecutor commandExecutor, IServiceBusMessages serviceBusMessages) | |
| { | |
| if (queryExecutor == null) throw new ArgumentNullException("queryExecutor"); | |
| if (commandExecutor == null) throw new ArgumentNullException("commandExecutor"); | |
| if (serviceBusMessages == null) throw new ArgumentNullException("serviceBusMessages"); | |
| this.queryExecutor = queryExecutor; | |
| this.commandExecutor = commandExecutor; | |
| this.serviceBusMessages = serviceBusMessages; | |
| } | |
| [DebuggerStepThrough] | |
| public TResult ExecuteQuery<TResult>(IQuery<TResult> query) | |
| { | |
| if (query == null) throw new ArgumentNullException("query"); | |
| return queryExecutor.ExecuteQuery(query); | |
| } | |
| [DebuggerStepThrough] | |
| public void ExecuteCommand(IPrincipal principal, CommandBase command) | |
| { | |
| if (principal == null) throw new ArgumentNullException("principal"); | |
| if (command == null) throw new ArgumentNullException("command"); | |
| commandExecutor.ExecuteCommand(principal, command); | |
| } | |
| [DebuggerStepThrough] | |
| public void SendToBus(params object[] messages) | |
| { | |
| if (messages == null) throw new ArgumentNullException("messages"); | |
| serviceBusMessages.Add(messages); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment