我们已经成功实现了 ServiceStack IService<> 接口并让它与 iPhone 通信,但我们不确定实现异常处理和日志记录的最佳方式。
public class AddCarService : IService<AddCar>
{
public UnitOfWork UnitOfWork { get; set; }
public object Execute(AddCar request)
{
try
{
PersonBo person;
if (UnitOfWork.PersonService.ValidateSession(request.SessionGuid, out person))
{
var car = new CarBo
{
PersonId = person.PersonId,
Name = request.Car.Name,
RegistrationNumber = request.Car.RegistrationNumber,
IsActive = true,
IsDeleted = false
};
UnitOfWork.CarService.Add(car);
var cars = UnitOfWork.CarService.GetCarByPersonId(person.PersonId);
var mobileCars = cars.Select(carBo => new Car { CarId = carBo.CarId, Name = carBo.Name, RegistrationNumber = carBo.RegistrationNumber }).ToList();
return new GetCarResponse { Cars = mobileCars, Status = ResponseStatus.Ok };
}
return new GetCarResponse { Cars = null, Status = ResponseStatus.Unauthorised };
}
catch (Exception ex)
{
UnitOfWork.Logging.Error("AddCarService", ex);
return new GetCarResponse { Cars = null, Status = ResponseStatus.Exception };
}
}
}
添加 try/catch 会降低性能,因此我们想知道 ServiceStack 框架中是否内置了一个函数来支持我们试图实现的目标,并且仍然向 iPhone 返回格式良好的响应。