Recently, no sorry, it’s too much time, I saw online some discussions about using and the “best-practice-usage” of NHibernate in conjuction with WCF, and I saw so many thing about it I think someone has to say smething serious and real about this thread!
I Think I’m not one voice which can say something about it as some people in the net could do, I know many names of peoples who can be a relevant voice better than me, (I’m really new in posting to blog!) But I have some experience in this argument and so I wish to share my experience made in the past 3 months.
Now I’m working on a project built as a 3-tier app, the core technologies are surely WCF and NHibernate. I’m proud of using WCF as I see and as I heard from many people, no much people has used WCF yet (for example in my city – Rome – ), and more, WCF in conjuction with NHibernate.
I have some real experience in WCF I have made 4 or 5 big projects using that, and I have some minor experience in NHibernate, but the past 3 month gave to me a “little strong” experience on that ” WCF + NHibernate “.
I”m working on an “intranet system” made as follow:
-
about 100 entities
-
about 50 of them are connected between them
-
about 10 services exchanging them
-
strong and large usage of ICriteria API
These are just some of the things that made up my solution, but are the more relevant I think to talk about the question:
-
My Objects are just POCOs, decorated with DataContract and used by some services.
-
My Objects are mapped as the NHibernate Best Practice would, with HBM files directly embedded in the containing assembly.
-
Mappings and pocos are physically in 2 different assemblies
-
The user Interface layer makes use of the POCOs directly by referencing the PoOCOs assembly (this is irrilevant as the nature of WCF that is Contrat Firts. Personally I hate generated entities..)
Here that’s the first thing note: everithing works correctly!
I heard and read somewhere something as “using WCF and NHibernate is impossible” or “using them togheter could made explode WCF”. Well, I think is important to understand and note what we think and wht we expect about when we say EVERITHING WORKS CORRECTLY.
Beginning with WCF, I have to remember that , it’s true WCF is born to glue some core technologies (Web Services, Remoting, …) as a “foundation” but, spending no many words, it’s made to work with “Entities” and, Entities, in the WCF world are just POCOs!
Continuing with NHibernate, it’s clear that some CORE functionalities are the MOST important things offered by the framework, I mean eg. lazy loading, collections, and so on.. These functinoalities, togheter with a fat-list of features, makes the framework on of the most important and more productive now.
Now with the advent of MS Entity Framework, the problem could be the same. I saw some Open Source projects that has been built for WCF, to give features like the “connected-local” way does to the “disconnected-distribuited” way could expect. These features makes the game as with DataSets, with Original, Modified, New and Deleted values. But came a question in mind to me, this however to work fine, surely has to “respect” some limitations? so we could take all these limitations (including maybe loosing the standards reducing the system to work just .net to .net), all the features, all the things that NH has more than Entity Framework and vice versa, and think, “if this is the situation I can use NH with WCF too” making use of some rules and some best-practice, trying to use all of the features I can without fall into a technology BREAK!
I have to say this could be a dissertation which could continue maybe for ever… I think should be clear that “the situation” drive into the solution, not all the projects are the same… So in the next post I will talk about the rules I adopted, maybe many people could find them antiquate, boring and non-elegant, but for me the most important thing is: Using NHibernate with WCF is possible!
Ciao
p.s. Sorry for my poor english…