控制器可以单独吗? [英] Can controller be singleton?

查看:77
本文介绍了控制器可以单独吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,



我正在为我的网站实施MVC模式。我正在考虑将控制器实现为单例模式。任何人都有任何想法将控制器作为一个单独的类是多么好/坏?



我认为控制器无论如何都不应该响应维护用户特定的数据。它应该只作为业务层和演示者之间的中介。它应该是唯一可以访问/更新会话数据的地方,如果有必要,它应该执行验证。



您对将其作为单身人士的建议和嫌疑的所有想法都是赞赏。



谢谢

Nyoti

解决方案

虽然我确定你可以解决这个问题的任何限制,如果有足够的时间,我认为这种方法没有任何好处。



你在哪个平台?什么语言?什么版本?您是否研究过其他MVC框架实现?



控制器(在模式中)并不直接负责维护用户特定的数据。使它成为单例也无济于事,也无法阻止用户直接连接数据库。



无状态HTTP协议的本质,控制器将无休止地创造和销毁。无论你做什么,都要确保它可以扩展。



我的


0.05。


谢谢你的回答。我正在研究ASP.NET C#2.0。



控制器负责创建业务层对象并调用适当的方法。正如您所提到的,它不负责维护用户特定的数据。



现在,如果10000个用户正在访问我的网站,那么我认为如果创建10000个对象,而不是创建10000个控制器对象和实体10000对象。控制器的单个实例(当然,包含业务层对象作为公共属性)并分享它。



我研究过一些关于Spring MVC,ASP.NET MVC的内容。

Guys,

I am implementing MVC pattern for my web site. I am thinking of implementing a controller as a singleton pattern. Anybody has any thoughts about how good/bad it is to have controller as a singleton class?

I think that controller should not anyway be responcible for maintaining user specific data. It should only act as a mediator between business layer and presenter. It should be the only place to access/update session data and if necessary it should perform validation.

All your thoughts about the advt and disadvts of having it as singleton are appreciated.

Thanks
Nyoti

解决方案

While I'm sure you could work around any of the limitations of this, given enough time, I don't see any benefit to this approach.

What platform are you on? What language? What version? Have you studied other MVC framework implementations?

Controllers (in the pattern) aren't directly responsible for maintaining user-specific data. Making it a singleton won't help that, nor can you prevent a user from making a direct connection to a database.

By nature of the stateless HTTP protocol, controllers will be created and destroyed endlessly. Whatever you do, make sure it can scale.

My


0.05.


Thank you for your answer. I am working on ASP.NET C# 2.0.

The controller is responsible for creating business layer object and calling appropriate methods. As you mentioned correctly, it is not responsible for maintaining user specific data.

Now if 10000 users are accessing my site, then instead of creating 10000 objects of controller and intern 10000 objects of business layer, I thought it will be better if I create single instance of controller (ofcourse, containing business layer object as public property) and share it accross.

I have studied a bit about Spring MVC, ASP.NET MVC.


这篇关于控制器可以单独吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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