where to put global code?

  • Thread starter Thread starter mike.miller
  • Start date Start date
M

mike.miller

You could create a static class for the connection with
static properties for the connection object itself and
all of the associated properties. Just be aware that
only one object can use a connection at one time.

As far as the namespace, it depends on whether you will
want to reuse this class for other apps. The normal
naming convention is
CompanyName.MajorSystem.MajorClassification.Etc.
Although, I find it hard to create a new namespace for a
single class - unless the solution is big enough that it
would make less sense to stick it in some generic
namespace.
 
I would pass it by reference. It is generally cheaper,
as you pass a reference to the original object instead of
copying it. In some cases you could run into problems
because static values are shared across all instances, so
you just have to make sure that one piece of code won't
change something that will break something else.

With a connection object, you should be OK. Only one
object can use it at a time and you would want all
objects to be affected if you changed something at the
connection level. Hope this helps.
 
thanks,

what if i wanted common code to be used by other classes and didn't want it
shared across all instances of the class?

how would i do this?

thanks again.
 
Actually, I think I misspoke (wrote).

If the connection object itself is represented as a
static property, then there is no need to pass it at
all. Each class that needs it can just create an
instance of it. You would just need part of your code to
initialize the object first and set the connection
properties so that the individual objects don't have to.

You would pass by reference if you had an instance of a
regular class that you wanted to share. For example, if
you create an instance of the connection object and you
want several classes to use it, you can just create a
public connection property for the class and pass it in
by reference. That would accomplish what I wrote earler.

If you have code that you want to use across the
application, but each instance should own its own object,
you would just create a class with the appropriate public
properties and methods. Generally each class would
create its own instance. If the class had a bunch of
properties that need to be initialized a certain way
across the application, you might initialize it once and
then pass it by value to each class that needs it.
 
Back
Top