Re: Global datasets
- From: David Colliver <DavidColliver@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 16 Nov 2006 02:45:02 -0800
Thank you Dan, your answer is helping me a lot.
A lot of my learning is by example. Since I started this particular project,
I have learned so much, such as inheritance, overrides, bubble events and now
singleton and static. I still have a long way to go, but I suspect I will
still need to learn much more before I reach my goal. (This is a large
project and has a huge scope, which will hopefully take me from an
intermediate level C# programmer to an advanced level C# programmer).
With what I have learned just in this thread alone, I can apply to other
areas of the project, Folders will only need to be at page level (at this
point in time), but other areas will need to be at session and others at
application.
If I remove static, then that will make the life of my mObj expire on the
page level? That could get me round another problem. I have noticed that when
I refresh the page, I lose information after I have enumerated (I have
modified this code to IEnumerate), but have found that .Reset() will bring it
back again... I was looking at ways to run the reset within the class, but
couldn't make it work, but if removing static makes it work, then I am happy
again. (I don't want to have to force future programmers to call reset
everytime.)
I did have Folders as private, but I had problems making it work. I will try
again now that I have got further with it.
The way I thought of caching is the generally known way, such as page
caching, sticking items in session or application variables or into the
viewstate. None of these really appealed to me for the purpose of this.
Apologies if this has caused a wasted journey to the solution.
So, one more question, with what I have here in this code, if I make the
"Folders" static, and I make a change to the database, I need to somehow
update the static folders. How can I trigger the update?
Thanks and best regards,
Dave Colliver.
http://www.NottinghamFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available
"Dan Bass" wrote:
First thing I notice is your constructor for Folders should be private..
Otherwise you can delcare it outside of the singleton pattern and it breaks
the concept. By being private only SiteFolders can create a new instance.
The second thing you've encountered is the use of statics in ASP.Net... In a
windows form application, the use of static keeps the object in memory until
the application is closed. So if you have a static integer that acts as a
counter in an object, the object can be destroyed, and recreated, and the
value in that static will persist if it's not explicitly reset. But on the
web side of things, static's are used to store Application wide data. So if
one user came in and the object was created, then a second user browsed the
page, they'd be referencing the same data as the first.
Now you'll never guess what, but I've gone back to the starting point I made
in the first thread. You're looking more into cached objects, rather than
global, in that the object may be hidden away somewhere for set period
(whether the life of the page or life of the session) rather than a global
object that's about for the lifetime of the application. I've never been too
sure of the scope and duration of objects that are made static, and have
tended to move towards session objects that are persisted on a per user
basis...
So we've got 3 things to look at.
- Application wide data store... It's initialised typically in the
Application_Start event method and each user hooks into the same data
- Session wide data store... Each user has their own object, but it's
typically initialised at the start of the session when the user connects for
the first time, and expires with their session.
- Local data store... This comes back to having an object on the Page
object, that is then passed into each object that uses it (sounding like a
broken record player I suspect). This time the object is created typically
each time on a post back, and is expired once the postback cycle is
complete. So the life of the object is really a few seconds while the page
loads up the data.
How long do you need to keep your data for? If it's just for the cycle of a
page loading up, then 3's the answer I'm afraid.
Cheers
Dan.
"David Colliver" <DavidColliver@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:3A2028C1-EA41-49F5-9C87-AF63717F9367@xxxxxxxxxxxxxxxx
I think I may have something, based on a singleton (I think).
public class Folders
{
private static Folders mObj;
public Folders()
{
Response.Write("something");
}
public static Folders SiteFolders
{
get
{
if (mObj == null)
mObj = new Folders();
return mObj;
}
}
private int _FolderID;
public int FolderID
{
get { return _FolderID; }
set { _FolderID = value; }
}
}
In my page, I can do something like...
MyNS.Folders MyFolders = MyNS.Folders.SiteFolders;
MyFolders.FolderID = 20;
Response.Write(MyFolders.FolderID);
With my response.write in the constructor of the Folders class, I can see
that no matter how many times I call the class using the code above, the
constructor is only called once.
HOWEVER, I have also found that each time I refresh the page, the
constructor is never called again, unless I re-build the project. It seems
that the object persists in memory. Whilst this may not be a bad thing, I
do
need some way to kill this persistence, for example, if I add new folders.
How can I do that?
Also, can you see anything wrong with my code? I will continue with what I
have and see how far I can take it.
Thanks for your help.
Regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available
"Dan Bass" wrote:
David,
I'm afraid I've not looked into the MS CMS so can't relate to what you're
saying...
In your below example though, Channels needs to have been created
somewhere,
as you said. The reason you can use the code anywhere is because
CmsHttpContext obviously has global scope. It'll be global because it's
inherent to the context it is in, created at an earlier point, then
exposed
as a property. From this I'm understand your query a little more, I
suspect
there is some parent class here that is initialising these objects "under
the hood".
Basically, to get to the same point with the dataset, you WOULD follow
the
property route. Because you can't see that how the channel array is
populated, it doesn't mean that you got the "global-ness" (there's a new
one!) of the object for free... the framework outside what you saw did
this
work to give you access to this object.
Good Luck!
Dan.
"David Colliver" <DavidColliver@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
news:E3C8C941-BB4D-4B6F-ADD1-FE072FB98E93@xxxxxxxxxxxxxxxx
Hi,
Thank you so far...
I understand what you are saying here and I have done similar in the
past
(and done it the other way around, where the UC calls the property from
the
page instead of the page pushing the property).
Do you know Microsoft CMS? Here is what I am trying to achieve with
slight
differences.
Channels MyChannels = CmsHttpContext.Current.Channel.Channels;
foeach (Channel ch in MyChannels)
{
Response.Write ch.DisplayName;
}
Now, what I notice is that the MyChannels is not using a "New"
statement.
So
(I may be assuming wrong) what I assume is that the
CmsHttpContext.Current.Channel.Channels is already instantiated and
running.
I can quite simply use the lines above ANYWHERE in my code and it will
work.
No mucking about with properties, pushing datasets or anything like
that.
(obviously, there is a using clause as well).
To add to the confusion, I need to develop this at a slightly lower
level.
My page codebehind class is...
class page : MyOwnPageClass.Page
My own page class is...
class Page : System.Web.UI.Page
I use a similar sort of thing for my controls. This will allow any
developer
to create pages deriving from my page class with the functionality that
I
need to add to the page.
Coming back to singleton, I only want to create it once and use many,
but
I
am not sure of the scope of the singleton. At this point in time, I
want
the
page to create the menu (somehow) and be available for the life of that
page.
(doesn't have to survive refreshes, postbacks or anything, just the
instant
the page is running.)
Regards,
Dave Colliver.
http://www.Burton-on-TrentFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available
"Dan Bass" wrote:
David,
The singleton architecture was simply a way of having a "create once,
use
many" type scenario...
What you've said suddenly shed light on the situation, if you're
coming
from
an ASP background into this, then I can understand why you're trying
to
go
about what are. Unfortunately for you though, I'd still have to come
back
to
what I said before! ;-)
Typically I'd see it like this:
-------------------------- Page Psuedo Code
class myPage
{
DataSet myData = null;
void Page_Load (...)
{
if ( !Page.IsPostback )
{
if (myData == null)
{
// create and populate dataset here
}
// pass the data set we've created through to the controls
on
the page
// If the controls are loaded dynamically, then where ever
the
controls
// are loaded should contain this logic...
foreach (Control on the page)
{
eachControl.MyDataSet = myData;
}
}
} // end of Page_Load
// other page code
}
-------------------------- User Control Code
class myUserControl
{
// define this dataset locally and ensure it can be referenced as
a
property
DataSet myData = null;
public DataSet MyDataSet
{
get { return myData; }
set { myData = value; }
}
void Page_Load(...)
{
if ( myData != null )
{
// bind the DS to the control here... Note we should
already
have
// a valid instance here since the Page gave us a valid DS
before this
// method is executed.
}
} // end of Page_Load
// other control code
}
Hope that helps, and if not, keep probing, we'll get there. ;-)
Dan.
- Follow-Ups:
- Re: Global datasets
- From: Dan Bass
- Re: Global datasets
- References:
- Global datasets
- From: David Colliver
- Re: Global datasets
- From: Dan Bass
- Re: Global datasets
- From: David Colliver
- Re: Global datasets
- From: Dan Bass
- Re: Global datasets
- From: David Colliver
- Re: Global datasets
- From: Dan Bass
- Re: Global datasets
- From: David Colliver
- Re: Global datasets
- From: Dan Bass
- Re: Global datasets
- From: David Colliver
- Re: Global datasets
- From: Dan Bass
- Re: Global datasets
- From: David Colliver
- Re: Global datasets
- From: Dan Bass
- Re: Global datasets
- From: David Colliver
- Re: Global datasets
- From: Dan Bass
- Global datasets
- Prev by Date: Re: Could not find a part of the
- Next by Date: validateRequest can't be set to false when deploying with aspnet_compiler.exe
- Previous by thread: Re: Global datasets
- Next by thread: Re: Global datasets
- Index(es):
Relevant Pages
|