Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 28-Nov-17 1:30am

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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900