【发布时间】:2014-06-20 12:27:34
【问题描述】:
我有几个将实体(持久性模型)返回到视图模型列表中的存储库。所有实体到视图模型的映射都发生在控制器中。示例:
public class TestController : Controller
{
private readonly ITestRepository repository;
public TestController (ITestRepository repository)
{
this.repository = repository;
}
public ActionResult Index(SomeFilter filter)
{
var viewModelList = repository.GetTestEntityBy(filter.TestId, filter.Name) // returns IQueryable<TestEntity>
.Select(x => new TestViewModel // linq projection - mapping into the list of viewModel
{
Id = x.Id,
Name = SomeFormatter.FormatName(
x.TestId,
x.TestAddress1,
x.TestAddress2),
Url = UrlFormatter.Format(x.TestName, Url.Action("ChangeValue", "TestController", new { x.id })),
AllergyType = x.TestType
Notes = x.Notes,
...
});
return View(viewModelList);
}
}
问题:在控制器外部存储此代码(映射、url 格式化程序等)的最佳方式是什么(模式?位置?)?最终,我最终在 Models 文件夹中创建了静态类。 谢谢!
【问题讨论】:
-
你到底是怎么得到 static 类的?您可以将这些查询放在查询对象/处理程序/存储库/服务中(您决定名称)
标签: c# asp.net-mvc design-patterns domain-driven-design