Adding global objects to a web application

A

Andy B

I want to have page level access to a certain object but cant figure out how
to do this. I create a class method that returns the object, but I cant get
it to be global to the page. By global to the page, I mean to let all
methods and event handlers have access to the same instance of the object. I
looked at a global.asax file, but that doesn't seem to be what I am looking
for (since the object is supposed to be page wide not application wide). Any
ideas how I can do this?
 
J

Juan T. Llibre

Place your object's class in the App_Code folder,
or compile an assembly from the source class
and place it in the /bin directory of your application.
 
Joined
Apr 9, 2008
Messages
6
Reaction score
0
I would create an instance of the class in global.asax and then place the object in a Session. Then the object can be retrieved on every page.

Is this what you mean ?
 
A

Andy B

I have this code:
//this code works in the page class
//Create a ContractManager so we can have an object that will create, save,
convert the object and other things as well. This I would like to be web
application
//global. I.e. once the instance is created for the whole application, it
doesn't need to be recreated.
ContractManager Manager = new ContractManager();

//Once the ContractManager is created, I now need to create the object
itself. This requires the following code which wont work inside a page class
since it is
//executable code. This is where I run into huge problems since the
following object needs to be accessible by all of the methods/event handlers
of the class during
//its entire lifecycle.
//This creates the object, assigns default values and returns a Contract
object and assigns it to StockContract.
Contract StockContract = Manager.CreateEmptyStockContract();

Hope this helps with what I need.


Eliyahu Goldin said:
Perhaps I am missing something. Why don't you just declare a reference to
the oblect:

private MyClass myObject;

in the page class?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Andy B said:
I want to have page level access to a certain object but cant figure out
how to do this. I create a class method that returns the object, but I
cant get it to be global to the page. By global to the page, I mean to let
all methods and event handlers have access to the same instance of the
object. I looked at a global.asax file, but that doesn't seem to be what I
am looking for (since the object is supposed to be page wide not
application wide). Any ideas how I can do this?
 
Joined
Apr 9, 2008
Messages
6
Reaction score
0
Make ContractManager class serializable

Underneath ContractManager Manager = new ContractManager();
do the following:
Session["ContractManager"] = Manager;

When you want to do this:
Contract StockContract = Manager.CreateEmptyStockContract();

Do this:
if(Session["ContractManager"] != null)
{
Contract StockContract = ((ContractManager)Session["ContractManager"]).CreateEmptyStockContract();
}

If i'm not wrong somewhere that should do the trick..it's possible that I'm wrong of course :)
 
B

bruce barker

as long as you realize the page lifecycle is for only one request, as a new
page object is created per request, just create a class variable:

private Contract StockContract = Manager.CreateEmptyStockContract();

this will be available to all methods in the page class. if you need the
same instance on a postback, then you will need to come up up a way to
save/restore the object between postbacks. the session object can help here.


-- bruce (sqlwork.com)


Andy B said:
I have this code:
//this code works in the page class
//Create a ContractManager so we can have an object that will create, save,
convert the object and other things as well. This I would like to be web
application
//global. I.e. once the instance is created for the whole application, it
doesn't need to be recreated.
ContractManager Manager = new ContractManager();

//Once the ContractManager is created, I now need to create the object
itself. This requires the following code which wont work inside a page class
since it is
//executable code. This is where I run into huge problems since the
following object needs to be accessible by all of the methods/event handlers
of the class during
//its entire lifecycle.
//This creates the object, assigns default values and returns a Contract
object and assigns it to StockContract.
Contract StockContract = Manager.CreateEmptyStockContract();

Hope this helps with what I need.


Eliyahu Goldin said:
Perhaps I am missing something. Why don't you just declare a reference to
the oblect:

private MyClass myObject;

in the page class?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Andy B said:
I want to have page level access to a certain object but cant figure out
how to do this. I create a class method that returns the object, but I
cant get it to be global to the page. By global to the page, I mean to let
all methods and event handlers have access to the same instance of the
object. I looked at a global.asax file, but that doesn't seem to be what I
am looking for (since the object is supposed to be page wide not
application wide). Any ideas how I can do this?
 
A

Andy B

Contract StockContract = Manager.CreateEmptyStockContract();

Doesn't actually work in just a class. I get a compiler error saying that
this statement is invalid unless inside a method of some kind. I will need
the StockContract object available to all methods/event handlers and it will
have to be around during postbacks.


bruce barker said:
as long as you realize the page lifecycle is for only one request, as a
new
page object is created per request, just create a class variable:

private Contract StockContract = Manager.CreateEmptyStockContract();

this will be available to all methods in the page class. if you need the
same instance on a postback, then you will need to come up up a way to
save/restore the object between postbacks. the session object can help
here.


-- bruce (sqlwork.com)


Andy B said:
I have this code:
//this code works in the page class
//Create a ContractManager so we can have an object that will create,
save,
convert the object and other things as well. This I would like to be web
application
//global. I.e. once the instance is created for the whole application, it
doesn't need to be recreated.
ContractManager Manager = new ContractManager();

//Once the ContractManager is created, I now need to create the object
itself. This requires the following code which wont work inside a page
class
since it is
//executable code. This is where I run into huge problems since the
following object needs to be accessible by all of the methods/event
handlers
of the class during
//its entire lifecycle.
//This creates the object, assigns default values and returns a Contract
object and assigns it to StockContract.
Contract StockContract = Manager.CreateEmptyStockContract();

Hope this helps with what I need.


Eliyahu Goldin said:
Perhaps I am missing something. Why don't you just declare a reference
to
the oblect:

private MyClass myObject;

in the page class?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


I want to have page level access to a certain object but cant figure
out
how to do this. I create a class method that returns the object, but I
cant get it to be global to the page. By global to the page, I mean to
let
all methods and event handlers have access to the same instance of the
object. I looked at a global.asax file, but that doesn't seem to be
what I
am looking for (since the object is supposed to be page wide not
application wide). Any ideas how I can do this?
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top