Accessing properties of object stored in session - c#

I must be misunderstanding something about session. I'm trying to store some information, so let me give the details.
Here's my "container" class that holds a list of business objects. This container is stored in session.
public class MySessionContainer {
private IList<SomeBusinessObjectType> _BusinessObjectList = new List<SomeBusinessObjectType>();
public IList<SomeBusinessObjectType> BusinessObjectList {
get { return _BusinessObjectList; }
set { _BusinessObjectList = value; }
}
}
I have a set of pages that form a wizard/multi-step process, and they all need to access the list of business objects in the container, which is in session.
The first page adds business objects to the list in session, so this sort of code is used to achieve that:
string key = GetKeyForCurrentUser();
MySessionContainer container = (MySessionContainer) Session[key];
SomeBusinessObjectType businessObject = /* Get object from view. */;
container.BusinessObjectList.Add(businessObject);
The generated key is always the same across all pages.
However, when the user gets to the second page, the container is in session as expected but the list of business objects is empty which is not what I expect. If the first page adds a business object, it should be there for the second and subsequent pages... right?
Is there something I am not understanding about session in ASP.net? Why is the container in session but not the list? Is the list not serialized with the container object when ASP.net writes/reads it from session?

You need to write it back to the session.
Take a look at the example under Session Variables:
// When retrieving an object from session state, cast it to
// the appropriate type.
ArrayList stockPicks = (ArrayList)Session["StockPicks"];
// Write the modified stock picks list back to session state.
Session["StockPicks"] = stockPicks;
ASP Session State Overview

Session object is not shared across different users so GetKeyForCurrentUser is useless. you can use some simple string for key. objects stored in Session must be serializable thus SomeBusinessObjectType must be serializable too.

Initalizers can act unexpectedly when serializing an object graph. Try using lazy loaded properties, if you don't want to initialize the collection out of the parent class:
public class MySessionContainer {
private IList<SomeBusinessObjectType> _BusinessObjectList;
public IList<SomeBusinessObjectType> BusinessObjectList {
get { return (_BusinessObjectList ??
(_BusinessObjectList = new List<SomeBusinessObjectType>()) ; }
set { _BusinessObjectList = value; }
}
}
This will allow deserialization to set your list and any getters will never get a null value for your collection.
Also, make sure every object in the graph can be serialized. See here for more information.
This question may also hold some relevance to yours.

Related

Xamarin Pass Entry Value to new page from old

Hi i have a page where i get the user to fill out their fast name and last name and then when they hit the next button it performs an:
await Navigation.PushModalAsync(new VisitorHSAgreement());
to the next page, what I want to do is bring them values to display in a label on the next page I have currently tried this but it's coming back null can anybody help me.
public string VisitorFirstName { get; set; }
public string VisitorLastName { get; set; }
var visitorPage = new VisitorPage();
VisitorFirstName = visitorPage.FindByName<Entry>("FirstNameEntry").Text;
When on VisitorPage, there is an instance of the class VisitorPage. This instance contains controls, which are instances, too, and might have values set in their properties.
Now you navigating to VisitorHSAgreement and try to get a value from the VisitorPage by creating a new object of this class. Each object of this class comes with its own values and the controls are newly created. Unless you create some static field in VisitorPage you will never be able to access the values that are set in the first instance from the new instance. You should really read up on basic concepts of OOP, because this is really OOP 101.
I'd suggest to use MVVM along with the Prism library, this will really make your Xamarin.Forms life easier. Anyway, if you can't or don't want to at the moment (it's up to you in the end), there is still a solution. Since your properties are public, you can set them before navigating to VisitorHSAgreement
var page = new VisitorHSAgreement()
{
VisitorFirstName = FirstNameEntry.Text,
VisitorLastName = LastNameEntry.Text
};
await Navigation.PushModalAsync(page);
You need to learn how to work with Object Oriented programming.
As mentioned in the previous answer, you are trying to access empty values from a recently created object (which will obviously empty). While what you need to do is assign the values to the object you want to send, and then send that object (data filled in) to the page you want to use it.
Then in your page, receive that object sent from the previous page and explore its properties to obtain your values.

Reference to cache object

I'm using .NET C# for a project.
I have a list of products which I want to cache as they're used company wide. If the products drop out of cache I already know how to lock the cache and rebuild it ok as per the patterns on various authority/blog sites.
In my pages/user controls etc, I might grab a reference to the cache, like this:
var myCacheInstance = cachedProducts
However, I might also want to do something like this:
myCacheInstance.Add(new product(...));
Which will also update the cache as it's the same object.
I have 2 queries.
If I have a reference to the cached object is it guaranteed to remain in cache for the lifetime of my variable?
In the scanario outlined above, how do I go about ensuring integrity? I'm only planning on adding in this instance, but suppose, I was updating and deleting objects as well?
1) If I have a reference to the cached object is it guaranteed to
remain in cache for the lifetime of my variable?
If I right interpret this question: responce is no.
cache.Add("key", new object()); // ADD KEY
var obj = cache["key"]; // GET REFERENCE TO CACHED OBJECT
cache.Remove("key"); // REMOVE OBJECT FROM CACHE
obj.DoSomething(..); //PERFECTLY VALID, STILL WORK ..
2) In the scanario outlined above, how do I go about ensuring
integrity? I'm only planning on adding in this instance, but suppose,
I was updating and deleting objects as well?
Can add bool property like, for example:
public bool IsValid
{
get; private set;
}
when object removed this property is set from the class to false. Just example, iff it really fits your need can tell us only you.
Do not pass around a reference to your cache!
Use an object for your cache and if a clients wants to have the cached items return a new list of your cached items, or a readonly collection.
If you want to add items to the cache, use a method on the cache object and in that method lock the cache and add the item. Same with remove.
question 1: If you pass around references you can not guarantee anything.
question 2: Use an object to cache all your items as I described above.
public class Cache
{
private List<Item> cachedItems = new List<Item>();
public void Add(Item item)
{
lock(cachedItems)
{
cachedItems.Add(item);
}
}
}
hello in order to ensure integrity, you must add key
Cache.Add("YourKey", yourValue)
here you can find helper for all operations
http://johnnycoder.com/blog/2008/12/10/c-cache-helper-class/
For duration or timeout you have this format, where you specify absoluteExpiration
public Object Add (string key, Object value, CacheDependency dependencies,
DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority
priority, CacheItemRemovedCallback onRemoveCallback)

Create property object on demand or in constructor?

What is the best way of initializing objects for properties without setters in C#?
For example I have property of type UserData and I can initialize it:
In constructor
In getter
private UserData _user;
public UserData User
{
get
{
return _user?? (_user= new UserData ());
}
}
Initialize field:
private UserData _user = new UserData()
I found few similiar threads:
Create an object in the constructor or at top of the class
C# member variable initialization; best practice?
But it is consideration between 1st and 3rd option - no one thinks about 2nd option - do you know way? From some time it is my preffered option to get objects, but I wonder if there are some cons that I don't know.
Could you tell me what is the best option and what problem could make use of 2nd option?
It all depends on what you want to do with it, so there is definite answer for that.
One difference between 1+3 and 2 is predictability.
With 1+3, you know exactly where your object is created and at which point during instantiation of your class. That can be desirable in some circumstances.
With 2, you depend on external influences (who accesses the property at which time) to initialize the field.
With the delayed creation in approach 2 (only create the object if needed), you could save some time when creating an object of the containing class.
If the UserData's creation takes a lot of time, like, when you have to query a database for it, you might want to delay its creation until really necessary. The object that contains the UserData object is constructed faster since it doesn't need to wait for the UserData object to be created. If the property isn't always accessed, you might even get to completely avoid creating a UserData instance.
If you're simply using plain data, initializing the backing field at its definition (if possible) is preferred:
// when you create constructor N+1, no worries about forgetting to set the value
private UserData _userData = new UserData();
public UserData User
{
get { return _userData; }
}
If you need initialization to be deferred, your best option is using Lazy<T>:
private Lazy<UserData> _userData = new Lazy<UserData>(() => new UserData());
public UserData User
{
get { return _userData.Value; }
}
The constructor for Lazy<T> contains overloads which can address your thread safety needs:
None: access from multiple threads is "undefined behavior"
PublicationOnly: the first thread to complete initialization "wins"
ExecutionAndPublication: locks ensure only one thread initializes the value
One issue with #2 is if the property could be accessed by multiple threads you could potentially create two copies of the UserData object. An additional consideration with #2 is if UserData is expensive to create you will be paying the cost of creating that object when the property is accessed rather than when the containing object is created. That may or may not be desirable depending on your use case.

shared asp.net object as static or cache

What is the best method of storing a shared object in asp.net? It will get called multiple times per request on every request. Ive been using these two methods but Id like to know if there is a better way. I refresh this object once an hour.
public static List<ResourceObject> SharedResources = new List<ResourceObject>()
//OR
public static List<ResourceObject> SharedResources
{
get
{
List<ResourceObject> _sharedResources = HttpContext.Current.Cache["RedirectRoutes"] as List<ResourceObject>;
if (_sharedResources == null)
{
_sharedResources = LoadNewSharedResource();
HttpContext.Current.Cache["RedirectRoutes"] = _sharedResources;
}
return _redirectRoutes;
}
set
{
HttpContext.Current.Cache["RedirectRoutes"] = value;
}
}
If your object is changing frequently (i.e. hourly as you mentioned) then you'll be best to use the cache as it will be able to take care of flushing for you (assuming you pass the correct parameters when adding the value to the cache). If you use a static value it will not be cleared out every hour automatically so you'd need to implement the check yourself.
If this is, as it seems, an object that needs to persist across requests, then this is a perfectly good and reasonable way to achieve it. You may want to put the cached version in a local variable if it is being accessed multiple times within one call, to save retrieving it from the cache each time.
Is there a specific issue with caching it like that that you are concerned about?

Application_End() cannot access cache through HttpContext.Current.Cache[key]

I want to be able to maintain certain objects between application restarts.
To do that, I want to write specific cached items out to disk in Global.asax Application_End() function and re-load them back on Application_Start().
I currently have a cache helper class, which uses the following method to return the cached value:
return HttpContext.Current.Cache[key];
Problem: during Application_End(), HttpContext.Current is null since there is no web request (it's an automated cleanup procedure) - therefore, I cannot access .Cache[] to retrieve any of the items to save to disk.
Question: how can I access the cache items during Application_End()?
If you want to get access to cache object before it will be disposed, you need to use somethink like this to add object to cache:
Import namespace System.Web.Caching to your application where you are using adding objects to cache.
//Add callback method to delegate
var onRemove = new CacheItemRemovedCallback(RemovedCallback);
//Insert object to cache
HttpContext.Current.Cache.Insert("YourKey", YourValue, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, onRemove);
And when this object is going to be disposed will be called following method:
private void RemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
//Use your logic here
//After this method cache object will be disposed
}
I strongly urge you to rethink your approach. You may want to describe specifics of what are you trying to do, so we might help you with that.
But if you are totally set on it, then you can simply save values on disk when you actually set them, i.e. your helper class would looks something like this:
public static class CacheHelper
{
public static void SetCache(string key, object value)
{
HttpContext.Current.Cache[key] = value;
if (key == "some special key")
WriteValueOnDisk(value);
}
}
You can access the cache through HttpRuntime.Cache when you don't have an HttpContext available. However, at Application_End, i believe the cache is already flushed.
The solution Dima Shmidt outlines would be the best approach to store your cached values. That is by adding your items to cache with a CacheItemRemovedCallback, and store the values to disk there.
As an alternative solution you could store the data in Application object (Application[key]) or simply create a static class and use it to keep your data within app - in this case the data would sill be available upon Application_End.

Categories