views:

184

answers:

2

I'm exploring the offerings of an ORM (focusing on NHibernate, considering all options) and am convinced that using one could be a great benefit for some of our projects--however I'm having a hard time picturing just how it would play in our system.

My understanding is that an ORM is ideally used to glue together a database and business logic. This assumes the business logic has access to a database, but in our system we have web-services stuck in the middle.

Our current system is fairly simple. We are .NET thru-and-thru and have:

  • Databases. With tables... and rows.
    • Permissions are limited to execute rights on sprocs, we build sprocs with basic validation and nothing goes into or out of the database without going through a sproc.
  • WebServices
    • Perform CRUD operations against the DB sprocs
    • Currently typically use DataSets/DataTables for their messages
  • Business Logic
    • Talks to the web services

Adding an ORM to the mix seems to logically put it between the database and webservice. So the client would make a request to the service, the service would use the ORM to retrieve the results as objects instead of as DataTables, and the client would receive objects from the web-service.

So my barage of questions is:

  • Does that approach work?

    • Do any particular ORMs favor this type of approach? Are any major ORMs particularly ill-suited to this environment?
  • Is there another implementation that would place some of the ORM interaction on the client-side of the web-service (have the client request an object from the ORM provider and have the ORM provider wrap the web-service communication)?

  • Our current unit-of-work focuses on DataTables and their row-state tracking.

    • How much of the state-tracking would an ORM provide when we stick a webservice between it and the business logic?
    • Would the objects we map to be required to provide their own state-tracking?
+1  A: 

Your webservice and stored procedure layers are already doing most of the work that a low level ORM would perform: accessing the database in a strong typed fashion. It sounds like you get DataTables back from the web service and you are looking to encapsulate those DataTables into classes.

You would get very little help from the "autogeneration" capabilities that most ORM products provide as they are usually geared toward creating the classes directly from a SQL source. On the other hand, if your web services allow the discovery of the columns returned it isn't a hard project to create such wrapper classes from traditional code generation techniques. If these generated classes derive from the base types of your ORM you may be able to use the rest of the infrastructure (entity collections, unit of work, etc).

This is pretty different from most of the architectures I have seen though. I have seen DataStore->ORM->Business Logic->Web Service==>Consumer used quite a bit. This makes the business logic easy to write against the data store while providing web services that do the heavy lifting of deciding how to engage the business logic. The consumer (the end user's application, either desktop or a web presentation) then is mostly responsible for presentation (as it should be in most cases).

On the other hand (unless I have misread) it seems you are interested in DataStore->Sproc->WebServices===>ORM->Business Logic->Consumer. This isn't something I have seen often. I think it works against your adopting ORM in the manner it appears you are thinking about.

Am I missing something, or is most of the business logic really being executed on the client?

Godeke
I suppose the term "business logic" is a little loose. Suppose our app is a widget editor where I can create/edit/delete widgets from our database. In our implementation all logic to actually work with that widget is client-side; the only server-side logic is user authentication and widget validation (cursory checks that the widget doesn't violate any major data rules). The client gets a DataTable of widgets and can add/edit/delete as much as it likes, plus it also contains full-blooded validation.
STW
In your example, the user can define the widgets. That would seem to imply a fairly small number of widget related classes that carry a lot of customized data about a specific widget's use and state (this is the DataTable info). Because of this (potentially) small number of classes it may be simpler to create some facade classes if you want to hide the DataTable's themselves behind a simple to use interface... but it would be custom, not a traditional ORM.
Godeke
+1  A: 

I would recomend against using an ORM in your situation, and this is coming from somone who uses NHibernate regularly. In your current setup, you will have to jump through several hoops just to get your application working and you will not be able to benefit from most of the features a good ORM like NHibernate provide.

  • Working with sprocs and an ORM, while it can be done, is difficult to configure and hamstrings the ORM from what it does.
  • Persistence by reachability is a HUGE benefit when using NHibernate, but would not be available to you since you're domain layer is "disconnected".
  • NHibernate is based off of IDbConnection, so trying to hook this up on the client side over a Web service would eliminate you from using the Session (Unit of Work)

If you're trying to pass objects instead of DataTables, why not use DTOs instead? This would give you your strongly typed objects, but would still work in your setup.

Stefan Moser