【问题标题】:ASP.NET MVC - Querying into View Models Outside the ControllerASP.NET MVC - 查询控制器外部的视图模型
【发布时间】: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


【解决方案1】:

最近我读了几篇关于重构控制器和将代码移动到应用程序服务的好文章:

7 保持控制器精简

http://www.codemag.com/Article/1405071

最佳实践 - 瘦控制器

http://www.arrangeactassert.com/asp-net-mvc-controller-best-practices-%E2%80%93-skinny-controllers/

所以我做了一些重构,控制器现在看起来:

public class TestController : Controller
{
    private readonly ITestApplicationService service;

    public TestController (ITestApplicationService service)
    {
        this.service = service;
    }

    public ActionResult Index(SomeFilter filter)
    {
        var viewModelList = service.GetModelList(filter, Url);
        return View(viewModelList);
    }
    ...
}

创建了一个新的应用服务:

public interface ITestApplicationService
{
    IQueryable<TestViewModel> GetModelList(SomeFilter filter, UrlHelper url);
    void Save(TestViewModel model);
    void Delete(int id);
}

public class TestApplicationService
{
    private readonly ITestRepository repository;
    
    public TestApplicationService(ITestRepository repository)
    {
        this.repository = repository;
    }
    
    public IQueryable<TestViewModel> GetModelList(SomeFilter filter, UrlHelper url)
    {
       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 viewModelList;
    }
    ...
}

如果有人有其他想法、想法等,请告诉我。

【讨论】:

    猜你喜欢
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多