NHibernate: How do I XmlSerialize an ISet?

后端 未结 4 1314
我在风中等你
我在风中等你 2020-12-01 17:32

Given:

  • I\'m trying to create a REST API using ASP.NET MVC.
  • I\'m using NHibernate in my data access layer.

Problem:

  • I\'m c
相关标签:
4条回答
  • 2020-12-01 18:01

    1) Load the Dozer bean mapper from mapping file

    DozerBeanMapper dtoMapper = new DozerBeanMapper(Arrays.asList(new String[]{dozerMappingfile}));
    

    2) Covert each object to a normal object removing persistentbag related details

    List<MyEjb>  lstProfilehib =  //hibernate loaded objects
            List<MyEjb>  lstProfile  = new ArrayList<MyEjb>();
            for(MyEjb sp: lstProfilehib){
                lstProfile.add( dtoMapper.map(sp, MyEjb.class)); 
            }
    
    0 讨论(0)
  • 2020-12-01 18:05

    NHibernate serialization has been treated a lot on stackoverflow. See:

    • C# Castle ActiveRecord: How to elegantly (XML) serialize ActiveRecord objects?
    • How do I serialize all properties of an NHibernate-mapped object?
    • NHibernate and WCF Serialization(Unidirectional)
    • JSON.NET and nHibernate Lazy Loading of Collections
    • Which .NET JSON serializers can deal with NHibernate proxy objects?
    • DTOs vs Serializing Persisted Entities
    • Returning NHibernate mapping classes from WCF services

    Bottom line: use DTOs.

    0 讨论(0)
  • 2020-12-01 18:14

    You can never XML Serialize an interface - only a concrete class that implements the interface.

    0 讨论(0)
  • 2020-12-01 18:15

    Try using the DataContractSerializer instead. It's more restrictive, but will serialize more.

    Dan Rigsby explains the difference between XMLSerializer and DataContractSerializer

    Here's an example from one of my posts on stackoverflow:

    public XDocument GetProductXML(Product product)
        {
            var serializer = new DataContractSerializer(typeof(Product));
            var document = new XDocument();
    
            using (var writer = document.CreateWriter())
            {
                serializer.WriteObject(writer, product);
                writer.Close();
            }
    
            return document;
        }
    
    0 讨论(0)
提交回复
热议问题