这是Project Silk中DDD的参考用法。
该项目使用DDD,并实施了处理程序和服务的概念。 这两种类型的区别和用例究竟是什么?
例如,有一个服务UserServices
用于创建用户。
但车辆的创造是在Handler中完成的。
想知道这个决定背后的原因。
域\ UserServices
public class UserServices : IUserServices
{
private readonly IUserRepository userRepository;
public UserServices(IUserRepository userRepository)
{
if (userRepository == null)
throw new ArgumentNullException("userRepository");
this.userRepository = userRepository;
}
public User CreateUser(User newUser)
{
if (newUser == null) throw new ArgumentNullException("newUser");
try
{
Model.User userToAdd = ToDataModelUser(newUser);
this.userRepository.Create(userToAdd);
return ToServiceUser(userToAdd);
}
catch (InvalidOperationException ex)
{
throw new BusinessServicesException
(Resources.UnableToCreateUserExceptionMessage, ex);
}
}
}
域\处理程序\ CreateVehicle.cs
public class CreateVehicle
{
private readonly IVehicleRepository _vehicleRepository;
private readonly IVehiclePhotoRepository _photoRepository;
public CreateVehicle(IVehicleRepository vehicleRepository,
IVehiclePhotoRepository photoRepository)
{
_vehicleRepository = vehicleRepository;
_photoRepository = photoRepository;
}
public virtual void Execute(int userId,
ICreateVehicleCommand vehicleForm,
HttpPostedFileBase photoFile)
{
if (vehicleForm == null) throw
new ArgumentNullException("vehicleForm");
try
{
var vehicle = vehicleForm.ConvertToEntity(userId);
_vehicleRepository.Create(userId, vehicle);
if (photoFile == null) return;
// the double reference between vehicle and photo
//is a potential source of pain
var photo = photoFile.ConvertToEntity();
_photoRepository.Create(vehicle.VehicleId, photo);
vehicle.PhotoId = photo.VehiclePhotoId;
_vehicleRepository.Update(vehicle);
}
catch (InvalidOperationException ex)
{
throw new BusinessServicesException
(Resources.UnableToCreateVehicleExceptionMessage, ex);
}
}
}
答案 0 :(得分:0)
在您的示例中,我认为区别在于用户的创建是用户的主动操作,而viecle的创建是被动的。有人提交表单来创建用户,另一方面,它会在某些操作完成后触发创建viecle(可能是在创建用户之后?)。
也许您会对此answer感兴趣。