views:

1225

answers:

3

Does anyone have advice or tips on using a web service as the model in an ASP.Net MVC application? I haven't seen anyone writing about doing this. I'd like to build an MVC app, but not tie it to using a specific database, nor limit the database to the single MVC app. I feel a web service (RESTful, most likely ADO.Net Data Services) is the way to go.

A: 

A web service exposes functionality across different types of applications, not for abstraction in one single application, most often.

Use a service from a service bus if you're after the decoupling and do an async pattern in your async pages. You can see Rhino.ServiceBus, nServiceBus and MassTransit for .Net native implementations and RabbitMQ for something different http://blogs.digitar.com/jjww/2009/01/rabbits-and-warrens/.

Edit: I've had some time to try rabbit out and implemented a book keeping system from a web shot using it very successfully. It's simply great and takes care of keeping my messages persistent and can even be used for decoupling concerns from within a single program, e.g. a spider, so that in the future, another program might parses the results (I used it for this as well). The rabbit team seems very devoted to their product, which in itself has a rather simple interface.

You can also simply provide service interfaces. Read Eric Evan's Domain Driven Design for a more detailed description or google "ddd quickly infoq" to read something free but less well written. REST is nice if you have something which requires complete ignorance of programming model, like pushing blog updates onto a site for example, but for interapplication calls it'll kill your performance and may cause some nice recursions.

ADO.Net is rather crap [N+1 problems with lazy collection because of code-to-implementation, data-access leakage - you always need your db context where your query code is etc] in comparison to for example LightSpeed (commercial) or NHibernate. Spring.Net also allows you to wrap service interfaces in their contain with a web service facade, but (without having browsed it for a while) I think it's a bit too xmly in its configuration.

Edit: With ADO.Net here I mean the default "best practice" with DataSets, DataAdapter and iterating lots of rows from a DataReader; it breeds rather ugly and hard-to-debug code. The N+1 stuff, yes, that is about the entity framework.

Create your domain layer in a separate assembly [aka. Core] and provide all domain and application services there, then import this assembly from your specific MVC application. Wrap data access in some DAO/Repository, through an interface in your core assembly, which your Data assembly then references and implements. Wire up interface and implementation with IoC. You can even program something for dynamic service discovery with the above mentioned service buses, to solve for the interfaces. WCF uses interfaces like this and so do most of the above service busses; you can provide a subcomponentresolver in your IoC container to do this automatically.

Edit: An very good container for async programming is AutoFac.

Update 2010-01-02

The jest of my idea has been codified by something called MindTouch Dream. They have made a screencast where they treat almost all parts of a web application as a (web)-service, which also is exposed with REST.

They have created a highly parallel framework using co-routines to handle this, including their own elastic thread pool.

To all the nay-sayers in this question, in ur face :p! Listen to this screen-cast, especially at 12 minutes.

The actual framework is here.

If you are into this sort of programming, have a look at how monads work and their implementations in C#. You can also read up on CoRoutines.

Happy new year!

Henrik
You are comparing ADO.Net to ORM (paragraph #4)? Did you mean LINQ2SQL or EF?
MotoWilliams
Well, ORM uses ADO.Net; I was comparing using them each as the main interface to the database, in which case it makes sense.
Henrik
N+1 on ado.net? I mean seriously....Lazy-loaded collections only cause N+1 in certain scenarios that you will reach with nhibernate. That's why you have per-query eager loading through fetch strategies in any reasonable ORM out there, including linq2sql.As it happens, nhibernate is actually running ado.net underneath for your db connection...
serialseb
Not sure what messaging and ESBs have to do with the OP's question. Isn't he essentially asking for something like ADO.NET Data Services (Astoria) - or something similar but not quite as "exposed"?
Michael Hart
"As it happens, nhibernate is actually running ado.net underneath for your db connection", that's why I said "ORM uses ADO.Net". What I aimed at was EF which has problems with N+1 and can be said to go under the banner of ADO.Net, just take the first result from google: http://en.wikipedia.org/wiki/ADO.NET#Entity_Framework.Serialseb: that's exactly what I'm talking about, that if you're willing to step out of comfort zone no. 1 with typed data-sets and EF and that BS, you'll be better off with an ORM.
Henrik
"messaging and ESBs have to do with the OP's question" - he is talking about using WSes as Model in MS MVC (model view controller), which means he'd be talking over http 1.1 for each query against the data model, which would mean that for every request you'd generate another one, over the network, without static compilation of the project (since it's just a proxy being generated) and all the problems that come along with that. (I could expand on this if you wish). When he says: "but not tie it to using a specific database" - I can't see how you can recommend ADO.Net data services!
Henrik
Btw, why give me -4??
Henrik
I don't get why this was downgraded. Sure, he confused EF for ADO .NET, but with all the other comments, it's clear he's not clueless on this stuff. All of these comments are constructive in one way or another; I see no reason for the downvotes.
Jason
+3  A: 

You should define your models in a data access agnostic way, e.g. using Repository pattern. Then you can create concrete implementations backed by specific data access technologies (Web Service, SQL, etc).

DSO
+23  A: 

How likely, or useful, is it for your MVC app to be decoupled from your database? How often have you seen, in your application lifetime, a change from SQL Server to Oracle? From the last 10 years of projects I've delivered, it's never happened.

Architectures are like onions, they have layers of abstractions above things they depend on. And if you're going to use an RDBMS for storage, that's at the core of your architecture. Abstracting yourself from the DB so you can swap it around is very much a fallacy.

Now you can decouple your database access from your domain, and the repository pattern is one of the ways to do that. Most mature solutions use an ORM these days, so you may want to have a look at NHibernate if you want a mature technology, or ActiveRecord / linq2sql for a simpler active record pattern on top of your data.

Now that you have your data strategy in place, you have a domain of some sort. When you expose data to your client, you can choose to do so through an MVC pattern, where you'll usually send DTOs generated from your domain for rendering, or you can decide to leverage an architecture style like REST to provide more loosely coupled systems, by providing links and custom representations.

You go from tight coupling to looser coupling as you go towards the external layers of your solution.

If your question however was to build an MVC app on top of a REST architecture or web services, and use that as a model... Why bother? If you're going to have a domain model, why not reuse it in your system and your services where it makes sense?

Generating a UI from an MVC app and generating documents needed for a RESTful architecture are two completely different contexts, basing one on top of each other is just going to cause much more pain than needed. And you're sacrificing performance.

Depends on your exact scenario, but remote XML-based service as the model in MVC, from experience, not a good idea, it's probably over-engineering and disregarding the need for a domain to start with.

serialseb
Increasingly, enterprise applications are required to be able to use a database from any vendor, and your software should be able to do that. A lot of companies have licenses for Oracle for example, but are now leaning towards MySql.
Liao
Requiring switching databases is an expensive abstraction to impose, and an expensive switch to make. It's one of those astronaut architecture requirements that, when the switch occur, still cost as much.For most scenarios, one is much better off architecting to one database. Should the requirement for a switch happen, it'll cost anyway, and I'll argue it may cost much less than designing for database agnostic scenarios.Even using an ORM a la nhibernate, one will still have inconsistencies, incompatibilities, variable sorting, etc.
serialseb
"How likely, or useful, is it for your MVC app to be decoupled from your database?" - I don't see why every other poster needs to tell the person asking the question that they are wrong unless it fits your self-imposed model of refuting the preconditions of the question (that he puts up) rather than asking the core of the question itself!
Henrik
Henrik,I think it's very healthy to ask the reason for a question, rather than answer blindly. It's the culture of cargo cult development and the lack of thorough questioning of the why that leads to most software development nightmares. When you go to the doctor, you don't ask them which antibiotic you should be taking, you ask them if they can cure your illness.
serialseb
I think you should open up your eyes to new paradigms of development instead of blindly saying no. I've updated my reply, perhaps more to your liking.
Henrik
I have my eyes quite open on new paradigms. What mindtouch is doing is adapted to their needs, and we can discuss at length their design choices. In this instance it smells of astronaute architecture, as no information provided by the person asking the question implies that there is an actual need for using rest as the model in mvc.
serialseb