How do I serialize all properties of an NHibernate-mapped object?

后端 未结 3 1610
逝去的感伤
逝去的感伤 2020-12-03 22:41

I have some web methods that return my objects back as serialized XML. It is only serializing the NHibernate-mapped properties of the object... anyone have some insight? I

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 23:34

    if its a WCF service, you could use a IDataContractSurrogate

     public class HibernateDataContractSurrogate : IDataContractSurrogate
    {
        public HibernateDataContractSurrogate()
        {
        }
    
        public Type GetDataContractType(Type type)
        {
            // Serialize proxies as the base type
            if (typeof(INHibernateProxy).IsAssignableFrom(type))
            {
                type = type.GetType().BaseType;
            }
    
            // Serialize persistent collections as the collection interface type
            if (typeof(IPersistentCollection).IsAssignableFrom(type))
            {
                foreach (Type collInterface in type.GetInterfaces())
                {
                    if (collInterface.IsGenericType)
                    {
                        type = collInterface;
                        break;
                    }
                    else if (!collInterface.Equals(typeof(IPersistentCollection)))
                    {
                        type = collInterface;
                    }
                }
            }
    
            return type;
        }
    
        public object GetObjectToSerialize(object obj, Type targetType)
        {
            // Serialize proxies as the base type
            if (obj is INHibernateProxy)
            {
                // Getting the implementation of the proxy forces an initialization of the proxied object (if not yet initialized)
                try
                {
                    var newobject = ((INHibernateProxy)obj).HibernateLazyInitializer.GetImplementation();
                    obj = newobject;
                }
                catch (Exception)
                {
                   // Type test = NHibernateProxyHelper.GetClassWithoutInitializingProxy(obj);
                    obj = null;
                }
            }
    
            // Serialize persistent collections as the collection interface type
            if (obj is IPersistentCollection)
            {
                IPersistentCollection persistentCollection = (IPersistentCollection)obj;
                persistentCollection.ForceInitialization();
                //obj = persistentCollection.Entries(); // This returns the "wrapped" collection
                obj = persistentCollection.Entries(null); // This returns the "wrapped" collection
            }
    
            return obj;
        }
    
    
    
        public object GetDeserializedObject(object obj, Type targetType)
        {
            return obj;
        }
    
        public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType)
        {
            return null;
        }
    
        public object GetCustomDataToExport(Type clrType, Type dataContractType)
        {
            return null;
        }
    
        public void GetKnownCustomDataTypes(Collection customDataTypes)
        {
        }
    
        public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
        {
            return null;
        }
    
        public CodeTypeDeclaration ProcessImportedType(CodeTypeDeclaration typeDeclaration, CodeCompileUnit compileUnit)
        {
            return typeDeclaration;
        }
    }
    

    Implementaion in the host:

    foreach (ServiceEndpoint ep in host.Description.Endpoints)
            {
                foreach (OperationDescription op in ep.Contract.Operations)
                {
                    var dataContractBehavior = op.Behaviors.Find();
                    if (dataContractBehavior != null)
                    {
                        dataContractBehavior.DataContractSurrogate = new HibernateDataContractSurrogate();
                    }
                    else
                    {
                        dataContractBehavior = new DataContractSerializerOperationBehavior(op);
                        dataContractBehavior.DataContractSurrogate = new HibernateDataContractSurrogate();
                        op.Behaviors.Add(dataContractBehavior);
                    }
                }
            }
    

提交回复
热议问题