Skip to content

Instantly share code, notes, and snippets.

@agross
Created July 24, 2009 10:02
Show Gist options
  • Save agross/153951 to your computer and use it in GitHub Desktop.
Save agross/153951 to your computer and use it in GitHub Desktop.
using System;
using Castle.Core;
using Indigo.ApplicationLayer.Commands;
using Indigo.ApplicationLayer.Messages;
using Indigo.ApplicationLayer.Properties;
using Indigo.BackEnd;
using Indigo.Domain.Model.Employees;
using Indigo.Domain.Queries.Employees;
using Indigo.Infrastructure.Persistence;
using Rhino.Commons;
namespace Indigo.ApplicationLayer.Services
{
public class EmployeeCreationService
: MessageProcessor,
IStartable,
Consumes<SaveEmployeeCommand>.Async
{
readonly IQueryableRepository<Employee> _employeeRepository;
public EmployeeCreationService(IBus bus,
IQueryableRepository<Employee> employeeRepository) : base(bus)
{
_employeeRepository = employeeRepository;
}
#region Async Members
public void Consume(SaveEmployeeCommand message)
{
using (UnitOfWork.Start())
{
With.Transaction(() =>
{
SaveEmployeeResult reply = null;
try
{
var employee = _employeeRepository.Query(new EmployeeByNumber(message.EmployeeNumber));
if (employee != null && employee.Id != message.EmployeeId)
{
reply = SaveEmployeeResult.Failure(message.CorrelationId, Resources.DuplicateEmployee);
return;
}
if (message.EmployeeId == 0)
{
var number = new EmployeeNumber(message.EmployeeNumber);
var costCenter = new EmployeeCostCenter(message.CostCenter);
var name = new EmployeeName(message.FirstName, message.LastName);
employee = new Employee(number, costCenter, name);
}
else
{
employee = _employeeRepository.Query(new EmployeeById(message.EmployeeId));
employee.Rename(new EmployeeName(message.FirstName, message.LastName));
employee.SetCostCenter(new EmployeeCostCenter(message.CostCenter));
employee.SetEmployeeNumber(new EmployeeNumber(message.EmployeeNumber));
}
_employeeRepository.SaveOrUpdate(employee);
reply = SaveEmployeeResult.Success(message.CorrelationId, employee.Id);
}
catch (Exception ex)
{
reply = SaveEmployeeResult.Failure(message.CorrelationId, ex.Message);
}
finally
{
Bus.Publish(reply);
}
});
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment