2013-07-03 3 views
0

저는 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. 
+0

여기에있는 기본 접근 방식을 심각하게 재검토 해 보겠습니다 ... MVC는 상태를 유지하지 않으려 고합니다.이 스트림에 대해 수영하고 있습니다. 또한 명명 충돌에 심각한 문제가 있습니다. –

+0

무엇을 제안 할 수 있습니까? –

+0

글쎄, 그건 당신의 애플 리케이션을 재 설계하는 것입니다 ... 왜 그 모든 상태를 유지해야한다고 생각합니까, 그렇다면 왜 MemCache를 사용하지 않습니까? ORM이 무엇입니까? –

답변

0

당신이 코드가 (데이터가 변경되었는지 확인합니다) :

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; 

    } 

모든 clases는 기본 클래스를 상속을 이 방법으로 따라서 HttpContext.Current.Session [ "SbackingStore"]은 새로운 배킹 스토어로 ovveriden을 얻습니다. 따라서 "사전에 키가 누락되었습니다"라는 이유가 파생 클래스에서 SaveValues ​​메소드를 호출 할 때마다 발생합니다.

+0

아니요, 죄송합니다. 올바르지 않습니다 ...... 문서가 열릴 때마다 백업 저장소를 삭제 한 다음 새 백업 저장소를 만듭니다.나는 위에서 언급 한 방식이 아닌 다른 방식으로 값을 저장하는 방법을 찾고있다 .... 값은 있지만, 각 속성에서 값을 얻는 방법을 모른다. –

관련 문제