ValueInjecter 架构建议
我对值注入器、整个对象到对象映射世界实际上有点陌生。似乎 valueinjecter 是目前更好的选择之一。我想知道映射此类对象的最佳方法是什么
基本上我想要的是将值从视图模型映射
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 这样的对象映射框架适合系统架构吗?我正在考虑为我的对象映射编写单元测试。
im kinda new to value injecter, to the whole object to object mapping world actualy. seems like valueinjecter is one of the better choice out as of the moment. Im wondering what is the best way of mapping this kind of objects
Basically what i want is map value from a 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; }
}
to my domain entity
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); }
}
}
i tried this code and it works but not sure if its the best way to do it
var newuserinfo = new UserInfo();
newuserinfo.InjectFrom(model);
newuserinfo.PersonName.InjectFrom(model);
And where does object-to-object mapping framework like valueinjecter fit in the system architecture? Im thinking of writing a unit tests for my object mapping.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
codeplex 上有一个示例项目,它使用 ValueInjecter 将实体映射到 ViewModel 并返回:
http://prodinner.codeplex.com/
一般来说,只要它能工作就可以了,如果不能当你要看到更大的图景时,你不打算重构它吗
There's a sample project on codeplex that uses ValueInjecter to map Entities to ViewModels and back:
http://prodinner.codeplex.com/
In general as long as it works it's ok, and if it isn't you are going to refactor it afterwards, when you are going to see the bigger picture