MVC 架构 - 模式 [英] MVC architecture - patterns

查看:38
本文介绍了MVC 架构 - 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 MVC 架构方面的帮助.我将以下体系结构用于名为 User 的对象.

I need some help with MVC architecture. I use the following architecture for an object named User.

UserRepository -> IUserRepository -> UserService -> IUserService -> UserController

UserRepository -> IUserRepository -> UserService -> IUserService -> UserController

用户对象来自我的数据库,我使用的是 EntityFramework.我有一个返回用户列表的函数 Return_All_Users().返回的字段之一是UserType".这是一个数字,但是当我在我的 Index.aspx 页面中显示列表时,我希望它显示为一个字符串.UserType"的数字需要查看字典才能找到与Administrator"这样的数字对应的字符串,我希望这个字符串显示在我的索引页面中.

The User object comes from my database and I'm using EntityFramework. I have a function that returns a list of users Return_All_Users(). One of the fields that gets returned is "UserType". This comes out as a number, but when I show the list in my Index.aspx page, I would like it to show as a string. The number for "UserType" needs to look into a Dictionary to find the string that goes along with the number like "Administrator" and I would like this string to show in my Index page.

我该怎么做?我目前正在使用此功能:

How would I go about doing this? I'm currently using this function:

  public IEnumerable<User> Return_All_Users()
{
  //my code here
}

我有另一篇关于类似问题的帖子,有人建议我的 IEnumerable 应该返回一个字符串,而不是 User 对象.我试过了,但后来我正在使用动态模型,我不想这样做.我也不知道如何使用动态模型,所以我想也许有更好的方法来做到这一点.

I had another post for a similar question, and it was suggested that my IEnumerable should return a string, not the User object. I tried that, but then I'm working with a Dynamic model and I didn't want to do that. Nor do I know how to work with a Dynamic model, so I thought maybe there is a better way of doing this.

这应该发生在我的 UserService 中吗?也许我需要创建一个名为 NewUser 的新类,并将 UserType 定义为字符串,然后实例化一个新的 NewUser 并从 User 传递值.在传递 UserType 之前,我会查看字典并获取键的正确值.

Should this happen in my UserService? Maybe I need to create a new class called NewUser, and define the UserType as a string, then instantiate a new NewUser and pass the value from User. Before the UserType is passed, I would look in the dictionary and get the correct value for the key.

我应该这样做,还是有更好的方法?

Should I do that, or is there a better way?

在我发布这个问题后,我只是想用一个存储过程来做这个.也许在我的数据库中使用存储过程来返回数据,而不是直接从我的数据库表中查找我的数据.

After I posted this question, I just thought of doing this with a stored procedure. Maybe use a stored procedure in my database to return the data, as opposed to looking up my data straight from my database table.

推荐答案

我将创建一个视图模型 UserView,它的格式被格式化为您的视图如何最好地使用该数据.您可以在存储库或控制器中执行从 User 到 UserView 的映射,具体取决于您喜欢在何处进行此类映射.

I would create a View Model UserView that was formatted to the representation of how your view would best use that data. You could perform the mapping from User to UserView within your Repository or Controller depending on where you prefer doing this kind of mapping.

这篇关于MVC 架构 - 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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