저는 C#을 처음 사용 했으므로 쉽게 제게 가보 십시 오.속성을 사용할 때 현재 세션에서 값을 얻으려고 시도합니다.
나는 몇 일 동안 나를 붙잡고있는 중요한 문제가있다.
문제 : 우리는 웹 응용 프로그램이 있고, 문서를 열 때, 모델의 모든 값이 세션에서 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));
}
}
}
) 메소드 SaveValues을 (호출하여 MVC4를 사용 }
다음과 같은 클래스 설정이 있습니다.
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]}
이 가진 문제는 내가 그들이 propertys로 저장됩니다 같은 사람이 주위에 방법을 제안 할 수 있습니다 .... 값을 찾을 수없는, 값을 얻을 변경된 데이터를 비교하는 데 사용하는 코드? 값을 저장할 때마다 각 클래스를 반복하고 각 클래스의 모든 값을 백업 저장소에 추가 할 수 있으므로 속성으로 저장되는 값이 중지됩니다.
코드는 백업 저장소에서 값을 얻을 수행 할 수있는 비교
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.
여기에있는 기본 접근 방식을 심각하게 재검토 해 보겠습니다 ... MVC는 상태를 유지하지 않으려 고합니다.이 스트림에 대해 수영하고 있습니다. 또한 명명 충돌에 심각한 문제가 있습니다. –
무엇을 제안 할 수 있습니까? –
글쎄, 그건 당신의 애플 리케이션을 재 설계하는 것입니다 ... 왜 그 모든 상태를 유지해야한다고 생각합니까, 그렇다면 왜 MemCache를 사용하지 않습니까? ORM이 무엇입니까? –