我有点新的价值注入,对整个对象实际对象映射世界。似乎valueinjecter是目前最好的选择之一。我想知道映射这种物体的最佳方法是什么
基本上我想要的是来自viewmodel的地图值
public class RegisterModel
{
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required]
[ValidatePasswordLength]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[StringLength(255)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Middle Name")]
public string MiddleName { get; set; }
[Required]
[StringLength(255)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
}
到我的域名实体
public class UserInfo : EntityBase
{
public UserInfo()
{
PersonName = new PersonName();
}
public virtual string Email { get; set; }
public virtual string Password { get; set; }
public virtual PersonName PersonName { get; set; }
}
public class PersonName
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Fullname1
{
get { return string.Format(@"{0}, {1} {2}", LastName, FirstName, MiddleName); }
}
public string Fullname2
{
get { return string.Format(@"{0} {1} {2}", FirstName, MiddleName, LastName); }
}
}
我尝试了这段代码并且它有效,但不确定它是否是最好的方法
var newuserinfo = new UserInfo();
newuserinfo.InjectFrom(model);
newuserinfo.PersonName.InjectFrom(model);
像valueinjecter这样的对象到对象映射框架在哪里适合系统架构?我正在考虑为我的对象映射编写单元测试。
答案 0 :(得分:1)
在codeplex上有一个示例项目,它使用ValueInjecter将实体映射到ViewModels并返回: http://prodinner.codeplex.com/
一般来说,只要它运作正常就可以了,如果不是这样你将会在之后重构它,当你想要看到更大的图片时