tags:

views:

367

answers:

1

Hello everyone,

i'm investigating Nhibernate, jquery and WCF at the same time for a in-house project, and i was wondering why i couldn't have "clean" data classes.

From what i've read, a very good point in the nhibernate world is that my business class won't be linked to the framework. I won't have

<SaveInTable("Thingie")> _
Public Class Thingie
    <ColumnName("ThingieId")> _
    Public Property Id as Integer
    ' accessors
End Class

but rather something like

Public Class Thingie
Public Property Id as Integer
' etc

And then

Public Class ThingieMapping
Inherits ClassMap(Of Thingie)
' etc, omitted for brevity's sake

What i don't understand is that if i want to list Thingies in a web page with jQuery, and if i want to use WCF webservices with jquery (it looks like the current trend from what i've seen on various tutorials), i find myself having to add DataContract and DataMember attributes to my Thingie class.
On the other hand, the classic ASMX webservices won't bother me and let me retrieve the Thingie class without worrying about adding attributes.

I have the feeling that i'm missing part of the picture here; it seems logical to try keeping the Thingie class mostly independent, but i haven't found examples so far.

So, do i have to abandon all hopes and stick to the classic webservices, or is there a way to have my WCF cake and eat it too?

+1  A: 

I suggest you use DTOs to send over the wire - then you can decorate the DTOs with the necessary attributes.

This means, of course, that you must somehow map your domain classes to the DTOs and vice versa. If the mapping is trivial (+some other cases that satisfies some conventions), you can use AutoMapper for that.

Using DTOs has several benefits, the best being that you have a clear seperation of concerns - your NHibernate-mapped domain model is about modeling stuff in your domain, and your DTOs are for sending data over the wire. Then, if one changes, the other doesn't necessarily need to change as well.

mookid8000