Let's say that I need to use a list of data (in picker for example) in more then 2 views.
Is there a way to store the data of this list temporary somewhere in the memory and populate it on app start.
Is Preferences built for this, or it is just for storing app data, like JWT, ... (I saw for that is a special storage called SecureStorage).
thnx
you can define static method and propery and fill them while the project is standing up. If you want the data to be deleted and refilled in a certain time interval, you can use MemoryCache, there is more detailed information here https://learn.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-7.0
or you can store this data on Session https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-7.0
Related
I have a custom object that is referenced many times throughout a module in my system. Let me refer to it as CustomObj. To minimize the constant loading of this object from the DB I'd like to store it in Session in a collection of those objects. So I'd like to store a Dictionary of CustomObj where the key is the ID of the CustomObj. That way I can just check the Session if they key exists, then just reference that CustomObj over and over again, without the hit on the DB every time.
However, these CustomObjs can be updated by an Admin and their properties change. When that happens I'd like to broadcast down to the users connected to update that object in the dictionary to use the latest properties. Is there a built in process for doing this or would I need to implement some sort of broadcast and force an update via SignalR (I already have a hub setup for Facebook like notifications). Is Session the right place for this ?
The objects won't be updated extremely frequently or by alot of different Admins, but every once in a while the Admin will make a change to 1-5 properties, save, and the object is now different.
It's not an ideal way to store an object in a session that is being modified by different users. In your case, Caching should be the recommended solution.
I am a little confused as to how ASP.NET works. To my understanding, each time a webpage is created, it is an instance of the ASP.NET program. First of all, is this correct? For my website I have a class called 'Control' which inherits from System.Web.UI.Page, from which every other class (e.g. the aspx pages and their code behind pages) inherits. I need to maintain a list of customers etc. somewhere where it can be accessed by every user of the website (currently accessing it) and thought that this may be a good place, but if every user is accessing a different instance of the program, this list will be different for every user (as only they will be communicating with it).
If my thoughts are correct, to keep this list updated would I have to synchronize it in every instance of the program some how (possibly using threading)? Or would I have to connect to an external program which maintains this list? Or am I wrong about everything?
Thanks in advance, and sorry if this sounds like a load of nonsense; I am very confused!
Edit:
Thank you to all who have answered. I already have a database to which this data is being stored, but I also wanted to represent some of the data in the program.
I am making a booking system and have a big input form, and my plan is to load the data into objects (bookings, customers etc.) when it comes into the program (so that I don't lose the data during successive post backs), get these objects to write it to the database (it is a requirement of my client to write all data to the database as soon as it comes in to the program to minimize loss if the system goes down), and to then retain those objects software side as the program has to put constrains on what users can book (check that these services are available to them) and this would require some logic which would be easier with objects instead of having to back to the database a lot.
I therefore had the idea of storing this data in a place which was accessible to every website instance, and this is what I was confused about how to do.
It sounds like you are looking for the Cache property of the HttpContext class. The Cache shares data across the application domain, as opposed to the Items collection, which is per request. See msdn. Note that you will still need to store the data in a database as commented above.
You want to store your data in an external place like a database. Your application can then for every user load the data needed to display from the same database. Your application will grow and if you have to edit the data to a later point in time you already have all the needed pieces in place.
In our application we want to 'translate' labels. I don't want to hit the database multiple times for the same label. So when I already have fetched the term X I want to get this term from the cache.
Now I am thinking on how to implement this. I could think of this options:
Create a Singleton. So basically you are creating a public variable
Create a class with a static list on it which contains the cached translations.
What should you do?
I am using C# winforms.
Edit:
I don't mean lanquage translation, but term translation. Our customers have their own term for the same thing. So this is a setting in our application. Say you have term X, they can say, I'd like to call the Z. So everywhere in the application where X is used, Z must be displayed.
Every form has a few labels with (the same) terms. So the data itself is small (only one word), but it is possible that it goes into the database 20 times to get the terms for one form.
The ASP.NET HttpCache is also available outside ASP.NET. You could use this as a backing mechanism for your cache and access it trough a Singleton.
Another option if it fits your scenario could be a T4 template that would parse your database and generate a class at compile time. This is way faster that doing the database look up at run time. Only if something in your database changes you would have to rerun your T4 and deploy the new assembly.
why don't you try Application Settings. while i don't recommend it for large amount of data bcoz it tend to put a load on application and is not intented for storing large data but you may use it if your data volume is small .
Go to Project>Properties>Settings Tab and add a setting of type User of Type Dataset (Use Browse Option for more types) or something more closer to your needs.
In the Runtime assign or retrieve using
<datatype> obj=Properties.Settings.Default.<yoursettingname>;
I want to create a wizard that includes a few steps, that in the final steps
we need to include all the steps and save to the data base.
What is the best design to do this ?
Is there an implementation for jquery ?
Do I need to save the steps in session till the final save ?
If you will not go to the database on each wizard page, then you will have to use either sessions or cookies to store the data between page requests.
You can also use client side tricks that will utilize javascript and hidden frames to hold the data.
I have created a few wizard for www usage. If you need to create 2 or more wizards then it is better to create a simple wizard framework responsible for navigation and data storage in session.
In my case every wizard step implements IWizardStep interface responsible for temporary data storage, as well as some common information like step number, display strings, next & previous steps etc. Then I just put them together and the framework takes care of displaying them in order, navigation and processing.
I'm not that sure if this is what you need but the C# has a wizard control that is a good way to implement wizard type processes.
http://weblogs.asp.net/scottgu/archive/2006/02/21/438732.aspx
Else, If you want to implement your own flow, use session to capture the values per step and save it later on the last step. cheers..
I am currently working on a website that will have a high volume of traffic on it. The website has only 4-5 pages on it but I need to pass values selected on page 1 over to page 2 where more values are stored and so on until the user gets to page 5 where all values are passed to a third party system using XML.
Currently I use a dictionary object with roughly 20 keys stored in a single session object to hold the values across the different pages. I do this because some of the values are not just simple values like 'name' or 'age' values but can be complex like holding a dataset of results from an XML call on page 2.
I have read everywhere that global variables are bad so I am wondering if there is an alternative to using Sessions for this example? I cannot use a database to hold these values because of the way some of the values need to be stored (i.e. my variables are not just strings/ints). If I was to use global static variables how could I ensure the global variables are unique for each user?
Thanks for your thoughts.
Rich
** Edit 1 **
I am storing the current sessions in ASP.NET Session State and not inproc. I am just trying to figure out if what I am doing is bad practice or just generally accepted as an ok way of doing things. For example on the second page I get a XML result that I store as a dataset in my session for use on page 3/4 of my site. Should I be store/passing this info another way?
I cannot use a database to hold these
values because of the way some of the
values need to be stored (i.e. my
variables are not just strings/ints).
You actually can. You can serialise you object graph and store it as binary column in the database.
Additionally you can have this functionality out of the box and still using SessionState.
Here is an article on how to Store Session State in Sql Server.
So I would recommend to use Session but store the session state in the database.
Sessions are the right choice. The "global variables are bad" argument is a good one, but it's describing global variables, not session variables and pertains to how you organize data being passed among methods within your application. Storing data between pages can be done in only one of three ways:
Store the data in hidden fields or cookies (so everything gets passed back and forth for each page request). You could do this by just serializing your XML and other fancy data, but it sounds like a terrible idea.
Store the data in a database, flat file. This is a fine idea, but you'd still have to do the serializing work yourself.
Store the data in a session. This is exactly the same as option #2, except that C# is doing the serializing for you, and then saving the results in a flat file.
You do not have to do any work to separate the data of one user from another. Every visitor gets a unique session, and the data you store there won't ever be applied to another user (excluding a malicious attack such as a hijacked session).
I would think session is the appropriate place to put things that are relevant to your 'session'. It is true that over-using/abusing session can bite you with excessive memory consumption, but the alternatives can be a bit slower, just depending. Also, if you use SessionStateService or store the Session in SQL Server, you have slow-down in serializing/deserializing the session objects on each request.
You can use ViewState, but that means all of the data will get serialized to the HTTP Form, which means it will get passed back and forth on subsequent requests. Also, I think ViewState gets dropped from one ASP.NET form to the next, so unless you are on the same form, that won't work.
You can store the data in the database. One way is to have Session use SQL Server. If I were to use a database for this problem, I don't think I would use session in SQL Server though simply because if you store it yourself, then you could recover previously entered data.