简体   繁体   English

在MVC 3中填充视图模型

[英]Populating a View Model in MVC 3

I'm working on refactoring an MVC 3 web app to not pass data models directly to the controllers, but to use View models instead (See inspiration here ). 我正在努力重构MVC 3 Web应用程序,以不将数据模型直接传递给控制器​​,而是改为使用View模型(请参见此处的灵感)。

Currently, when populating a list of models, we do something along these lines in the controller: 当前,当填充模型列表时,我们在控制器中按照以下步骤进行操作:

var myQuery = from t in _db.TimeEntries
select t;

List<TimeEntry> timeEntryList = myQuery.ToList<TimeEntry>();

TimeEntries is bound to our database. TimeEntries绑定到我们的数据库。

Now however, I have a view model with this: 但是现在,我有了一个视图模型:

public class TimeEntryViewModel
    {

        public TimeEntry entry {get; set;}
        public Time time { get; private set; }

        public TimeEntryViewModel();
        public TimeEntryViewModel(int ID)
        {
            PraxisTime.Models.PraxisTimeDB _db = new PraxisTime.Models.PraxisTimeDB();
            entry = _db.TimeEntries.Find(ID);

            time = _db.Times.Find(entry.TimeID);
        }
    }

This is all well and good, until I want to populate a list of these. 一切都很好,直到我想填充这些列表为止。 Here's my solution, added to the view model, but it feels cludgy. 这是我的解决方案,已添加到视图模型中,但感觉很笨拙。

public static List<TimeEntryViewModel> LoadTimeEntryViewModels(string userID)
        {
            theDB _db = new theDB();
            List<int> myQuery = (from t in _db.TimeEntries
                                 select t.ID).ToList<int>();

            List<TimeEntryViewModel> timeEntryList = new List<TimeEntryViewModel>();

            foreach (int i in myQuery)
            {
                timeEntryList.Add(new TimeEntryViewModel(i));
            }
            return timeEntryList;
        }

Is there a better way that I'm missing? 有没有更好的方法我想念吗?

Its not your view model's responsibility to know how to pull data and populate itself, its basically a dto with a little extra metadata for the view (like required etc). 知道如何提取数据和填充自身不是视图模型的责任,它基本上是一个dto,其中包含视图的一些额外元数据(如所需等)。 First go around I would have the controller make the call to your db. 首先,我要让控制器调用您的数据库。 You can then refactor the db calls into a repository/data access layer later on as your app grows. 然后,随着应用的增长,您可以稍后将db调用重构为存储库/数据访问层。

Also you can clean up your linq queries by doing this: 您还可以通过执行以下操作清理linq查询:

var entries = from t in _db.TimeEntries
              select new TimeEntryViewModel { entry = t.entry, time = t.time };

Also is TimeEntry you db entity? 另外TimeEntry是您的数据库实体吗? if so, you have reference to it in the view model. 如果是这样,则可以在视图模型中对其进行引用。 You are also newing up your db, I'd use some sort of dependency injection to decouple your code which will clean up your code as well as give you the ability to mock it in unit tests, and will set you up so later on you can pass in interfaces for dependancies instead of concretes. 您还正在更新数据库,我将使用某种依赖注入来分离您的代码,这将清理您的代码,并使您能够在单元测试中对其进行模拟,并将在以后为您设置可以传递依赖关系的接口而不是具体接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM