Should I create my object with singleton pattern? thanks.

D

davidw

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
D

davidw

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 said:
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]
- (e-mail address removed)

davidw said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top