4

I'm working on creating n-tiered application where I will have two separate project

1) project EF (where it will have all my edmx...)
2) project MVC 4 (internet application.)

In my EF i have, I have my .edmx file and it generate couple of classes with all props as show below (as sample)...

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
    public partial class Requester
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        <//more...........>
    }

everything is good so far!!

Back to MVC project

Now I will be creating a new Controller in my MVC project and when I'm trying to create Scaffolding and provide the Controller name and Controller expects a Model so the real question is:

What Model should I be passing here? should I have the same class that EF created? or should I be creating another Model in my 'Model Folder` (MVC) and bind it? if yes than am I not creating duplicate property if I go ahead and create my same Model in MVC model Folder project?

What I'm trying to do? : Well my purpose of this exercise is to have my Data Access Layer (DAL) totally separate from MVC project.

any thoughts?

4

2 回答 2

1

我建议创建一个视图模型,以便您可以使用与视图相关的东西(即 UIHint)来装饰属性。此外,此视图模型将是该类的简化版本(例如,它可以仅包含相关对象的 id 而不是整个对象),使其更易于用作操作参数。

另外,您在这里谈论对象,尽量不要考虑“数据”。

于 2013-08-29T00:54:54.723 回答
1

MVC 确实需要改名为 VMVC - ViewModel View Controller。

MVC 中的模型与 EF、Persistence 或您的域无关。它们是视图中表示/需要的多个数据/设置/事物源的组合。

因此,为您的视图创建新的视图模型。

编辑:

所有使用 EF Code First 模型作为视图模型的示例/教程都是糟糕的教程/示例。他们教你不好的做法,因为在现实世界中,你永远不会,也不应该直接在你的视野中使用它们。

ViewModel 是进入视图的数据的组合或聚合。例如:

如果您有一个产品详细信息页面,您可能会从数据库中获取产品信息,从 Web 服务中获取产品的可用性,从某个缓存中获取您的购物车。

这些将组成一个 ViewModel ,它代表您正在显示的视图。并渲染。

ViewModel 不应在视图之间共享,因为如果您更改 ViewModel,您会更改共享该 View Model 的视图的含义。

于 2013-08-29T01:21:37.777 回答