【发布时间】:2012-09-25 14:19:30
【问题描述】:
我开始使用 AutoMapper,但出现了一些疑问。 将 dto 映射到域模型的正确方法在哪里? 我正在这样做:
DTO:
public class PersonInsert
{
[Required]
public string Name { get; set; }
public string LastName { get; set; }
}
行动:
[HttpPost]
public ActionResult Insert(PersonInsert personInsert)
{
if (ModelState.IsValid)
{
new PersonService().Insert(personInsert);
return RedirectToAction("Insert");
}
return View("Insert");
}
服务:
public class PersonService
{
public int Insert(PersonInsert personInsert)
{
var person = Mapper.Map<PersonInsert, Person>(personInsert);
return new PersonRepository().Insert(person);
}
}
存储库:
public class PersonRepository
{
internal int Insert(Person person)
{
_db.Person.Add(person);
_db.SaveChanges();
return person.Id;
}
}
那么,这是正确的吗?我的服务应该知道域吗?还是我应该只在存储库中进行绑定?在 DTO 中使用 [Required] 是否正确?
【问题讨论】:
标签: asp.net-mvc architecture automapper dto domain-model