Session properties and methods

Hi,

I have read below that Modules should not be used for web apps as properties become global to multiple end users using the app at the same time, which obviously can cause disastrous issues.

Modules vs Classes - Getting Started - Xojo Programming Forum

So I am now wondering if there are best practices on when to have properties/methods in a WebPage and when to have properties/methods in a Session? I understand that in both cases, they are session aware so I don’t have to worry about things going wrong when there are multiple active users. Is it as easy as saying, if there is only one WebPage in the app, it doesn’t matter where the properties and methods go? Or is it always best to put them in sessions if there’s a chance they’d be needed by other WebPages in future?
And is there anything that should never be put in Sessions?

Any advice on how I should organise properties and methods between WebPages and Sessions would be appreciated.

You are correct in your assumptions.

If you want any WebPage in your app to have access to something specific to the user, then it should be a property or method of the session.

However, each WebPage might have methods and properties that are only needed on that WebPage. In that case I would put the methods and properties on that WebPage. I would not put them on the Session.

If you have a common method that might be used anywhere but is not specific to a user, put in a module ( or it could be on the App object, but generally you should stick with a module.)

1 Like

One consideration: if the data is kept in the Session, then it’s relatively easy for the App to iterate across all open Sessions and do something with the data.

See WebApplication — Xojo documentation

If the data is in a WebPage inside a Session, this becomes more difficult (but is not impossible).

The general rule about scope is to try and keep the scope of a variable or property as close to where it is used.

I would believe that in most cases, unless you need to access the property throughout several pages, a webpage property is preferable.

If, however, you want some kind of global access, a session property can be accessed from all webpages. Note that the session object is not accessible in some circumstances. For instance, accessing the session from a server side timer run event handler requires special steps, using WebSessionContext.

https://documentation.xojo.com/api/web/websessioncontext.html

1 Like

I would believe that in most cases, unless you need to access the property throughout several pages, a webpage property is preferable.

If I had 2 WebPage that were both public scope, can one WebPage access the other’s properties by prefixing by object name e.g. In a method belonging to WebPage1, could I reference WebPage2.SomeProperty? If so, then access isn’t a massive issue?

Why? It is the same as any other 2 public objects

Yes, you can do that. Just make sure the page has been instantiated before you try to access its properties. It is pretty much the same as windows in desktop.

1 Like

Just to be clear, it is possible to store properties in other places if you are really careful about it.

One way is to make a class which contains a property for each thing you want to store and then use a dictionary whose keys are session identifiers and values are instances of this class. When a session starts, you create a new class and add it to the dictionary, when the session closes, you remove it (or have a routine that periodically goes through the keys and removes any that don’t match a current s session ID.

2 Likes