C#城堡的ActiveRecord:如何优雅(XML)序列ActiveRecord对象? [英] C# Castle ActiveRecord: How to elegantly (XML) serialize ActiveRecord objects?

查看:356
本文介绍了C#城堡的ActiveRecord:如何优雅(XML)序列ActiveRecord对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有困难的时候找到了如何优雅的ActiveRecord的序列化对象的信息。

I'm having a difficult time finding information on how to elegantly serialize ActiveRecord objects.

我们想用XML作为格式,因为我们需要输出我们对象在这样一种方式,另一个方案将能够可行性的解析。

We would like to use XML as the format because we need to output our objects in such a way that another program will be able to feasibly parse them.

XML的序列通常是非常容易和简单实现,但试图当问题出现序列化从ActiveRecord的数据库返回的对象。该数据库返回的代理类对象,其中无法通过 [XmlInclude] 属性明确预期的类型。

XML-Serialization is usually very easy and straightforward to implement, but the problem comes when trying to serialize an object returned from the ActiveRecord database. The database returns a proxy class of the object, the type of which cannot be explicitly anticipated via the [XmlInclude] attribute.

例如:

public class Foo : ActiveRecordLinqBase<Foo>
{
   public virtual string Bar{get;set;}

   public virtual int FooId{get;set;}

   public Foo(string bar)
   {
      Bar = bar;
   }

   public static void FooSerializeExample()
   {
      Foo tehFoozor = new Foo("omgFoo!");
      tehFoozor.SaveAndFlush();
      int id = tehFoozor.FooId;

      //...
      //Assume new ActiveRecord session.

      XmlSerializer serializer = new XmlSerializer(typeof(Foo));

      Foo tehFoozorToSerialize = Foo.Find(id);

      using(Stream stream = File.OpenWrite("tehFoozor.xml"))
      {
         serializer.Serialize(stream, tehFoozorToSerialize); //Will fail
      }
   }
}



当序列在这里,我们会得到一个消息:该类型是没有预料FooProxy2e2de24df9be42909d13a67fdb00b981使用XmlInclude或SoapInclude属性来指定不是静态已知类型

When serializing here, we'll get a message:
"The type FooProxy2e2de24df9be42909d13a67fdb00b981 was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."

,其中代理类型将是完全不可预测的(至少据我所知)。

where the Proxy type will be completely unpredictable (at least to my knowledge).

作为一个临时的解决方案,我的团队已经掐灭每个AR对象性能成接口。然后,我们实施了各容器的对象,这是对象的本质的XML序列化非AR的版本。考虑到这一事实,我们目前有哪些被序列18个不同ar对象,这是在我们的解决方案36其他文件!东西(一切)告诉我,这是一个糟糕的解决方案,但我没能找到一个更好的办法。

As a temporary solution, my team has stubbed out each AR object's properties into interfaces. Then, we implemented "Container" objects for each, which are essentially Xml-Serializable non-AR versions of the objects. Considering the fact that we currently have 18 different AR objects which are serialized, that's 36 additional files in our solution! Something (everything) tells me this is a bad solution, but I've not been able to find a better way.

我们也尝试过使用SOAP格式化,但由于ActiveRecrodLinqBase<?>是不是标记为可序列化,这是一个死胡同还

We also tried using the Soap Formatter, but since ActiveRecrodLinqBase<> isn't "marked as serializable", this was a dead end also.

推荐答案

获取你需要急切的一切。然后使用 AutoMapper 其映射到DTO的和序列化的DTO的。

Fetch everything you need eagerly, then use AutoMapper to map it to DTOs and serialize those DTOs.

你得到代理的错误意味着你的DTO实际上是利用NHibernate的代理的集合,当他们要使用纯列表< T> 或数组来代替。请确保您使用了ToList()或相似的。

The proxy error you're getting suggests that your DTOs are actually using NHibernate's proxied collections, when they should be using plain List<T> or arrays instead. Make sure you use ToList() or similar.

此外,你并不需要使用接口映射DTO的。

Also, you don't need to use interfaces to map DTOs.

这篇关于C#城堡的ActiveRecord:如何优雅(XML)序列ActiveRecord对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆