Keeping global stuff in a windows program.

  • Thread starter Thread starter Mufasa
  • Start date Start date
M

Mufasa

I have done lots of stuff with ASP.Net but am now starting to do stuff with
VB.Net. My question is how do you keep stuff around that is global? I mean
things like the user name and what not?

In ASP.Net you'd put it in to Session variables but there doesn't seem to be
anything like that in Windows.

I want to keep things like connections to databases, user info, ... all in
one place and have them persist (especially the DB connections because our
connection to the DB is slow so it is slow on the open and close.)

TIA - Jeff.
 
I have done lots of stuff with ASP.Net but am now starting to do stuff with
VB.Net. My question is how do you keep stuff around that is global? I mean
things like the user name and what not?

In ASP.Net you'd put it in to Session variables but there doesn't seem tobe
anything like that in Windows.

I want to keep things like connections to databases, user info, ... all in
one place and have them persist (especially the DB connections because our
connection to the DB is slow so it is slow on the open and close.)

TIA - Jeff.

Use a configuration file, probably an xml document that you can store
the information in and make part of your application.
This is also useful as you can edit it after installing the
application.
 
Mufasa said:
I have done lots of stuff with ASP.Net but am now starting to do stuff with
VB.Net. My question is how do you keep stuff around that is global? I mean
things like the user name and what not?

In ASP.Net you'd put it in to Session variables but there doesn't seem to
be anything like that in Windows.

ASP.NET with a WEB server is a stateless session between Web client and Web
server, meaning all connections between the client and the Web server are
broken and must be established between round trips between the client and
the server each time. So, one has to keep things in a session variable to
keep state with session information between round trips between Web client
and Web server, and it's only for that particular session and is not global.
I want to keep things like connections to databases, user info, ... all in
one place and have them persist (especially the DB connections because our
connection to the DB is slow so it is slow on the open and close.)

That maybe true for a Web application where multiple connections are being
opened by multiple users that are in session with the Web server, with the
Web server being the single machine that is initiating connections to the
DB for all users. And in addition, the DB admin for the DB server doesn't
know who to use connection pooling or the Web application is not using a
generic user-id and psw for all database connections taking advantage of
connection pooling.

Windows desktop solutions are stateful solutions running on a single
machine/workstation, meaning that as long as the application is running on
the single machine/workstation, it has state with all elements within its
scope of it being a running program and nothing is out of session, and its
never out of session. Connection pooling with a generic user id and psw
could be used their to, but most would use Windows Authentication for
desktop solutions.

As long as the connection is being made for each single machine/workstation,
then there is going to be no speed issues on open/close connections. You
could just leave the connection open until the program is ended, but you run
the risk of using up all available connections to the database server is the
user just left the program unattended and many users were doing it.

You want things to persist, like user info, database connection info etc,
etc, then one uses the app.config for Windows Desktop solutions, and you
keep reading the app.config, like one would use web.config for Web
solutions.
 
Thanks for the info but I already know about the configuration files.

I mean I want global things like definitions/global variables....

For instance - somebody logs in - I want to keep the user name they logged
in as somewhere so all of my modules/windows/whatever have access to it.
Writing it into the configuration files doesn't seem right. And I'm not
using Windows Authentication.

As far as the connections to the database, I'm not worried about running out
of connections. I'm more worried that if I close the DB and reopen it every
time I need to do that, it will be slow (our connection is to a DB that is
stored off site and the WAN to that DB is slow). So I'd like to leave the
connection open with logic to check to make sure the connection is open and
active. I understand that there will be issues with that but it will provide
a better user experience.

So for instance - Let's suppose a user logs in. I check to see what
privileges they have. Let's supposed I have a security module that does all
the checking and sets up things so I can later know what options they have.
But now what? All my other forms/modules don't have access to this instance
of the security module unless I pass it around which seems cumbersome. Do I
make the module static? Would that mean that everybody can access the module
and any changes one form makes everybody can see? So Form A logs the person
in. The Security Module loads all of their information and then
automatically Form B sees those changes?

TIA - Jeff.
 
Mufasa said:
Thanks for the info but I already know about the configuration files.

I mean I want global things like definitions/global variables....

For instance - somebody logs in - I want to keep the user name they logged
in as somewhere so all of my modules/windows/whatever have access to it.
Writing it into the configuration files doesn't seem right. And I'm not
using Windows Authentication.

You need to find out how to use the System.Security.Principle namespace,
System.Security.Thread and possibly role based security in .Net, look it up
use Google to find examples.
As far as the connections to the database, I'm not worried about running
out of connections. I'm more worried that if I close the DB and reopen it
every time I need to do that, it will be slow (our connection is to a DB
that is stored off site and the WAN to that DB is slow). So I'd like to
leave the connection open with logic to check to make sure the connection
is open and active. I understand that there will be issues with that but
it will provide a better user experience.

Some programmers like to leave the connection open until the application
closes, but that would mean that all the programming for connections would
be done at the UI, and all database activities would be done at the UI, not
a good thing IMHO. But of course that depends upon the situation.
So for instance - Let's suppose a user logs in. I check to see what
privileges they have. Let's supposed I have a security module that does
all the checking and sets up things so I can later know what options they
have. But now what? All my other forms/modules don't have access to this
instance of the security module unless I pass it around which seems
cumbersome. Do I make the module static? Would that mean that everybody
can access the module and any changes one form makes everybody can see? So
Form A logs the person in. The Security Module loads all of their
information and then automatically Form B sees those changes?

System.Security.Principle namespace, System.Security.Thread and possibly
role based security in .Net, look it up use Google to find examples.

If you add a Public Module called Global, then you can keep all the global
stuff there if you like, and at the form/UI level, everyone can see it.
 
Mufasa said:
Thanks for the info but I already know about the configuration files.

You were very polite here.
I mean I want global things like definitions/global variables....

The solution is 'static'. It works fine for me and my applications. No
one complains.

for example

namespace MyData
{
public static class Global
{
public const int WM_KEYDOWN = 0x0100;
public static User User;
public static Client Client;

public static Workflow WorkflowDefault = new Workflow "Default");
public static Workflow WorkflowA = new Workflow("A");
public static Workflow WorkflowB = new Workflow("B");
public static Workflow WorkflowC = new Workflow("C");
}
}

MH
 
Back
Top