2

FreezeAllAccountsForUser関数を使用して次のXMLを生成しました。この関数では、次の2つの列のみをシリアル化する必要があります。シリアル化を2列に制限するにはどうすればよいですか?

  1. BankAccountID
  2. 状態

注:重要な点は、この制限を行う範囲は、関数FreezeAllAccountsForUser内にのみある必要があるということです。グローバルではありません

参照

  1. 派生クラスを含むIEnumerableのシリアル化:循環参照の問題

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";
    }
}  
}

LINQtoSQLによって自動生成されたクラス

[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
4

1 に答える 1

8

This is going to be vexing to hear (I know you've been bouncing between the two serializers), but: XmlSerializer does support that, in two ways: a) by using XmlAttributeOverrides to specify the attributes at runtime, and b) by way of "conditional serialization" (public bool ShouldSerializeFoo() for member Foo). DataContractSerializer supports neither of these. Different serializers: different features.

My advice: stop trying to fit serialization into your domain model. That can work, but the moment it gets messy stop fighting it, and create a separate DTO model that is simple, designed to be serialized as it's primary purpose, and just map between the domain model and the DTO model as required.

于 2012-07-03T10:36:34.970 に答える