asp.net MVC:将查询字符串作为字符串或单个参数传递?

时间:2022-12-02 10:04:17

In asp.net MVC I have a search action in my controller and I am trying to decide If I should pass the query to my repository as a string

在asp.net MVC中,我在我的控制器中有一个搜索操作,我正在尝试决定是否应该将查询作为字符串传递给我的存储库

public ActionResult Search(string query)
{
    return View(_repository.ListPeople(query));
}

or as individual parameters:

或作为个别参数:

public ActionResult Search(string FirstName, string LastName, System.Nullable<byte> Education)
{
    return View(_repository.ListPeople(FirstName, LastName, Education));
}

A lot of examples I have seen online use the query string method, but to me it doesn't feel as "safe", even though it's a little easier to deal with when you have a bunch of parameters to pass in. Is there a general consensus as to the better way to do this?

我在网上看到的很多例子都使用了查询字符串方法,但对我而言,它并不觉得“安全”,即使你有一堆参数可以传入时更容易处理。有没有关于更好的方法这样做的普遍共识?

3 个解决方案

#1


I would favour model binding. That way if you decide to add extra search options you have no changes to make apart from the model class.

我赞成模型绑定。这样,如果您决定添加额外的搜索选项,则除了模型类之外,您没有任何更改。

Info here and here

信息在这里和这里

#2


Personally, I prefer to not have the repository take on the responsibility of parsing a query string. Looking at it from a separation of concerns point of view, I feel that the repository should remain implementation-agnostic. Having to create query strings for unit testing, for example, is more cumbersome than calling the method directly with parameters.

就个人而言,我更喜欢不让存储库承担解析查询字符串的责任。从关注点分离的角度来看,我觉得存储库应该保持与实现无关。例如,必须为单元测试创​​建查询字符串比直接使用参数调用方法更麻烦。

That being said, I also prefer to have controllers interface with well-defined services, as well, as it helps to keep the controllers lighter, in my experience. I try to let the service act as a gateway to the repository, although there is no requirement to do so. There are two things that I will pass into a repository (or more likely, service), depending upon what the purpose of the call is - either a set of parameters, like you give in your second example, or as a constructed domain object, if applicable.

话虽这么说,我也更喜欢控制器与定义良好的服务接口,因为根据我的经验,它有助于保持控制器更轻。我尝试让服务充当存储库的网关,尽管没有要求这样做。我将把两件事传递到存储库(或者更可能是服务),这取决于调用的目的是什么 - 一组参数,就像你在第二个例子中给出的那样,或者作为构造的域对象,如果适用的话

#3


I would most definitely suggest going with the second route. There's no need to couple your controller with your repository. Let your repository be in charge of getting data, it shouldn't have to do any kind of parsing of a query string. That's not its job. Let the controller deal with that.

我绝对建议选择第二条路线。您无需将控制器与存储库耦合。让您的存储库负责获取数据,它不应该对查询字符串进行任何类型的解析。那不是它的工作。让控制器处理。

#1


I would favour model binding. That way if you decide to add extra search options you have no changes to make apart from the model class.

我赞成模型绑定。这样,如果您决定添加额外的搜索选项,则除了模型类之外,您没有任何更改。

Info here and here

信息在这里和这里

#2


Personally, I prefer to not have the repository take on the responsibility of parsing a query string. Looking at it from a separation of concerns point of view, I feel that the repository should remain implementation-agnostic. Having to create query strings for unit testing, for example, is more cumbersome than calling the method directly with parameters.

就个人而言,我更喜欢不让存储库承担解析查询字符串的责任。从关注点分离的角度来看,我觉得存储库应该保持与实现无关。例如,必须为单元测试创​​建查询字符串比直接使用参数调用方法更麻烦。

That being said, I also prefer to have controllers interface with well-defined services, as well, as it helps to keep the controllers lighter, in my experience. I try to let the service act as a gateway to the repository, although there is no requirement to do so. There are two things that I will pass into a repository (or more likely, service), depending upon what the purpose of the call is - either a set of parameters, like you give in your second example, or as a constructed domain object, if applicable.

话虽这么说,我也更喜欢控制器与定义良好的服务接口,因为根据我的经验,它有助于保持控制器更轻。我尝试让服务充当存储库的网关,尽管没有要求这样做。我将把两件事传递到存储库(或者更可能是服务),这取决于调用的目的是什么 - 一组参数,就像你在第二个例子中给出的那样,或者作为构造的域对象,如果适用的话

#3


I would most definitely suggest going with the second route. There's no need to couple your controller with your repository. Let your repository be in charge of getting data, it shouldn't have to do any kind of parsing of a query string. That's not its job. Let the controller deal with that.

我绝对建议选择第二条路线。您无需将控制器与存储库耦合。让您的存储库负责获取数据,它不应该对查询字符串进行任何类型的解析。那不是它的工作。让控制器处理。