-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Passing nhibernate entities via WCF
PostPosted: Wed Aug 20, 2008 7:42 am 
Newbie

Joined: Sat Jun 10, 2006 10:24 am
Posts: 16
Hi People!

Just wondering if anyone has a solution for passing nHibernate entities over WCF. I've tried the Net Data Contract Serializer Attribute method, but this caused the system to have issues with the dynamic proxy for relations (no session exception), Ideally I want to be able to pass the entities across the WCF wire, is there a way to set the proxy type to null when being passed across the wire? how this affect things when i go to save the entity back via the service?

Thanks Heaps!
Maxus



Hibernate version: v2.0.0.2002
Mapping documents: N/A
Code between sessionFactory.openSession() and session.close():N/A
Full stack trace of any exception that occurs: No exception.
Name and version of the database you are using: SQL server 2005
The generated SQL (show_sql=true): N/A
Debug level Hibernate log excerpt: N/A


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 20, 2008 11:33 am 
Newbie

Joined: Fri Aug 15, 2008 5:08 am
Posts: 6
I don't think it's a wiser method,why you don't use dto or data contract?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 4:51 am 
Newbie

Joined: Sat Jun 10, 2006 10:24 am
Posts: 16
Hi Lonely7345,

Thanks for taking the time to reply, I am currenlty using data contracts here is and example of one of my entities this is used by nhibernate and WCF:

Code:

    [DataContract]
    public class StorageCharge : BaseEntity<StorageCharge>, IEquatable<StorageCharge>
    {
       
        [DataMember]
        private decimal _Rate;

        [DataMember]
        private string _Description;

        [DataMember]
        private string _Code;


        public StorageCharge(){}

        public virtual decimal Rate
        {
            get { return _Rate; }
            set { _Rate = value; }
        }

        public virtual string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }

        public virtual string Code
        {
            get { return _Code; }
            set { _Code = value; }
        }

        #region IEquatable members
        public bool Equals(StorageCharge other)
        {
            if (other == this)
            {
                return true;
            }

            return (other != null) && (this._Id == other.Id);

        }
        #endregion

    }




these entities wrok fine until they get to the client side, where if you go anywhere near the a proxyed property an exception is thrown relating to the missing session.

Any suggestions on how to pass the entities and thier relation back across wcf and if the relation hasnt been fetched to just return null for that property?

Thanks Heaps!
-M


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 11:00 am 
Newbie

Joined: Fri Aug 15, 2008 5:08 am
Posts: 6
i can't understand all. DataContract and nhibernate model is different each orther. use nhibernate model to mapping ,datacontract to communicating. In your wcf service ,exchange model and datacontract


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 8:03 pm 
Newbie

Joined: Sat Jun 10, 2006 10:24 am
Posts: 16
Hi Lonely7345,

With the example I have posted, that entity is populated by nhibernate and then sent out across the wire by WCF, are you suggesting the nhibernate entity and the WCF entity should be seperate? not exactly sure how you mean, can you provide a very simple example?

Thanks
M


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 10:15 pm 
Newbie

Joined: Fri Aug 15, 2008 5:08 am
Posts: 6
I prefer Chinese to my poor english, hehe.
nhibernate model:
Code:
   [Serializable]
    public class StorageChargeModel : BaseEntity<StorageCharge>, IEquatable<StorageCharge>
    {
        public virtual decimal Rate
        {
            get ;  set ;
        }
        public virtual string Description
        {
            get ;set ;
        }
        public virtual string Code
        {
            get;  set ;
        }

//....may be some other property for nhibernate mapping but not in datacontract

        #region IEquatable members
        public bool Equals(StorageCharge other)
        {
            if (other == this)
            {
                return true;
            }

            return (other != null) && (this._Id == other.Id);

        }
        #endregion
    }


datacontract as yours:

[DataContract]
    public class StorageCharge : BaseEntity<StorageCharge>, IEquatable<StorageCharge>
    {
       
        [DataMember]
        private decimal _Rate;

        [DataMember]
        private string _Description;

        [DataMember]
        private string _Code;

  // also maybe some property not in nhibernte model


        public StorageCharge(){}

        public virtual decimal Rate
        {
            get { return _Rate; }
            set { _Rate = value; }
        }

        public virtual string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }

        public virtual string Code
        {
            get { return _Code; }
            set { _Code = value; }
        }

        #region IEquatable members
        public bool Equals(StorageCharge other)
        {
            if (other == this)
            {
                return true;
            }

            return (other != null) && (this._Id == other.Id);

        }
        #endregion

    }


In your wcf service ,exchange StorageCharge and StorageChargeModel.
use StorageChargeModel  only for inner. publish StorageChargeModel
     




Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 22, 2008 12:26 am 
Beginner
Beginner

Joined: Mon Feb 04, 2008 7:36 pm
Posts: 31
I am in the process of working through the same issue. What I came up with is... ugly (but not as ugly as writing my own data access layer).

I use the metadata and reflection helpers provided by NHibernate to rebuild an object graph from an existing object graph.

Basically, when I call dao.GetMyObject(id), my dao calls NHibernate and gets the object graph back. Then I use NHibernate metadata to pull the data from each object in the graph and put it into a new instance of the same type.

If a property (many-to-one, one-to-many or many-to-many) has not been initialized, I set it to null and set a flag on a common base class that indicates the property has not been initialized.

The downside to all this.... I had to write all my own collections to manage added/removed elements, I need a base class (I acutally use an interface) to enforce the UninitializedProperties dictionary.

Also, from a scale standpoint, I have to rebuild the object graph twice.

In other words, its a real pain to pull this off.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.