【问题标题】:DataContract Serialization without DataMember Attribute没有 DataMember 属性的 DataContract 序列化
【发布时间】:2012-07-03 18:32:39
【问题描述】:

我使用 FreezeAllAccountsForUser 函数生成了以下 XML。我只需要为此函数序列化以下两列。如何将序列化限制为两列?

  1. 银行账户ID
  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

【问题讨论】:

    标签: c# wcf linq-to-sql serialization datacontractserializer


    【解决方案1】:

    听到这会令人烦恼(我知道你一直在两个序列化程序之间弹跳),但是:XmlSerializer 确实支持它,有两种方式:a)使用 @ 987654322@ 在运行时指定属性,b) 通过“条件序列化”(public bool ShouldSerializeFoo() 成员 Foo)。 DataContractSerializer 都不支持这些。不同的序列化器:不同的功能。

    我的建议:停止尝试将序列化融入您的领域模型。这可以工作,但是当它变得混乱时停止战斗,并创建一个简单的单独 DTO 模型,设计为序列化因为它是主要目的,只需根据需要在域模型和 DTO 模型之间进行映射。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 2011-06-17
      • 2012-06-18
      相关资源
      最近更新 更多