2012-07-03 64 views
1

我使用函數FreezeAllAccountsForUser生成以下XML。我只需要序列化這個函數的以下兩列。我如何限制序列化爲兩列?無DataMember的DataContract序列化屬性

  1. BankAccountID
  2. 狀態

注:關鍵的一點是,使這種限制的範圍應該只在功能FreezeAllAccountsForUser; 不全球

參考

  1. Serializing IEnumerable Containing Derived classes: Circular Reference Issue

XML

<ArrayOfBankAccount xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="1" z:Size="1" 
       xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" 
       xmlns="http://schemas.datacontract.org/2004/07/DBML_Project"> 

    <BankAccount z:Id="2" i:type="FixedBankAccount"> 

<AccountOwnerID>2</AccountOwnerID> 
<AccountType z:Id="3">Fixed  </AccountType> 
<BankAccountID>2</BankAccountID> 

<BankUser z:Id="4"> 
    <BankAccounts z:Id="5" z:Size="1"> 
    <BankAccount z:Ref="2" i:nil="true" /> 
    </BankAccounts> 
    <Name z:Id="6">TestP1 </Name> 
    <UserID>2</UserID> 
    <UserType z:Id="7">Ordinary </UserType> 
</BankUser> 

<OpenedDate i:nil="true" /> 

<Status z:Id="8">FrozenFA</Status> 

    </BankAccount> 

</ArrayOfBankAccount> 

系列化

public class BankAccountAppService 
{ 
    public RepositoryLayer.ILijosBankRepository AccountRepository { get; set; } 

    public void FreezeAllAccountsForUser(int userId) 
    { 
     IEnumerable<DBML_Project.BankAccount> accounts = AccountRepository.GetAllAccountsForUser(userId); 
     foreach (DBML_Project.BankAccount acc in accounts) 
     { 

      string typeResult = Convert.ToString(acc.GetType()); 
      string baseValue = Convert.ToString(typeof(DBML_Project.BankAccount)); 

      if (String.Equals(typeResult, baseValue)) 
      { 
       throw new Exception("Not correct derived type"); 
      } 

      acc.Freeze(); 
     } 



     System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); 
     System.Xml.XPath.XPathNavigator nav = xmlDoc.CreateNavigator(); 

     using (System.Xml.XmlWriter writer = nav.AppendChild()) 
     { 
      System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(List<DBML_Project.BankAccount>), null, int.MaxValue, false, true, null); 
      serializer.WriteObject(writer, accounts); 
     } 

     xmlDoc.Save("C:\\DevTEST\\FileName.txt"); 



    } 

} 

域類

namespace DBML_Project 
{ 
[KnownType(typeof(FixedBankAccount))] 
[KnownType(typeof(SavingsBankAccount))] 
public partial class BankAccount 
{ 
    //Define the domain behaviors 
    public virtual void Freeze() 
    { 
     //Do nothing 
    } 
} 

public class FixedBankAccount : BankAccount 
{ 

    public override void Freeze() 
    { 
     this.Status = "FrozenFA"; 
    } 
} 

public class SavingsBankAccount : BankAccount 
{ 

    public override void Freeze() 
    { 
     this.Status = "FrozenSB"; 
    } 
} 
} 

自動生成的類由LINQ to SQL的

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.BankAccount")] 
[InheritanceMapping(Code = "Fixed", Type = typeof(FixedBankAccount), IsDefault = true)] 
[InheritanceMapping(Code = "Savings", Type = typeof(SavingsBankAccount))] 
public partial class BankAccount : INotifyPropertyChanging, INotifyPropertyChanged 

回答

7

這將是棘手的聽到(我知道你一直在這兩個串行之間彈跳),但:XmlSerializer確實支持即通過兩種方式:a)通過使用XmlAttributeOverrides在運行時指定屬性,以及b)通過「有條件序列化」(public bool ShouldSerializeFoo(),成員Foo)。 DataContractSerializer不支持這些。不同的序列化器:不同的功能。

我的建議:停止嘗試將序列化插入到您的域模型中。這可以工作,但它會混亂停止戰鬥吧,並創建一個單獨的DTO模式說起來很簡單,設計的那一刻被序列化因爲它的主要目的,只是域模型和DTO模式之間進行映射按要求。

相關問題