Model view controller 我的MVC方法正确吗?

Model view controller 我的MVC方法正确吗?,model-view-controller,asp.net-mvc-5,Model View Controller,Asp.net Mvc 5,我现在正在用MVC5编写一个应用程序。不久前,我制作了MVC应用程序(用于iOS),但老实说,我现在有点困惑。我试图找到一些关于这种模式的信息,但似乎有很多方法 我的应用程序使用JSON格式的外部数据库。我在Api类中有很多方法,它们从数据库返回填充了数据的对象。在我看来,这个Api类基本上是模型,但我不确定 型号: //Model public class UserModel { public string Name { get; set; } public string La

我现在正在用MVC5编写一个应用程序。不久前,我制作了MVC应用程序(用于iOS),但老实说,我现在有点困惑。我试图找到一些关于这种模式的信息,但似乎有很多方法

我的应用程序使用JSON格式的外部数据库。我在Api类中有很多方法,它们从数据库返回填充了数据的对象。在我看来,这个Api类基本上是模型,但我不确定

型号:

//Model
public class UserModel
{
    public string Name { get; set; }
    public string Lastname { get; set; }

}
Api类别(型号?):

控制器:

//Controller
public ActionResult Index ()
{
    UserModel model = new UserModel();
    Api api = new Api();
    model = api.GetUserData();

    return View(model);
}
视图:

最后一个问题。我应该将loginuser、delete user等方法放在哪里:

public void DeleteUser(UserModel model)
{
    //code deleting user 
}
它应该交给模型还是控制器?我的想法是——如果它将在不同的地方多次使用,我应该把它放在模型中,否则它应该放在控制器中


提前感谢。

您的Api类不是模型,模型类必须是域的实体。你的领域是你业务的核心,例如,如果你正在申请学校,你的模型/实体是:学生、教师、纪律

因此,您的Api属于基础架构层,所有类都具有数据库访问责任

您的控制器只能编排工作流,例如:

private readonly api;

public HomeController()
{
    this.api = new Api();
}

public ActionResult Index ()
{
    var model = api.GetUserData();
    return View(model);
}
public ActionResult Login(string login, string password)
{
    var user = api.GetUserByLogin(login);

    if(user == null)
    {
        ViewBag.ErrorMsg = "There is no user with this login";
        return View();
    }

    //userService is a class responsible for business rules for users
    var isSuccess = userService.LoginUser(login, password);

    if(isSuccess)
        return RedirectToAction("Index", "Home");

    ViewBag.ErrorMsg = "Password is incorrect";
    return View();
}
您的Api可能会用于其他操作,所以您可以创建一个私有字段并从构造函数初始化它。但最好的方法是使用IoC容器

LoginUser必须是控制器上的操作,但业务规则可以位于项目的其他层,因此可以重用。例如:

private readonly api;

public HomeController()
{
    this.api = new Api();
}

public ActionResult Index ()
{
    var model = api.GetUserData();
    return View(model);
}
public ActionResult Login(string login, string password)
{
    var user = api.GetUserByLogin(login);

    if(user == null)
    {
        ViewBag.ErrorMsg = "There is no user with this login";
        return View();
    }

    //userService is a class responsible for business rules for users
    var isSuccess = userService.LoginUser(login, password);

    if(isSuccess)
        return RedirectToAction("Index", "Home");

    ViewBag.ErrorMsg = "Password is incorrect";
    return View();
}

要小心,因为ASP.NET MVC仍然有ViewModels的概念。这些类用于在视图中显示数据。例如,在我们的学校申请中,我们可以有一个必要的视图来显示所有注册学生的学科,以及教授是什么。因此,我们需要使用3个实体的信息,在这种情况下,我们创建一个ViewModel来同时显示所有这些信息。

您的Api类不是模型,模型类必须是您域的实体。你的领域是你业务的核心,例如,如果你正在申请学校,你的模型/实体是:学生、教师、纪律

因此,您的Api属于基础架构层,所有类都具有数据库访问责任

您的控制器只能编排工作流,例如:

private readonly api;

public HomeController()
{
    this.api = new Api();
}

public ActionResult Index ()
{
    var model = api.GetUserData();
    return View(model);
}
public ActionResult Login(string login, string password)
{
    var user = api.GetUserByLogin(login);

    if(user == null)
    {
        ViewBag.ErrorMsg = "There is no user with this login";
        return View();
    }

    //userService is a class responsible for business rules for users
    var isSuccess = userService.LoginUser(login, password);

    if(isSuccess)
        return RedirectToAction("Index", "Home");

    ViewBag.ErrorMsg = "Password is incorrect";
    return View();
}
您的Api可能会用于其他操作,所以您可以创建一个私有字段并从构造函数初始化它。但最好的方法是使用IoC容器

LoginUser必须是控制器上的操作,但业务规则可以位于项目的其他层,因此可以重用。例如:

private readonly api;

public HomeController()
{
    this.api = new Api();
}

public ActionResult Index ()
{
    var model = api.GetUserData();
    return View(model);
}
public ActionResult Login(string login, string password)
{
    var user = api.GetUserByLogin(login);

    if(user == null)
    {
        ViewBag.ErrorMsg = "There is no user with this login";
        return View();
    }

    //userService is a class responsible for business rules for users
    var isSuccess = userService.LoginUser(login, password);

    if(isSuccess)
        return RedirectToAction("Index", "Home");

    ViewBag.ErrorMsg = "Password is incorrect";
    return View();
}

要小心,因为ASP.NET MVC仍然有ViewModels的概念。这些类用于在视图中显示数据。例如,在我们的学校申请中,我们可以有一个必要的视图来显示所有注册学生的学科,以及教授是什么。因此,我们需要使用3个实体的信息,在这种情况下,我们制作一个ViewModel来同时显示所有这些信息。

是的,我熟悉ViewModel的概念。谢谢您的解释。是的,我熟悉ViewModel的概念。谢谢你的解释。