我有2个模型(EndUser和EndUserServicesDTO具有相似的逻辑,但是存在于两个不同的层上)
public class EndUserServicesDTO
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Role { get; set; }
public string Password { get; set; }
public string RetypePassword { get; set; }
}
public class AppDeveloperServicesDTO : EndUserServicesDTO
{
public string CompanyName { get; set; }
public string CompanyAdress { get; set; }
public string CompanyEmail { get; set; }
public string CompanyPhoneNumber { get; set; }
public string CompanyWebSite { get; set; }
}
我在不同的存储库中有2种方法
private readonly IMongoUOWRepositoryGeneric<EndUser> _userManager;
public async Task<bool> AddAsync(EndUserServicesDTO userServicesDTO)
{
EndUser user = _mapper.Map<EndUser>(userServicesDTO);
user.PasswordHash = PBKDF2Hasher.HashPassword(userServicesDTO.Password);
await _userManager.Clients.AddAsync(user);
return true;
}
private readonly IMongoUOWRepositoryGeneric<AppDeveloper> _developerManager;
public async Task<bool> AddAsync(AppDeveloperServicesDTO developerServicesDTO)
{
AppDeveloper developer = _mapper.Map<AppDeveloper>(developerServicesDTO);
developer.PasswordHash = PBKDF2Hasher.HashPassword(developerServicesDTO.Password);
await _developerManager.Clients.AddAsync(developer);
return true;
}
两种方法都具有相同的功能,但用于不同的模型。您能否指导我编写相同的通用方法来同时牢记确切的场景。
到目前为止我尝试过的事情
导致进入EndUser
类,但是如果这样做,我将删除Developer model
中的公司属性,并在没有它的情况下将其保存,否则我无法将.PasswordHash
用于属性{{ 1}},我不知道如何使用通用方法。
答案 0 :(得分:2)
整个更改应该是这样
public async Task<bool> AddAsync<T>(T dto) where T : IPassword
{
var data = _mapper.Map<T>(dto);
data.PasswordHash = PBKDF2Hasher.HashPassword(data.Password);
await _developerManager.Clients.AddAsync(data);
return true;
}
public interface IPassword
{
string Password { set; get; }
string PaswordHash { set; get; }
}
public class EndUserServicesDTO : IPassword
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Role { get; set; }
public string Password { get; set; }
public string RetypePassword { get; set; }
}
public class AppDeveloperServicesDTO : EndUserServicesDTO, IPassword
{
public string CompanyName { get; set; }
public string CompanyAdress { get; set; }
public string CompanyEmail { get; set; }
public string CompanyPhoneNumber { get; set; }
public string CompanyWebSite { get; set; }
}
基于新要求的新版本:
public class ClientRepository<T>
{
private readonly IMongoUOWRepositoryGeneric<T> _manager;
readonly IMapper _mapper;
public ClientRepository(IMongoUOWRepositoryGeneric<T> manager, IMapper mapper)
{
_manager = manager;
_mapper = mapper;
}
public async Task<bool> AddAsync<U>(U dto) where U : IPassword
{
dto.PaswordHash = PBKDF2Hasher.HashPassword(dto.Password);
T data = _mapper.Map<T,U>(dto);
await _manager.Clients.AddAsync(data);
return true;
}
}
答案 1 :(得分:0)
也许使用通用T可以解决您的问题
public async Task<bool> AddAsync<T>(T dto)
{
T data = _mapper.Map<T>(dto);
data.PasswordHash = PBKDF2Hasher.HashPassword(data.Password);
await _developerManager.Clients.AddAsync(data);
return true;
}