使用属性时试图从当前会话获取值

本文关键字:会话 获取 属性 | 更新日期: 2023-09-27 18:20:48

我对C#很陌生,所以请对我宽容一点。

我有一个重大问题困扰了我好几天。

问题:我们有一个web应用程序,使用MVC4,当打开文档时,通过调用方法SaveValues(),在会话中的backingstore中创建模型中的所有值

public class NotifyPropertyChangedBase : INotifyPropertyChanged
{
    public Dictionary<string, object> BackingStore = new Dictionary<string, object>();
    public Dictionary<string, object> Changes = new Dictionary<string, object>();
    public bool HasChanges { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;

    public void SaveValues()
    {
        // Expensive, to use reflection, especially if LOTS of objects are going to be used. 
        // You can use straight properties here if you want, this is just the lazy mans way.
        this.GetType().GetProperties().ToList().ForEach(tProp => { BackingStore[tProp.Name] = tProp.GetValue(this, null); Changes[tProp.Name] = ""; });
        HttpContext.Current.Session["SbackingStore"] = BackingStore;

        HasChanges = false;
    }
    public void RevertValues()
    {
        // Again, you can use straight properties here if you want. Since this is using Property setters, will take care of Changes dictionary.
        this.GetType().GetProperties().ToList().ForEach(tProp => tProp.SetValue(this, BackingStore[tProp.Name], null));
        HasChanges = false;

    }
    public void OnPropertyChanged(string propName, object propValue)
    {
        // If you have any object types, make sure Equals is properly defined to check for correct uniqueness.
        if (HttpContext.Current.Session["SbackingStore"] != null)
        {
            if (propValue == null) propValue = "";
            BackingStore = (Dictionary<string, object>)HttpContext.Current.Session["SbackingStore"];

            if (BackingStore[propName].Equals(propValue))
            { }
            else
            {
                Changes[propName] = propValue;
                HasChanges = true;
            }

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

}

我有一个这样的类设置,它包含;

public class VisitViewModel : NotifyPropertyChangedBase
{
    public Activity ActivityVM { get; set; }
    public VBSSteps VBSStepsVM { get; set; }
    public ProductTime ProductTimeVM { get; set; }
    public OtherPST OtherPSTVM { get; set; }
    public TimeRange TimeRangeVM { get; set; }
}

属于上述VisitViewModel类的每个类都按照下面的示例进行编码。它们继承了NotifyPropertyChangedBase(我不会在这里发布太多的类信息);

public class Activity : NotifyPropertyChangedBase
{
    public int Id { get; set; }
    public string NotesID { get; set; }
    public string SubID { get; set; }
    public string Form { get; set; } // Form Name
    private string _custNumber;
    [MobileCRM.Resources.LocalizedString.LocalizedDisplayName("CustomerNumber")]
    [DataType(DataType.Text)]
    [Required]
    public string CustNumber
    {
        get { return _custNumber; }
        set { _custNumber = value; OnPropertyChanged("CustNumber", value); }
    }
    private string _companyName;
    [MobileCRM.Resources.LocalizedString.LocalizedDisplayName("CustomerName")]
    [DataType(DataType.Text)]
    [Required]
    public string CompanyName
    {
        get { return _companyName; }
        set { _companyName = value; OnPropertyChanged("CompanyName", value); }
    }
}

现在的问题是,在后备存储(会话)中创建的值如下所示(当我展开其中任何一个时,即ActivityVM,它包含我想要的密钥和值);

[0] {[ActivityVM, MobileCRM.Models.Activity]}
[1] {[VBSStepsVM, MobileCRM.Models.VBSSteps]}
[2] {[ProductTimeVM, MobileCRM.Models.ProdcutTime]}
[3] {[OtherPSTVM, MobileCRM.Models.OtherPST]}
[4] {[TimeRangeVM, MobileCRM.Models.TimeRange]}
[5] {[HasChanges, False]}

这方面的问题是,我用来获取值并比较更改后的数据的代码无法找到值,因为它们存储为属性。。。。有人能提出解决这个问题的办法吗?也许在保存值时,我可以循环遍历每个类,并将每个类中的所有值添加到后备存储中,从而停止将值保存为属性。

从后备存储中获取值并执行比较的代码(查看数据是否已更改)

public void OnPropertyChanged(string propName, object propValue)
{
    // If you have any object types, make sure Equals is properly defined to check for correct uniqueness.
    if (HttpContext.Current.Session["SbackingStore"] != null)
{
    if (propValue == null) propValue = "";
    BackingStore = (Dictionary<string, object>)HttpContext.Current.Session["SbackingStore"];

    if (BackingStore[propName].Equals(propValue)) // Errors here : gives The given key was not present in the dictionary.

使用属性时试图从当前会话获取值

您有以下代码:

public void SaveValues()
    {
        // Expensive, to use reflection, especially if LOTS of objects are going to be used. 
        // You can use straight properties here if you want, this is just the lazy mans way.
        this.GetType().GetProperties().ToList().ForEach(tProp => { BackingStore[tProp.Name] = tProp.GetValue(this, null); Changes[tProp.Name] = ""; });
        HttpContext.Current.Session["SbackingStore"] = BackingStore;

        HasChanges = false;
    }

所有类都使用此方法继承基类。因此,每当你在任何派生类上调用SaveValues方法时,HttpContext.Current.Session["SbackingStore"]就会被新的backingstore覆盖,这就是为什么你会得到"字典中缺少一个键"。