English 中文(简体)
适用于XML的飞机——私人财产
原标题:Serialize to XML - private properties
  • 时间:2009-08-28 13:49:14
  •  标签:

I m 寻求一种办法,将含有一些只读的特性的POCO序列化。 在一些谷歌和StackOverflow搜索中,我看到了以下建议:

  • use DataContractSerializer; or
  • use SoapFormatter or BinaryFormatter; or
  • replace my readonly properties by read/write properties;

我的班子非常简单,他们喜欢:

public class MyClass
{
    public int Id { get; private set; }
    public string Name { get; private set; }
    public MyClass(int id, string name)
    {
        Id = id;
        Name = name;
    }
}

因此,

  • I don t want to make my properties read/write. If they are read-only, it s because my domain model asks for read-only properties. The domain model cannot change just because of this.
  • I don t want to use DataContractSerializer, as this would pollute my domain model with serialization-related stuff.
  • BinaryFormatter is not a very good option, as the result is a byte[], and I would like to treat it as string (and I don t want to deal with Encondings and alike when Deserializing my object).

我真的喜欢的是。 能够编篡只读物的XmlSerializer类别

你们是否知道这种执行? 或任何其他方便的解决办法?

感谢!

最佳回答

通常,XmlSerializer ,可按读物序号......然而,有可能将具有内部一套特性的特性序列化:你需要生成XML序列化组,并用宣布其为“朋友”组。 您可以在项目档案中增加以下代码,从而实现自动化:

  <Target Name="AfterBuild"
          DependsOnTargets="AssignTargetPaths;Compile;ResolveKeySource"
          Inputs="$(MSBuildAllProjects);@(IntermediateAssembly)"
          Outputs="$(OutputPath)$(_SGenDllName)">
    <SGen BuildAssemblyName="$(TargetFileName)"
          BuildAssemblyPath="$(OutputPath)"
          References="@(ReferencePath)"
          ShouldGenerateSerializer="true"
          UseProxyTypes="false"
          KeyContainer="$(KeyContainerName)"
          KeyFile="$(KeyOriginatorFile)"
          DelaySign="$(DelaySign)"
          ToolPath="$(SGenToolPath)">
      <Output TaskParameter="SerializationAssembly"
              ItemName="SerializationAssembly" />
    </SGen>
  </Target>

在议会中:

[assembly: InternalsVisibleTo("MyAssembly.XmlSerializers")]

当然,你可能不想让这些财产有一个内部结构,但如果你这样做的话,上述解决办法应当行之有效。

问题回答

虽然很不幸,如果序列化能够进入私人财产,那么它就会是空洞的。

But there is another option in the way of an architecture solution. Do NOT destroy your business domain requirements, instead seperate your layers similar to a a nTeir design and implement DTO s...

If you seperate your business, datafacade/dataadaptor (factory pattern fits well here) and DataAccess layers into 3 projects you can control through referencing that business never know about your DTO s. Hense if you decided to delete or implement the serialization or swap it to saving to SQL server you would not affect anything in your business layer.

There is always one downfall, there is a bunch more code to write: * you have to write a object convertor both ways for each entity you wish to go to Dataaccess * you potentially destroy some of the OO hiding sinse a .Save method in business will need to be translated to the correct type in Dataface before moving on down to dataaccess

You can make this a whole lot more easy with something like nHybinate or similar. Cheers Choco





相关问题