c# - Proper use of Singleton in DAL and BO layer? -



c# - Proper use of Singleton in DAL and BO layer? -

i'm tasked implementing business object / info access layer project , have expect thousands of users concurrently. i've used singletons manage dal never game much thought how behave many multiple users @ same time, i'd inquire proper utilize it.

i have:

public class userdal { private static userdal _userdal = null; //private constructor private userdal() { } public static userdal getinstance() { if(_userdal == null) { _userdal = new userdal(); } homecoming _userdal; } //example of method public user getusers() { idatareader datareader = connectionfactory.getconnection().executesomequery("queryhere"); } }

for connection mill don't think it's problem, although did read it's best leave connection pooling ado.net itself:

public sealed class connectionfactory { private static string _connectionstring = configurationmanager.connectionstrings["connectionname"].connectionstring; //my connection interface private static iconnection _connection = null; public static iconnection getconnection() { if(_connection == null) { //some checks determine type _connection = new sqlconnection(_connectionstring); } homecoming _connection; } }

i'm using singleton pattern in bo, although don't think it's necessary:

public class userbo { private static userbo _userbo = null; private static userdal _userdal = null; private userbo() { } public static userbo getinstance() { if(_userbo == null) { _userbo = new userbo(); _userdal = userdal.getinstance(); } homecoming _userdal; } //example of method public user getuser() { //rules homecoming _userdal.getusers(); //return userdal.getinstance().getusers(); //or } }

i'm doing can phone call in ui/presentation layer:

user someuser = userbo.getinstance().getuser(1);

this worked me applications i've made far, i'm guessing it's because there wasn't many users simultaneously. i'm worried happen in userdal instance when sec user requests there's 1st user doing heavy operation in it.

should drop pattern in bo/dal layer , leave in connectionfactory? there issues should expect if utilize this?

i drop altogether, connection: connectionfactory static, homecoming new connection each time asked: ado.net @ managing connection pooling , need out of it's way.

in has changeable state maintain away singletons. includes ado.net connections, , actual business objects. having 1 user mutate state of object beingness used user can lead sorts of unusual bugs: in web site, have massively multithreaded application , changeable singletons bad news!

you need come sort of locking strategy, though, when 2 or more users alter copies of same business object. valid strategy includes saying 'actually, isn't going problem i'll ignore it' - if have thought it. 2 basic strategies optimistic , pessimistic locking.

optimistic locking means optimistically think users won't alter same things (for whatever reason) , don't set database locks on read data. possibility on web site

pessimistic locking says perchance changed info will, when read, have db locks applied until user finished it. means keeping transaction open, , it's not practical web site.

optimistic locking can implemented creating update statements update row columns haven't been changed current user haven't been changed in database; if have, else has changed same row. alternatively, can add together column tables - version int not null - , update version hasn't changed since read object; increment version number in every update.

if either method fails, need reread now-current info , user confirm or re-apply changes. bit of pain can necessary.

c# .net design-patterns singleton

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -