By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,012 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,012 IT Pros & Developers. It's quick & easy.

Should I create my object with singleton pattern? thanks.

P: n/a
As I asked in last post, I want to put some logic in a object and all my
webcontrol instance will access that object, the object is responsed to
retrieve data from database if the data has not been retrieved yet. Vadym
Stetsyak suggested me to use singleton, it seems a good solution for me, but
after I read more about singleton, I find another issue - if I use singleton
patter, I can not store context data in the class anymore. How I used the
class is that I assigned required data to the class's variables, and then I
called methods in the class to get result. If I use singleton pattern, how
should I tell the called method what are those context data? Should I pass
all them by parameters? It seems there gonna be a lot of parameters passing
around this way.

If it is a problem, should I just let each webcontrol create their own
instance of the class? Will the performance be a lot worse this way? (the
class includes quite a few method functions).

Thanks.
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
David,

Generally speaking, if a class has a number of methods on it, it's not
going to impact performance as much as the data contained in instances of
the class. For example, having 10 methods on a class isn't the same as
having an array with 2048 elements in it (you pick the type, it still gets
large).

As for your singleton, what kind of contextual data are you trying to
store? I think this will help determine what you need to do. If the
context is the current user of your web application, then I would create an
instance and place it in the Session, and then it will be around for as long
as the server process is processing requests from that user.

If it is another kind of context, then you have to make that
determination (when a new one is created, etc, etc), and manage that
yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"davidw" <da****@affinisys.com> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
As I asked in last post, I want to put some logic in a object and all my
webcontrol instance will access that object, the object is responsed to
retrieve data from database if the data has not been retrieved yet. Vadym
Stetsyak suggested me to use singleton, it seems a good solution for me, but after I read more about singleton, I find another issue - if I use singleton patter, I can not store context data in the class anymore. How I used the
class is that I assigned required data to the class's variables, and then I called methods in the class to get result. If I use singleton pattern, how
should I tell the called method what are those context data? Should I pass
all them by parameters? It seems there gonna be a lot of parameters passing around this way.

If it is a problem, should I just let each webcontrol create their own
instance of the class? Will the performance be a lot worse this way? (the
class includes quite a few method functions).

Thanks.

Nov 15 '05 #2

P: n/a
Thanks.

I think the number of method doesn't matter too. In my case, the class need
read style data to decide how to display the webcontrol, let's assume there
are 100+ styles, when one user use one style, and the style data is not in
the class, it will read it from database and keep it for other users' call.
So I am looking for a centrol place to maintain the style data, doesn't that
make sense?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:e9*************@TK2MSFTNGP11.phx.gbl...
David,

Generally speaking, if a class has a number of methods on it, it's not
going to impact performance as much as the data contained in instances of
the class. For example, having 10 methods on a class isn't the same as
having an array with 2048 elements in it (you pick the type, it still gets
large).

As for your singleton, what kind of contextual data are you trying to
store? I think this will help determine what you need to do. If the
context is the current user of your web application, then I would create an instance and place it in the Session, and then it will be around for as long as the server process is processing requests from that user.

If it is another kind of context, then you have to make that
determination (when a new one is created, etc, etc), and manage that
yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"davidw" <da****@affinisys.com> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
As I asked in last post, I want to put some logic in a object and all my
webcontrol instance will access that object, the object is responsed to
retrieve data from database if the data has not been retrieved yet. Vadym Stetsyak suggested me to use singleton, it seems a good solution for me, but
after I read more about singleton, I find another issue - if I use

singleton
patter, I can not store context data in the class anymore. How I used the class is that I assigned required data to the class's variables, and then I
called methods in the class to get result. If I use singleton pattern,

how should I tell the called method what are those context data? Should I pass all them by parameters? It seems there gonna be a lot of parameters

passing
around this way.

If it is a problem, should I just let each webcontrol create their own
instance of the class? Will the performance be a lot worse this way? (the class includes quite a few method functions).

Thanks.


Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.