asp.mvc模型设计 [英] asp.mvc model design

查看:96
本文介绍了asp.mvc模型设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新的MVC,我寻找一种方式来设计我的模型。

I am pretty new to MVC and I am looking for a way to design my models.

我有MVC网站项目,另一个类库,需要照顾的数据访问和构建业务对象。

I have the MVC web site project and another class library that takes care of data access and constructing the business objects.

如果我有在组装了一个名为项目类,是一个业务对象,我需要在一个视图中显示所有项目......我应该再拍模型类的项目?在这种情况下,类将是相同的。
我是否能从中获益,做一个新的模型类的东西吗?
我不喜欢在视图中引用从另一个DLL的对象......但我不喜欢复制code都不是。

If I have in that assembly a class named Project that is a business object and I need to display all projects in a view ... should I make another model class Project? In this case the classes will be identical. Do I gain something from doing a new model class? I don't like having in views references to objects from another dll ... but i don't like duplicating the code neither.

你有没有遇到同样的问题?

Did you encounter the same problem?

推荐答案

我通常使用现有和自定义类的组合模式,根据不同的观点如何紧密地映射到数据存储中的实际的类上。对于纯CRUD页面,使用数据类模型是完美的。在现实世界中使用,虽然你通常最终需要数据的几个其他位。我觉得把它们混合起来最简单的方法是一样的东西。

I usually use a combination of existing and custom classes for models, depending on how closely the views map to the actual classes in the datastore. For pure crud pages, using the data class as the model is perfect. In real world use though you usually end up needing a few other bits of data. I find the easiest way to combine them is something like

// the class from the data store
public class MyDataObject {
    public string Property1 {get;set;}
    public string Property2 {get;set;}
    }

// model class in the MVC Models folder
public class MyDataObjectModel {
    public MyDataObject MyDataObject {get;set;}
    // list of values to populate a dropdown for Property2
    public List<string> Property2Values {get;set;}
    // related value fetched from a different data store
    public string Property3 {get;set;}
    }

我也有相当的只是一组表单域的几个模型类 - 基本上相当于动作参数从一个特定的形式,而不是任何实际的数据库结束了

I also have quite a few model classes that are just a set of form fields - basically corresponding to action parameters from a particular form rather than anything that actually ends up in the database.

这篇关于asp.mvc模型设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆