473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Static Vs. Instance

I was discussing class architecture with one of the senior developers
at my new job and him and I have a similar idea on how to work with
data access and class libraries. That said, our implementations vary
slightly and I wanted to post the question to the .Net community to get
some feedback. So here is the issue:

When designing classes both the senior developer and I agree that data
access should be abstracted out from a business object class by way of
a "factory layer" that communicates between the data access layer and
the business class itself. The factory layer returns an instance of the
object requested based on an ID field used to retrieve the instance
info in the DB. I like to create static methods on the Class being
created that wrap a static factory class and return the object; where
as he believes in creating an instance of the factory that returns the
new object.

My Method (static):

MyWidget mwInstance = MyWidget.GetWidget(555);
//MyClass.GetWidget Wraps static method MyWidgetFactory.GetWidget(int
intWidgetID);

His Method(instance):

MyWidgetFactory mwfFactory = new MyWidgetFactory();
MyWidget mwInstance = mwfFactory.GetWidget(555);

I devised my method while working in a windows environment so I knew
that a limited number of users or processes is accessing the DB. I am
now working in a Web environment, which means "unlimited" users or
processes accessing this code. So, my questions are thus:

1) Is either method perferrable (Static vs. Instance)?
2) Will environment affect performance when creating new instances?
3) If so, which methodology will perform better in a web environment?

Cheers
Dinsdale

Aug 11 '06 #1
5 2203
Dinsdale <ru********@gmail.comwrote:

<snip>
1) Is either method perferrable (Static vs. Instance)?
2) Will environment affect performance when creating new instances?
3) If so, which methodology will perform better in a web environment?
The performance won't make much difference. There isn't very much to
choose between the two, but you might want to consider whether all
these factories could implement some interface which you might want to
use in a general way. At that point, creating instances (or obtaining
those instances in another way, eg from a map of type->factory) could
give you a benefit.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 11 '06 #2
"Dinsdale" <ru********@gmail.comwrote:
I was discussing class architecture with one of the senior developers
at my new job and him and I have a similar idea on how to work with
data access and class libraries. That said, our implementations vary
slightly and I wanted to post the question to the .Net community to get
some feedback. So here is the issue:

When designing classes both the senior developer and I agree that data
access should be abstracted out from a business object class by way of
a "factory layer" that communicates between the data access layer and
the business class itself. The factory layer returns an instance of the
object requested based on an ID field used to retrieve the instance
info in the DB. I like to create static methods on the Class being
created that wrap a static factory class and return the object; where
as he believes in creating an instance of the factory that returns the
new object.
I agree with the senior developer. I'll tell you why: static methods
that rely on state (such as your factory class in the background) are
basically just hiding global variables. If you ever have a situation
where you need to work with two factories side by side, all your code
will be tied to the static methods and will only work with one of them.
That'll lead to swapping the global state in and out as you toggle
between the two.
My Method (static):

MyWidget mwInstance = MyWidget.GetWidget(555);
//MyClass.GetWidget Wraps static method MyWidgetFactory.GetWidget(int
intWidgetID);

His Method(instance):

MyWidgetFactory mwfFactory = new MyWidgetFactory();
MyWidget mwInstance = mwfFactory.GetWidget(555);

I devised my method while working in a windows environment so I knew
that a limited number of users or processes is accessing the DB. I am
now working in a Web environment, which means "unlimited" users or
processes accessing this code.
That's not necessarily a showstopper, depending on what your factory is
serving. If the factory is tied to a client and you're using
asynchronous code, then you'll find it difficult to use static methods
like you are. The factory isn't tied to a client, or if you're not using
asynchronous I/O, it's possible - in the first instance, there's no
conflict to manage other than locking where necessary, and in the second
instance, you can use a static field marked [ThreadStatic] to hold the
factory that the static methods wrap.
So, my questions are thus:

1) Is either method perferrable (Static vs. Instance)?
When possible and easy, I prefer Instance, and I dislike the 'Singleton'
pattern. In fact, I've never understood why it appeals to people - it
always seemed like a bad idea to me, except in extremely limited
scenarios.

On the other hand, if it's important to deal with a 'context' of some
kind everywhere in your code, it can be worth it to make that context
available everywhere, rather than try to thread it through every call
path and data structure.
2) Will environment affect performance when creating new instances?
3) If so, which methodology will perform better in a web environment?
It depends on if you're using asynchronous code (in which case you'll
find it somewhat difficult to get the desired behaviour if it's tied to
a client, since you can come back in on a completely different thread)
and whether the state is per-client or shared across the server.

Hope that made sense.

-- Barry

--
http://barrkel.blogspot.com/
Aug 11 '06 #3
Thanks guys, lots of great info. Since there is no overriding need to
use a singleton approach and the code may need to support multiple
factories in the future, using an instance of the factory does seem to
be the logical answer.

Cheers
Dinsdale

Barry Kelly wrote:
"Dinsdale" <ru********@gmail.comwrote:
I was discussing class architecture with one of the senior developers
at my new job and him and I have a similar idea on how to work with
data access and class libraries. That said, our implementations vary
slightly and I wanted to post the question to the .Net community to get
some feedback. So here is the issue:

When designing classes both the senior developer and I agree that data
access should be abstracted out from a business object class by way of
a "factory layer" that communicates between the data access layer and
the business class itself. The factory layer returns an instance of the
object requested based on an ID field used to retrieve the instance
info in the DB. I like to create static methods on the Class being
created that wrap a static factory class and return the object; where
as he believes in creating an instance of the factory that returns the
new object.

I agree with the senior developer. I'll tell you why: static methods
that rely on state (such as your factory class in the background) are
basically just hiding global variables. If you ever have a situation
where you need to work with two factories side by side, all your code
will be tied to the static methods and will only work with one of them.
That'll lead to swapping the global state in and out as you toggle
between the two.
My Method (static):

MyWidget mwInstance = MyWidget.GetWidget(555);
//MyClass.GetWidget Wraps static method MyWidgetFactory.GetWidget(int
intWidgetID);

His Method(instance):

MyWidgetFactory mwfFactory = new MyWidgetFactory();
MyWidget mwInstance = mwfFactory.GetWidget(555);

I devised my method while working in a windows environment so I knew
that a limited number of users or processes is accessing the DB. I am
now working in a Web environment, which means "unlimited" users or
processes accessing this code.

That's not necessarily a showstopper, depending on what your factory is
serving. If the factory is tied to a client and you're using
asynchronous code, then you'll find it difficult to use static methods
like you are. The factory isn't tied to a client, or if you're not using
asynchronous I/O, it's possible - in the first instance, there's no
conflict to manage other than locking where necessary, and in the second
instance, you can use a static field marked [ThreadStatic] to hold the
factory that the static methods wrap.
So, my questions are thus:

1) Is either method perferrable (Static vs. Instance)?

When possible and easy, I prefer Instance, and I dislike the 'Singleton'
pattern. In fact, I've never understood why it appeals to people - it
always seemed like a bad idea to me, except in extremely limited
scenarios.

On the other hand, if it's important to deal with a 'context' of some
kind everywhere in your code, it can be worth it to make that context
available everywhere, rather than try to thread it through every call
path and data structure.
2) Will environment affect performance when creating new instances?
3) If so, which methodology will perform better in a web environment?

It depends on if you're using asynchronous code (in which case you'll
find it somewhat difficult to get the desired behaviour if it's tied to
a client, since you can come back in on a completely different thread)
and whether the state is per-client or shared across the server.

Hope that made sense.

-- Barry

--
http://barrkel.blogspot.com/
Aug 11 '06 #4
Dinsdale,

A static class is in my opinion not a part of OOP programming.

To explain this in another way than probably mostly is done.

In VBNet a Static class, which has in that the more describtive name Shared
class is nothing more than a module. You can use only Shared Classes or
Modules (they behave exactly the same) in your application.

The result is than a nice modulair written program (Here not meant in the
context of an end user modulair program).

Cor

"Dinsdale" <ru********@gmail.comschreef in bericht
news:11**********************@75g2000cwc.googlegro ups.com...
>I was discussing class architecture with one of the senior developers
at my new job and him and I have a similar idea on how to work with
data access and class libraries. That said, our implementations vary
slightly and I wanted to post the question to the .Net community to get
some feedback. So here is the issue:

When designing classes both the senior developer and I agree that data
access should be abstracted out from a business object class by way of
a "factory layer" that communicates between the data access layer and
the business class itself. The factory layer returns an instance of the
object requested based on an ID field used to retrieve the instance
info in the DB. I like to create static methods on the Class being
created that wrap a static factory class and return the object; where
as he believes in creating an instance of the factory that returns the
new object.

My Method (static):

MyWidget mwInstance = MyWidget.GetWidget(555);
//MyClass.GetWidget Wraps static method MyWidgetFactory.GetWidget(int
intWidgetID);

His Method(instance):

MyWidgetFactory mwfFactory = new MyWidgetFactory();
MyWidget mwInstance = mwfFactory.GetWidget(555);

I devised my method while working in a windows environment so I knew
that a limited number of users or processes is accessing the DB. I am
now working in a Web environment, which means "unlimited" users or
processes accessing this code. So, my questions are thus:

1) Is either method perferrable (Static vs. Instance)?
2) Will environment affect performance when creating new instances?
3) If so, which methodology will perform better in a web environment?

Cheers
Dinsdale

Aug 12 '06 #5
INLINE

"Dinsdale" <ru********@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
><SNIP>
So, my questions are thus:

1) Is either method perferrable (Static vs. Instance)?
Static is great for helper methods, like adding values together, returning a
value or set of values based on a particular input. Anything that does not
require instantiation.

In general, factories are not helpers (actually can't think of an instance
where you would not instantiate a factory, but I am sure there is at least
one).
2) Will environment affect performance when creating new instances?
Instance creation is more a scalability issue, but scalability issues are
not always solved by pushing to static and there are good reasons not to go
static for scalability (lengthy subject, little time).
3) If so, which methodology will perform better in a web environment?
Until the environment is taxed, neither is going to show a superior enough
performance to make a decision. Instead, you should make the decision on
whether there is a reason to short circuit a well-documented pattern. I am
not saying there is no reason to do this, as you may have come up with a
completely new pattern that is superior, but I cannot see the benefit of the
static method and keeping a factory alive forever. There might be an
instance where you stoke up the factory and it is so overused that it makes
sense to keep it up as a single instance.

Now, the bad news. If you code the factory method incorrectly, you can end
up pulling information from one instance into the incorrect instance, esp.
if you ever need to multi-thread for performance. Long story over.

Short story: Without knowing the specifics of your project, I would agree
with the senior developer, although I must admit I highly respect you for
asking before falling in line. A junior programmer willing to learn and
think for himself is a great asset. Rock on!

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside the box!
*************************************************
Aug 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
14
by: Jordan Marr | last post by:
I have the following class: class ProvisionCollection { ... private int m_VarianceCount; public int VarianceCount { get { return m_VarianceCount; }
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
5
by: Andy B | last post by:
I have a class that I want to make static but it uses some objects that are instance objects. I keep getting a compiler error saying something about using instance objects in a static class or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.