如何初始化MVC模块?

时间:2023-01-15 22:19:20

I am new to MVC programming. In normal OOP, where I have my class, I would just initiliaze and load data from database. In MVC, we have modules, how do I load up records from it?

我是MVC编程的新手。在普通的OOP中,我有我的班级,我只是初始化并从数据库加载数据。在MVC中,我们有模块,如何从中加载记录?

Here is my current code for type UserAcount:

这是我当前的UserAcount类型代码:

[Table("UserAccount")]
public class UserAccount {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public string userName { get; set; }
    public string email { get; set; }
    public int companyID { get; set; }
}

Say that I have an user with name "testUser", how do I initialize on this record and get it's information? How do I do this:

假设我有一个名为“testUser”的用户,如何初始化此记录并获取其信息?我该怎么做呢:

UserAccount user = new UserAccount("tesetUser");

How and where shoulud I use this?

我怎么以及在哪里使用这个?

user = user.SingleOrDefault(u => u.userName.ToLower() == User.Identity.Name.ToLower());

3 个解决方案

#1


1  

You need to read up on Entity Framework. This is the default ORM that MVC uses. Simply:

您需要阅读实体框架。这是MVC使用的默认ORM。只是:

  1. If you don't have a project context, yet, create one:

    如果您还没有项目上下文,请创建一个:

    public class MyProjectContext : DbContext
    {
        public MyProjectContext()
            : base("name=ConnectionStringNameHere")
        {
        }
    }
    
  2. Add your models to your project context:

    将模型添加到项目上下文中:

    public class MyProjectContext : DbContext
    {
        ...
    
        public DbSet<SomeModel> SomeModels { get; set; }
        public DbSet<SomeOtherModel> SomeOtherModels { get; set; }
        # etc.
    }
    
  3. Update your database using Package Manager Console (TOOLS > Library Package Manager > Package Manager Console):

    使用程序包管理器控制台更新数据库(TOOLS>库包管理器>程序包管理器控制台):

    > update-database
    

(hit ENTER after typing that)

(输入后按ENTER键)

Now, to use your context in your controllers:

现在,在控制器中使用您的上下文:

public class MyAwesomeController : Controller
{
    private MyProjectContext db = new MyProjectContext();

    public ActionResult Index()
    {
        var someModels = db.SomeModels;
        return View(someModels);
    }

    public ActionResult GetSomeModel(int id)
    {
        var someModel = db.SomeModels.Find(id);
        return View(someModel);
    }

    # other actions
}

#2


0  

In the simplest case, you should do this logic in your controller, which will pass the data to the view. However, MVC is meant for UI separation of concerns, so theoretically you should be doing this in your domain layer, which is called from your controller.

在最简单的情况下,您应该在控制器中执行此逻辑,这将把数据传递给视图。但是,MVC用于关注UI的分离,因此从理论上讲,您应该在您的域层中执行此操作,该层将从您的控制器调用。

Here is a decent article from Jeff Atwood, however I disagree that the controller is the brains of the application. It is more of the brains of the UI...but that depends on how complex your code is. Dont create a domain layer if it is stupidly simple

这是Jeff Atwood的一篇不错的文章,但我不同意控制器是应用程序的大脑。它更像是UI的大脑......但这取决于代码的复杂程度。如果它非常简单,请不要创建域层

#3


0  

In the MVC model, Controllers are responsible for processing HTTP requests.

在MVC模型中,控制器负责处理HTTP请求。

Typically you would load your entity (e.g. UserAccount) in a controller action.

通常,您会在控制器操作中加载实体(例如UserAccount)。

If you want to edit / update an entity, typically you would map the relevant fields to a model that reflects the UserAccount. A separate model is suggested because the needs of the UI are often somewhat different than the needs of the entity model. Having separate classes for each concern avoids polluting the entity model to satisfy the needs of the view.

如果要编辑/更新实体,通常会将相关字段映射到反映UserAccount的模型。建议使用单独的模型,因为UI的需求通常与实体模型的需求有所不同。为每个关注点分别设置类可以避免污染实体模型以满足视图的需要。

#1


1  

You need to read up on Entity Framework. This is the default ORM that MVC uses. Simply:

您需要阅读实体框架。这是MVC使用的默认ORM。只是:

  1. If you don't have a project context, yet, create one:

    如果您还没有项目上下文,请创建一个:

    public class MyProjectContext : DbContext
    {
        public MyProjectContext()
            : base("name=ConnectionStringNameHere")
        {
        }
    }
    
  2. Add your models to your project context:

    将模型添加到项目上下文中:

    public class MyProjectContext : DbContext
    {
        ...
    
        public DbSet<SomeModel> SomeModels { get; set; }
        public DbSet<SomeOtherModel> SomeOtherModels { get; set; }
        # etc.
    }
    
  3. Update your database using Package Manager Console (TOOLS > Library Package Manager > Package Manager Console):

    使用程序包管理器控制台更新数据库(TOOLS>库包管理器>程序包管理器控制台):

    > update-database
    

(hit ENTER after typing that)

(输入后按ENTER键)

Now, to use your context in your controllers:

现在,在控制器中使用您的上下文:

public class MyAwesomeController : Controller
{
    private MyProjectContext db = new MyProjectContext();

    public ActionResult Index()
    {
        var someModels = db.SomeModels;
        return View(someModels);
    }

    public ActionResult GetSomeModel(int id)
    {
        var someModel = db.SomeModels.Find(id);
        return View(someModel);
    }

    # other actions
}

#2


0  

In the simplest case, you should do this logic in your controller, which will pass the data to the view. However, MVC is meant for UI separation of concerns, so theoretically you should be doing this in your domain layer, which is called from your controller.

在最简单的情况下,您应该在控制器中执行此逻辑,这将把数据传递给视图。但是,MVC用于关注UI的分离,因此从理论上讲,您应该在您的域层中执行此操作,该层将从您的控制器调用。

Here is a decent article from Jeff Atwood, however I disagree that the controller is the brains of the application. It is more of the brains of the UI...but that depends on how complex your code is. Dont create a domain layer if it is stupidly simple

这是Jeff Atwood的一篇不错的文章,但我不同意控制器是应用程序的大脑。它更像是UI的大脑......但这取决于代码的复杂程度。如果它非常简单,请不要创建域层

#3


0  

In the MVC model, Controllers are responsible for processing HTTP requests.

在MVC模型中,控制器负责处理HTTP请求。

Typically you would load your entity (e.g. UserAccount) in a controller action.

通常,您会在控制器操作中加载实体(例如UserAccount)。

If you want to edit / update an entity, typically you would map the relevant fields to a model that reflects the UserAccount. A separate model is suggested because the needs of the UI are often somewhat different than the needs of the entity model. Having separate classes for each concern avoids polluting the entity model to satisfy the needs of the view.

如果要编辑/更新实体,通常会将相关字段映射到反映UserAccount的模型。建议使用单独的模型,因为UI的需求通常与实体模型的需求有所不同。为每个关注点分别设置类可以避免污染实体模型以满足视图的需要。