Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton DAL class

Shouldn't classes in DAL (customerDAL) be singleton? Since my controllers (customerController) exposes "Shared Subs", then on every call, there is no need to create a new DAL object if already existed. Correct?

Thanks

like image 716
Saif Khan Avatar asked Jan 24 '23 22:01

Saif Khan


2 Answers

Singleton objects are notoriously hard to test. I would look at creating your DAL in such a way that it isn't expensive to instantiate and then creating a new one as needed. This way you'll be able to write unit tests for DAL much more easily and still not incur much overhead. Additionally, if you create the DAL as a singleton, you'll need to be much more concerned with making it thread-safe if you use it in a multi-threaded environment (such as a web app).

like image 75
tvanfosson Avatar answered Feb 04 '23 16:02

tvanfosson


If you have a web site singleton dal objects are very dangerous because every request to the site is part of the same application. If you build them wrong, they can become bottlenecks for access to the database.

like image 43
Joel Coehoorn Avatar answered Feb 04 '23 15:02

Joel Coehoorn