web pages, instantiated classes, and parents

G

Guest

Not sure if I'm posting this in the right spot, but....

Lets say i have a class for data access that gets instantiated on each page
in a web application ( .Net 1.1 )

From inside that class, how can I get a reference to the page that
instantiated it?

I'm assuming there is something in system.reflection, but I'm not familiar
with reflection enough to figure out what to use.
 
R

Robbe Morris [C# MVP]

You don't. You'd have to pass that in as a parameter
in the constructor (or some method in the class).

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp
 
G

Guest

There is no way to get it?

I can change the New to get the information, but I didn't really want to
have to change all the pages that instantiate the class.
 
E

Eliyahu Goldin

You can pass the reference to the page in a session variable and access it
from a class as HttpContext.Current.Session["myPage"].

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
 
R

Robbe Morris [C# MVP]

Yes, could definitely do this but it would be bad design
practice to incorporate the HttpContext in a data access layer
class. What happens when you need to deploy that data access
layer in a non-web environment?

The bigger question for me is what value is there to telling
the data access layer which class instantiated it or in what
environment it was called from. Typically, you'd write
separate methods to perform this sort of logging
as an http handler or via some other method triggered
from the user interface.

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp





Eliyahu Goldin said:
You can pass the reference to the page in a session variable and access it
from a class as HttpContext.Current.Session["myPage"].

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


pirho said:
There is no way to get it?

I can change the New to get the information, but I didn't really want to
have to change all the pages that instantiate the class.
 
E

Eliyahu Goldin

Well, you can almost always tell if you are in a web application by checking
HttpContext.Current on null.

I am not going to disagree with you on your points. I just think we should
sometimes give directions how to achieve a goal in a certain way even if we
would not have taken this way ourselves.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Robbe Morris said:
Yes, could definitely do this but it would be bad design
practice to incorporate the HttpContext in a data access layer
class. What happens when you need to deploy that data access
layer in a non-web environment?

The bigger question for me is what value is there to telling
the data access layer which class instantiated it or in what
environment it was called from. Typically, you'd write
separate methods to perform this sort of logging
as an http handler or via some other method triggered
from the user interface.

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp





Eliyahu Goldin said:
You can pass the reference to the page in a session variable and access
it from a class as HttpContext.Current.Session["myPage"].

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


pirho said:
There is no way to get it?

I can change the New to get the information, but I didn't really want to
have to change all the pages that instantiate the class.

:

You don't. You'd have to pass that in as a parameter
in the constructor (or some method in the class).

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp





Not sure if I'm posting this in the right spot, but....

Lets say i have a class for data access that gets instantiated on
each
page
in a web application ( .Net 1.1 )

From inside that class, how can I get a reference to the page that
instantiated it?

I'm assuming there is something in system.reflection, but I'm not
familiar
with reflection enough to figure out what to use.
 
G

Guest

ok, maybe some more information.

This is an existing application.

It is a web based catalog editor for an etailer.

When it was written, there were perhaps 5 employees in the company. Over a
hundred now.

There were perhaps 2 people who initially entered data into the catalog.
I'm not sure how many there are now, must over 10 people. There are about to
be another 10 from a data entry company.

There was no authorization. There is now as I just finished that portion.

There is no auditing, that is what I'm working on now.

I've decided to binary serialize any business objects to store them in SQL
image fields, got that working ( at least until those objects change, but
I'll worry about that later. Most of the time, the auditing needs will be "
what happened in the last two days" )

Now, security is done through a "BasePage" that checks whether a person can
see the current page.
All pages inherit from BasePage.

All pages instantiate a DAL object, and call methods to do any SQL access.

I need to audit any Insert, Delete, Change actions.
Part of the audit is Who did the change, which the BasePage has a property
for.
( Using integrated auth )

Thus, I need access to the Calling/Instantiating object, not for the Page
context, but for what the page object contains.

If I really MUST change all the calls, well, I'll figure out some non-tying
way to do it, but I didn't want to find and change them all because this
WOULD tie my DAL to the pages.

There is no way to do this in 1.1?

Cripes, I hope they put it in for 2.0 I'll have to go look.

Eliyahu Goldin said:
Well, you can almost always tell if you are in a web application by checking
HttpContext.Current on null.

I am not going to disagree with you on your points. I just think we should
sometimes give directions how to achieve a goal in a certain way even if we
would not have taken this way ourselves.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Robbe Morris said:
Yes, could definitely do this but it would be bad design
practice to incorporate the HttpContext in a data access layer
class. What happens when you need to deploy that data access
layer in a non-web environment?

The bigger question for me is what value is there to telling
the data access layer which class instantiated it or in what
environment it was called from. Typically, you'd write
separate methods to perform this sort of logging
as an http handler or via some other method triggered
from the user interface.

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp





Eliyahu Goldin said:
You can pass the reference to the page in a session variable and access
it from a class as HttpContext.Current.Session["myPage"].

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


There is no way to get it?

I can change the New to get the information, but I didn't really want to
have to change all the pages that instantiate the class.

:

You don't. You'd have to pass that in as a parameter
in the constructor (or some method in the class).

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp





Not sure if I'm posting this in the right spot, but....

Lets say i have a class for data access that gets instantiated on
each
page
in a web application ( .Net 1.1 )

From inside that class, how can I get a reference to the page that
instantiated it?

I'm assuming there is something in system.reflection, but I'm not
familiar
with reflection enough to figure out what to use.
 
D

Damien

pirho said:
ok, maybe some more information.

This is an existing application.

It is a web based catalog editor for an etailer.

When it was written, there were perhaps 5 employees in the company. Over a
hundred now.

There were perhaps 2 people who initially entered data into the catalog.
I'm not sure how many there are now, must over 10 people. There are about to
be another 10 from a data entry company.

There was no authorization. There is now as I just finished that portion.

There is no auditing, that is what I'm working on now.

I've decided to binary serialize any business objects to store them in SQL
image fields, got that working ( at least until those objects change, but
I'll worry about that later. Most of the time, the auditing needs will be "
what happened in the last two days" )

Now, security is done through a "BasePage" that checks whether a person can
see the current page.
All pages inherit from BasePage.

All pages instantiate a DAL object, and call methods to do any SQL access.

I need to audit any Insert, Delete, Change actions.
Part of the audit is Who did the change, which the BasePage has a property
for.
( Using integrated auth )

Thus, I need access to the Calling/Instantiating object, not for the Page
context, but for what the page object contains.

If I really MUST change all the calls, well, I'll figure out some non-tying
way to do it, but I didn't want to find and change them all because this
WOULD tie my DAL to the pages.

But the way you're trying to do it, the DAL has to have knowledge of
the class structure of your website, and surely a dependency in that
direction is even worse?
There is no way to do this in 1.1?

Cripes, I hope they put it in for 2.0 I'll have to go look.

I doubt you'll find anything. It's not something that *most* or even
*the majority* of developers will ever need, so why incur the
substantial overheads that would be required, just so that one or two
developers can write a bit less code.

My recommendation would be to create a new class in your DAL called
SecurityContext (or something similar). It contains each field which
the DAL needs for it's purposes. Then in your base page, you create a
function that returns an instance of SecurityContext, populated with
all the relevant details. Finally, you make the changes you need so
that all your calls into the DAL include a SecurityContext as a
parameter.

Yes, you'll have to change the existing code, but thereafter, if you
decide you need more details coming through to the DAL, you should only
have to change the SecurityContext class and the
GetDALSecurityContext() function in your base page.

And half of the rewriting should be quite quick - once you've edited
the functions within the DAL, a compile will find all of the locations
where there are calls into the DAL, and if you have ",
Me.GetDALSecurityContext()" in the clipboard, you can just click on
each error, press End, back-arrow, Ctrl-V, and move onto the next.

Damien
 
G

Guest

Well, the dal implements the class structure for CRUD.
Sure, there are some other methods, but I won't need to audit them.

I took the cheap route and simply added a property to the class, and I
changed the New function to require passing a string of the username.
If I need to pass more information later, I'll make a new class.

Oh, when I use the term dal for this application, it isn't a real dal
it is one monolithic class that simply contains methods to do the data access.

It already knows and uses the class structure of the website. I just didn't
want to make any more dependencies within the existing framework of what I
must work with.

Thanks for all the help to everyone though. It is much appreciated.

I guess I was looking for a shortcut to help me continue with a
"problematic" application.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top