每次都使用会话获取/设置对象属性
本文关键字:设置 对象 属性 获取 会话 | 更新日期: 2023-09-27 18:33:10
我尝试搜索这个,但我什至不确定如何措辞搜索。
我正在尝试做的是拥有一个类,每次我访问它以更改它时,我实际上都会从会话中获取并设置值。
这是我正在尝试做的事情(到目前为止我所拥有的)。
public class example
{
public int prop1 {get;set;}
public static example Instance
{
return (example)(HttpContext.Current.Session["exampleClass"] ?? new example());
}
}
public class main
{
protected void Page_Load(object sender, EventArgs e)
{
example.Instance.prop1 = "aaa"; //stores value into session
txtInput.Text = example.Instance.prop1; //retrieves value from session
}
}
我希望这对我正在努力做的事情是有意义的。
任何帮助将不胜感激,谢谢。
这很容易用泛型来实现。
试一试。
public class Session
{
public User User
{
get { return Get<User>("User"); }
set {Set<User>("User", value);}
}
/// <summary> Gets. </summary>
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <param name="key"> The key. </param>
/// <returns> . </returns>
private T Get<T>(string key)
{
object o = HttpContext.Current.Session[key];
if(o is T)
{
return (T) o;
}
return default(T);
}
/// <summary> Sets. </summary>
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <param name="key"> The key. </param>
/// <param name="item"> The item. </param>
private void Set<T>(string key, T item)
{
HttpContext.Current.Session[key] = item;
}
}
看起来你已经很接近了,但你没有任何东西可以实际在会话中存储对象。尝试这样的事情:
public static Example Instance
{
get
{
//store the object in session if not already stored
if (Session["example"] == null)
Session["example"] = new Example();
//return the object from session
return (Example)Session["example"];
}
}
这基本上只是单例模式的 Web 友好实现。
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
public static class ExampleSession
{
private static HttpSessionState session { get { return HttpContext.Current.Session; } }
public static string UserName
{
get { return session["username"] as string; }
set { session["username"] = value; }
}
public static List<string> ProductsSelected
{
get
{
if (session["products_selected"] == null)
session["products_selected"] = new List<string>();
return (List<string>)session["products_selected"];
}
}
}
你可以像这样使用它:
public class main
{
protected void Page_Load(object sender, EventArgs e)
{
//stores value into session
ExampleSession.UserName = "foo";
ExampleSession.ProductsSelected.Add("bar");
txtInput.Text = ExampleSession.UserName; //retrieves value from session
}
}
public class example {
public int prop1 { get; set; }
public static example Instance {
var exampleObject = (example)(HttpContext.Current.Session["exampleClass"]
?? new example());
HttpContext.Current.Session["exampleClass"] = exampleObject;
return exampleObject;
}
}
如果需要,您可以进一步优化它
如果您正在寻找一种更面向对象的会话方式,那么这里是在下面执行此操作的好方法。
用户会话类
[Serializable()]
public class UserSession
{
private CurrentRecord _CurrentRecord;
public CurrentRecord CurrentRecord
{
get
{
if ((_CurrentRecord == null))
{
_CurrentRecord = new CurrentRecord();
}
return _CurrentRecord;
}
set
{
if ((_CurrentRecord == null))
{
_CurrentRecord = new CurrentRecord();
}
_CurrentRecord = value;
}
}
}
全局类
public static class Globals
{
public static UserSession TheUserSession
{
get
{
if ((HttpContext.Current.Session["UserSession"] == null))
{
HttpContext.Current.Session.Add("UserSession", new CurrentUserSession());
return (CurrentUserSession)HttpContext.Current.Session["UserSession"];
}
else
{
return (CurrentUserSession)HttpContext.Current.Session["UserSession"];
}
}
set { HttpContext.Current.Session["UserSession"] = value; }
}
}
当前记录类
[Serializable()]
public class CurrentRecord
{
public int id { get; set; }
public string name { get; set; }
}
代码隐藏中的用法
public void SetRecordId(int newId)
{
Globals.TheUserSession.CurrentRecord.id = newId;
}