我正在尝试使用最佳实践进行编码,但我对此有疑问。我正在 WebForms 上对此进行测试。
我有一个 UserService 层,其中我有一个将用户传递给 RepositoryLayer 的方法:
public AddUserResponse AddUserResponse(AddUserRequest addUserRequest)
{
AddUserResponse response = new AddUserResponse();
User objUser = new User();
objUser.Names = addUserRequest.Names;
objUser.LastName = addUserRequest.LastName;
objUser.Email = addUserRequest.Email;
objUser.Alias = addUserRequest.Alias;
objUser.Profile.IdProfile = addUserRequest.Profile.IdProfile;
objUser.Password = addUserRequest.Password;
objUser.Active = addUserRequest.Active;
short OperationState=_userRepository.Add(objUser);
if (OperationState==0)
{
response.State=true;
response.Message="User inserted";
}
else if (OperationState==2)
{
response.State=false;
response.Message="Alias or Email already exist. Cannot insert User";
}
else
{
response.State=false;
response.Message="Error in User insertion";
}
return response;
}
然后我有一个 UserRepository 层,其中我有一个添加来自我的服务层的用户的功能:
public short Add(User objUser)
{ ... return OperationState }
如图所示,此函数依靠存储过程调用来插入用户记录。如果用户电子邮件或别名不存在,则插入并返回 0,如果存在则返回 2,如果操作失败则返回 1。
我在一次调用中执行检查和插入以节省数据库往返。
我是否以正确的方式对我的服务和存储库类执行检查?或者如果不是,我应该如何抽象逻辑以让系统确定何时是重复用户?我应该使用模型或服务来放置验证逻辑并在发生这种情况时引发自定义异常吗?
非常感谢您的洞察力。
更新
出于普遍的兴趣,我现在发布如何在我的应用程序上实现这一点,一旦我得到 Jason 的 IoC 解决方案,我也会对此进行更新。
型号类:
using ABC.DEF.Infrastructure.Domain;
namespace ABC.DEF.Model
{
public class AliasOrEmailAreUnique
{
private readonly IRepository<User, int> repository;
public AliasOrEmailAreUnique(IRepository<User,int> repository)
{
this.repository = repository;
}
//If the user is added has Id 0 so search among all the existing users for one that could have the alias or email registered already
//else if the user is being edit then search among all the user except the user with such Id(itself)
public bool IsBroken(User model)
{
if (model.IdUser == 0)
{
return (
repository.List().Where(x => x.Alias == model.Alias).Any()
|| repository.List().Where(x => x.Email == model.Email).Any()
);
}
else
{
return (
repository.List().Where(x => x.Alias == model.Alias && x.IdUser != model.IdUser).Any()
|| repository.List().Where(x => x.Email == model.Email && x.IdUser != model.IdUser).Any()
);
}
}
public ErrorMessage ErrorMessage
{
get { return new ErrorMessage { Property = "AliasEmail", Message = "Alias or Email exists already" }; }
}
}
}
服务等级:
using ABC.DEF.Repository;
using ABC.DEF.Model;
using ABC.DEF.Service.Messaging.User;
namespace ABC.DEF.Service
{
public class UsuarioService
{
public AddUserResponse AddUserResponse(AddUserRequest addUserRequest)
{
AddUserResponse response = new AddUserResponse();
User objUser = new User();
objUser.Names = addUserRequest.Names;
objUser.LastName = addUserRequest.LastName;
objUser.Email = addUserRequest.Email;
objUser.Alias = addUserRequest.Alias;
objUser.Profile.IdProfile = addUserRequest.Profile.IdProfile;
objUser.Password = addUserRequest.Password;
objUser.Active = addUserRequest.Active;
//Determine if the Alias or Email are unique
Model.AliasOrEmailAreUnique aliasOrEmailAreUnique = new Model.AliasOrEmailAreUnique(_userRepository);
if (!aliasOrEmailAreUnique.IsBroken(objUser))
{
_usuarioRepository.Add(objUser);
response.State = true;
response.Message = "User added succesfully";
}
else
{
response.State = false;
response.Message = aliasOrEmailAreUnique.ErrorMessage.Message;
}
return response;
}
}
}