2016-03-03 29 views
0

我們在BOM中使用codefluent實體,在控制器中使用webapi,在客戶端使用angularjs Framework。關係中的序列化方法

當在父對象中存儲對象的引用時,我們正面臨一個問題。任何時候,引用都會被生成的代碼清空。

給定一個帶有關係ENTA [EntAId,PROP1,EntB]和EntB兩個實體[EntBId,PROP1,PROP2] 我結束了兩類:

class EntA{ 
    EntAId 
    prop1 
    EntB 
    EntBEntBId 
} 

class EntB{ 
    EntBId 
    prop1 
    prop2 
} 

CodeFluent已生成以下代碼:

 [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)] 
    [System.ComponentModel.DataObjectFieldAttribute(true)] 
    public System.Guid EntBEntBId 
    { 
     get 
     { 
      if (((this._EntBEntBId.Equals(CodeFluentPersistence.DefaultGuidValue) == true) 
         && (this._entB != null))) 
      { 
       this._EntBEntBId = this._entB.EntBId; 
      } 
      return this._EntBEntBId; 
     } 
     set 
     { 
      if ((System.Collections.Generic.EqualityComparer<System.Guid>.Default.Equals(value, this.EntBEntBId) == true)) 
      { 
       return; 
      } 
      this._entB = null; 
      this._EntBEntBId = value; 
      this.EntityState = CodeFluent.Runtime.CodeFluentEntityState.Modified; 
      this.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("EntB")); 
      this.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("EntBEntBId")); 
     } 
    } 

    [System.Xml.Serialization.XmlIgnoreAttribute()] 
    public Namespace.EntB.EntB EntB 
    { 
     get 
     { 
      if ((this._entB == null)) 
      { 
       this._entB = Namespace.EntB.EntB.Load(this._EntBEntBId); 
      } 
      return this._entB; 
     } 
     set 
     { 
      this._EntBEntBId = CodeFluentPersistence.DefaultGuidValue; 
      this._entB = value; 
      this.EntityState = CodeFluent.Runtime.CodeFluentEntityState.Modified; 
      this.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("EntB")); 
      this.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("EntBEntBId")); 
     } 
    } 

在客戶端(使用Angular)我wa nt在不發送整個EntB對象的情況下注冊objectA.EntBEntBId。因此我會考慮下面的這個片段來使EntB無效。

if(objectA.EntB) 
    objectA.EntB = null 

這最終發送正確的流到服務器(沒有整個B對象被序列化)。

當HTTP PUT調用被觸發時,webapi將首先評估首先經過get/set方法的類。屬性EntBEntBId將被正確評估,但是然後EntB屬性的setter將繼續擦除先前的值(因爲EntB當前爲空)。

有什麼辦法可以避免這種行爲?

在此先感謝您的回答;

回答

1

我可能會回答我自己的問題,但是當使用delete objectA.entB而不是objectA.entB = null Stream不會包含entB屬性,因此不會通過它的setter。

+0

似乎是一個很好的解決方案。也許你可以在刪除它的屬性之前克隆JavaScript對象。在使用angularjs時,可以使用[HttpInterceptor](https://docs.angularjs.org/api/ng/service/$http) – meziantou