<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/wordpress-mu-1.0" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Combining ORM and SOAP - Part 1</title>
	<link>http://tssblog.blogs.techtarget.com/2007/02/08/combining-orm-and-soap-part-1/</link>
	<description></description>
	<pubDate>Wed, 25 Nov 2009 03:05:36 +0000</pubDate>
	<generator>http://wordpress.org/?v=wordpress-mu-1.0</generator>

	<item>
		<title>by: Steve</title>
		<link>http://tssblog.blogs.techtarget.com/2007/02/08/combining-orm-and-soap-part-1/#comment-435</link>
		<pubDate>Thu, 22 Mar 2007 04:35:19 +0000</pubDate>
		<guid>http://tssblog.blogs.techtarget.com/2007/02/08/combining-orm-and-soap-part-1/#comment-435</guid>
					<description>Error	39	TestCase 'Bistro.Domain.CompaniesTests.General'
failed: System.Web.Services.Protocols.SoapException : System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&#38;gt; System.InvalidOperationException: There was an error generating the XML document. ---&#38;gt; System.InvalidOperationException: CProxyTypeMyObject_NHibernate_ProxyINHibernateProxy_System_Runtime_SerializationISerializable2 cannot be serialized because it does not have a parameterless constructor.


This is error if you return it with lazyload on...big problem with xml serialization and nhibernate</description>
		<content:encoded><![CDATA[<p>Error	39	TestCase &#8216;Bistro.Domain.CompaniesTests.General&#8217;<br />
failed: System.Web.Services.Protocols.SoapException : System.Web.Services.Protocols.SoapException: Server was unable to process request. &#8212;&#38;gt; System.InvalidOperationException: There was an error generating the XML document. &#8212;&#38;gt; System.InvalidOperationException: CProxyTypeMyObject_NHibernate_ProxyINHibernateProxy_System_Runtime_SerializationISerializable2 cannot be serialized because it does not have a parameterless constructor.</p>
<p>This is error if you return it with lazyload on&#8230;big problem with xml serialization and nhibernate
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Anand</title>
		<link>http://tssblog.blogs.techtarget.com/2007/02/08/combining-orm-and-soap-part-1/#comment-432</link>
		<pubDate>Wed, 28 Feb 2007 18:48:57 +0000</pubDate>
		<guid>http://tssblog.blogs.techtarget.com/2007/02/08/combining-orm-and-soap-part-1/#comment-432</guid>
					<description>Thanks Scott for the sample code.
About Java soap example, can the J2EE containers   generate the stubs &#38;#38; custom type classes in a configurable package name ,instead of the default say test.ws  package in your example. Is there any way for the client code to use User,Group classes 
to talk to the example web servce? Thanks</description>
		<content:encoded><![CDATA[<p>Thanks Scott for the sample code.<br />
About Java soap example, can the J2EE containers   generate the stubs &#38;#38; custom type classes in a configurable package name ,instead of the default say test.ws  package in your example. Is there any way for the client code to use User,Group classes<br />
to talk to the example web servce? Thanks
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Scott Balmos</title>
		<link>http://tssblog.blogs.techtarget.com/2007/02/08/combining-orm-and-soap-part-1/#comment-434</link>
		<pubDate>Thu, 15 Feb 2007 02:57:12 +0000</pubDate>
		<guid>http://tssblog.blogs.techtarget.com/2007/02/08/combining-orm-and-soap-part-1/#comment-434</guid>
					<description>As requested, here is the equivalent example in ASP.Net / C#. Since ASP.Net doesn't really have a standard framework for interchangeable persistence architectures, I used NHibernate along with the NHibernate Mapping Attributes contributed assembly. This allows for mapping metadata to be in the class attributes rather than in the old-style Hibernate HBM XML files.

First, create a new ASP.Net Web Service. Add assembly references to NHibernate.dll and NHibernate.Mapping.Attributes.dll . Add the following lines to Web.config (taken almost verbatim from NHibernate's documentation):


  
    
  


  
    
      NHibernate.Connection.DriverConnectionProvider
      Server=(local);initial catalog=TSSBlog;Integrated Security=SSPI
    
  

In this example, I'm using SQL Server 2005 Express with a database named TSSBlog. Further NHibernate configuration information is outside the scope of this entry.

Now, the entities...

using System;
using System.Collections.Generic;
using NHibernate.Mapping.Attributes;

namespace test
{
    [Serializable, Class]
    public class User
    {
        private long id;
        private string userName;
        private string password;
        private string firstName;
        private string lastName;

        [Id]
        public long Id
        {
            get { return id; }
            set { id = value; }
        }

        [Property]
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        [Property]
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        [Property]
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        [Property]
        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
    }

    [Serializable, Class]
    public class Group
    {
        private long id;
        private string name;
        private List members = new List();

        [Id]
        public long Id
        {
            get { return id; }
            set { id = value; }
        }

        [Property]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        [OneToMany]
        public List Members
        {
            get { return members; }
            set { members = value; }
        }
    }
}

Nothing really Earth-shattering. Almost exactly like the Java entities. Now the service endpoint (in the code-behind C# file named Service.cs):

using System;
using System.IO;
using System.Reflection;
using System.Web.Services;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Mapping.Attributes;

namespace test
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class TestService : System.Web.Services.WebService
    {
        private Configuration cfg;
        private ISession em;

        public TestService()
        {
            cfg = new Configuration();
            MemoryStream stream = new MemoryStream();
            HbmSerializer.Default.Validate = true;
            HbmSerializer.Default.Serialize(stream, Assembly.GetExecutingAssembly());
            stream.Position = 0;
            cfg.Configure();
            cfg.AddInputStream(stream);
            stream.Close();
            em = cfg.BuildSessionFactory().OpenSession();
        }

        [WebMethod]
        public User GetUserById(long userId)
        {
            User user = em.Get(typeof(User), userId) as User;
            return user;
        }

        [WebMethod]
        public User AddUser(string firstName, string lastName, string userName, string password)
        {
            User user = new User();
            user.FirstName = firstName;
            user.LastName = lastName;
            user.UserName = userName;
            user.Password = password;

            em.SaveOrUpdate(user);
            return user;
        }

        [WebMethod]
        public Group GetGroupById(long groupId)
        {
            Group group = em.Get(typeof(Group), groupId) as Group;
            return group;
        }
    }
}

I'll get back to the NHibernate cruft in the constructor in a moment. Finally, Service.asmx (mainly because I renamed the endpoint class):



If you compare this code to the original Java code, all the explanation is the same. The only difference here is in the constructor, and this is also almost verbatim taken from NHibernate documentation. The MemoryStream is used by the internal NHibernate Mapping Attributes HBM generator. It auto-generates HBM XML mapping files on the fly and writes it to the stream, which is then fed into the NHibernate Configuration object. We finally pull open a session, which for our intents here is the same as a JPA EntityManager. This is why I kept the original variable name of "em", just to keep things consistent with the original code.

Hope this is a useful-enough conversion. I don't do all that much ASP.Net coding. :)</description>
		<content:encoded><![CDATA[<p>As requested, here is the equivalent example in ASP.Net / C#. Since ASP.Net doesn&#8217;t really have a standard framework for interchangeable persistence architectures, I used NHibernate along with the NHibernate Mapping Attributes contributed assembly. This allows for mapping metadata to be in the class attributes rather than in the old-style Hibernate HBM XML files.</p>
<p>First, create a new ASP.Net Web Service. Add assembly references to NHibernate.dll and NHibernate.Mapping.Attributes.dll . Add the following lines to Web.config (taken almost verbatim from NHibernate&#8217;s documentation):</p>
<p>      NHibernate.Connection.DriverConnectionProvider<br />
      Server=(local);initial catalog=TSSBlog;Integrated Security=SSPI</p>
<p>In this example, I&#8217;m using SQL Server 2005 Express with a database named TSSBlog. Further NHibernate configuration information is outside the scope of this entry.</p>
<p>Now, the entities&#8230;</p>
<p>using System;<br />
using System.Collections.Generic;<br />
using NHibernate.Mapping.Attributes;</p>
<p>namespace test<br />
{<br />
    [Serializable, Class]<br />
    public class User<br />
    {<br />
        private long id;<br />
        private string userName;<br />
        private string password;<br />
        private string firstName;<br />
        private string lastName;</p>
<p>        [Id]<br />
        public long Id<br />
        {<br />
            get { return id; }<br />
            set { id = value; }<br />
        }</p>
<p>        [Property]<br />
        public string UserName<br />
        {<br />
            get { return userName; }<br />
            set { userName = value; }<br />
        }</p>
<p>        [Property]<br />
        public string Password<br />
        {<br />
            get { return password; }<br />
            set { password = value; }<br />
        }</p>
<p>        [Property]<br />
        public string FirstName<br />
        {<br />
            get { return firstName; }<br />
            set { firstName = value; }<br />
        }</p>
<p>        [Property]<br />
        public string LastName<br />
        {<br />
            get { return lastName; }<br />
            set { lastName = value; }<br />
        }<br />
    }</p>
<p>    [Serializable, Class]<br />
    public class Group<br />
    {<br />
        private long id;<br />
        private string name;<br />
        private List members = new List();</p>
<p>        [Id]<br />
        public long Id<br />
        {<br />
            get { return id; }<br />
            set { id = value; }<br />
        }</p>
<p>        [Property]<br />
        public string Name<br />
        {<br />
            get { return name; }<br />
            set { name = value; }<br />
        }</p>
<p>        [OneToMany]<br />
        public List Members<br />
        {<br />
            get { return members; }<br />
            set { members = value; }<br />
        }<br />
    }<br />
}</p>
<p>Nothing really Earth-shattering. Almost exactly like the Java entities. Now the service endpoint (in the code-behind C# file named Service.cs):</p>
<p>using System;<br />
using System.IO;<br />
using System.Reflection;<br />
using System.Web.Services;<br />
using NHibernate;<br />
using NHibernate.Cfg;<br />
using NHibernate.Mapping.Attributes;</p>
<p>namespace test<br />
{<br />
    [WebService(Namespace = &#8220;http://tempuri.org/&#8221;)]<br />
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]<br />
    public class TestService : System.Web.Services.WebService<br />
    {<br />
        private Configuration cfg;<br />
        private ISession em;</p>
<p>        public TestService()<br />
        {<br />
            cfg = new Configuration();<br />
            MemoryStream stream = new MemoryStream();<br />
            HbmSerializer.Default.Validate = true;<br />
            HbmSerializer.Default.Serialize(stream, Assembly.GetExecutingAssembly());<br />
            stream.Position = 0;<br />
            cfg.Configure();<br />
            cfg.AddInputStream(stream);<br />
            stream.Close();<br />
            em = cfg.BuildSessionFactory().OpenSession();<br />
        }</p>
<p>        [WebMethod]<br />
        public User GetUserById(long userId)<br />
        {<br />
            User user = em.Get(typeof(User), userId) as User;<br />
            return user;<br />
        }</p>
<p>        [WebMethod]<br />
        public User AddUser(string firstName, string lastName, string userName, string password)<br />
        {<br />
            User user = new User();<br />
            user.FirstName = firstName;<br />
            user.LastName = lastName;<br />
            user.UserName = userName;<br />
            user.Password = password;</p>
<p>            em.SaveOrUpdate(user);<br />
            return user;<br />
        }</p>
<p>        [WebMethod]<br />
        public Group GetGroupById(long groupId)<br />
        {<br />
            Group group = em.Get(typeof(Group), groupId) as Group;<br />
            return group;<br />
        }<br />
    }<br />
}</p>
<p>I&#8217;ll get back to the NHibernate cruft in the constructor in a moment. Finally, Service.asmx (mainly because I renamed the endpoint class):</p>
<p>If you compare this code to the original Java code, all the explanation is the same. The only difference here is in the constructor, and this is also almost verbatim taken from NHibernate documentation. The MemoryStream is used by the internal NHibernate Mapping Attributes HBM generator. It auto-generates HBM XML mapping files on the fly and writes it to the stream, which is then fed into the NHibernate Configuration object. We finally pull open a session, which for our intents here is the same as a JPA EntityManager. This is why I kept the original variable name of &#8220;em&#8221;, just to keep things consistent with the original code.</p>
<p>Hope this is a useful-enough conversion. I don&#8217;t do all that much ASP.Net coding. <img src='http://tssblog.blogs.techtarget.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Michael</title>
		<link>http://tssblog.blogs.techtarget.com/2007/02/08/combining-orm-and-soap-part-1/#comment-433</link>
		<pubDate>Wed, 14 Feb 2007 18:23:36 +0000</pubDate>
		<guid>http://tssblog.blogs.techtarget.com/2007/02/08/combining-orm-and-soap-part-1/#comment-433</guid>
					<description>Thanks for the example, Scott. I would like to see how this is done with C#.</description>
		<content:encoded><![CDATA[<p>Thanks for the example, Scott. I would like to see how this is done with C#.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
