asp.net mvc 在另一个控制器的构造函数中创建控制器的实例是否正确?

发布于 2024-08-23 05:01:57 字数 317 浏览 5 评论 0原文

我有几个控制器,它们都将使用通用功能。所以我将该功能分离到一个单独的控制器中。

共享控制器需要一个特定于从哪个控制器使用它的参数,并且需要根据传递给它的 id 整数返回视图。

因此,一个想法是在每个将使用它的控制器的构造函数中创建 SharedController(int CallingControllerId) 的实例。然后,在每个控制器的操作方法中,调用共享控制器的操作方法,传递适当的 ID,并将视图从 SharedController 返回到调用控制器,这将返回要渲染的视图。

这听起来正确吗?控制器是否应该在 MVC 中创建其他控制器?

谢谢!

i have several controllers which will all be using common functionality. So i have separated that functionality into a separate controller.

the shared controller needs a parameter specific to which controller it is being used from, and needs to return views based on id ints passed to it.

So, one idea is to create an instance of SharedController(int callingControllerId) in the constructor of each of the controllers that will be using it. Then, within the action methods of each controller, call the action methods of the shared controller, passing appropriate ids, and returning views from SharedController to the calling controller, which would return the view to be rendered.

Does this sound right? Should controllers be creating other controllers in MVC?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

破晓 2024-08-30 05:01:57

绝对不是,尽管让你的控制器继承共享的基本控制器是很常见的。

public KittyController : MySharedBaseController
{

}

就像 Greg Roberts 指出的那样,虽然 ControllerBase 很常见,但它并不总是存储共享功能的最佳位置。 MVC 有很多 扩展点并选择一个狭窄的地方来插入代码比将大量内容塞入基本控制器要好得多。

Absolutely not although having your controller inherit from a shared, base controller is pretty common.

public KittyController : MySharedBaseController
{

}

Like Greg Roberts points out that although having a ControllerBase is common it is not always the best place to store shared functionality. MVC has a lot of extensibility points and picking a narrow place to wedge in your code is a lot better than smushing tons of things into a base controller.

笑饮青盏花 2024-08-30 05:01:57

我同意基本控制器非常常见,但我不确定这是否是您所描述内容的正确解决方案。

如果不知道确切共享的内容,则很难弄清楚,但是你的基本控制器的整个部分都知道从它继承的控制器的细节,有点味道。

这里有一些需要考虑的事情。

  • 组合几乎总是比继承更好。 关于此的精彩文章
  • 基本控制器应该具有通用的方法/属性,例如我的控制器具有获取当前用户的一些属性和用于调用外部服务的通用方法,但没有继承控制器的特定内容。 根据经验,如果超过 1 个控制器不需要它,则基本实现可能不太好。
  • 一些新的 MVC 2 功能可能会解决您正在做的事情。 查看渲染操作。

与软件中的任何内容一样,解决问题的方法有很多,因此最终要以对您和您的团队最有意义的方式进行。

I would agree that a base controller is pretty common, but I'm not sure it's the right solution for what you are describing.

Without knowing what is exactly shared it's a bit hard to figure out, but the whole bit of your base controller knowing specifics about the controllers that are inheriting from it smells a bit.

Here are a couple things to consider.

  • Composition is almost always better than inheritance. Great article on this
  • Base controller should have common methods/properties, for example mine have some properties of getting the current user and a generic method for calling external services, but not specific things from inheriting controllers. Rule of thumb, if it's not needed by more than 1 controller, than probably not good in base implementation.
  • It's possible that some of the new MVC 2 features might solve what you are doing. Look at render action.

As with anything in software, there are tons of ways to solve the problem, so ultimately do it the way that makes the most sense to you and your team.

江南烟雨〆相思醉 2024-08-30 05:01:57

控制器是由 ASP.NET MVC 运行时构建的,您永远不应该在自己的代码中构建它们。听起来您实际上并不需要单独的控制器,您只需要在控制器中聚合并用作服务的另一个类。这是完全可以接受的。事实上,我什至想说,一般来说,控制器应该将他们的工作委托给其他“服务”类,并且它们本身不应该承担太多责任(特别是域逻辑)。

我不知道为什么这个服务类需要知道从哪个控制器调用它的详细信息,但也许您可以声明一个定义不同用例的枚举,并将其传递到其构造函数中。实际上让服务类了解不同的控制器对我来说是一种代码味道。

Controllers are constructed by the ASP.NET MVC runtime and you should never be constructing them in your own code. It sounds like you don't really need a separate controller, you just need another class that you aggregate in the controller, and use as a service. This is perfectly acceptable. In fact, I would go so far as to say that in general controller should be delegating their work to other "service" classes and shouldn't have much responsibility (especially domain logic) in and of themselves.

I don't know the details of why this service class needs to know what controller it is being called from, but perhaps you can just declare an enum that defines the different use cases, and pass that in in its constructor. Actually having the service class know about different controllers would be a code smell for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文