views:

125

answers:

2

Hi. The question title might not be clear but what I want to do is something like this:

This is how I layer out my app

App.Domain App.Services App.Web

What I want is, if I request something like /api/OrderProcessor/GetAllOrder it will call method GetAllOrder in App.Services.OrderProcessorService.

The method will return an IList<Order> and i would like it to be serialized into JSON according to a certain format (I'm using ExtJS actually), to maybe something like:

{ 
  success: true,
  totalCount: 10,
  list: [ { ... }, { ... } ]
}

I can go on and make the Services as Controllers, but I don't want the service layer to be tainted with presentation stuff.

How can I go about making a wrapper controller like this?

I don't want to attach any attributes on the Service class itself, and would probably be nice if I can configure it using IoC, where by maybe later on I want the output be XML or maybe the ability to use a DTO class instead of the original Domain class.

Any idea?

+2  A: 

It's sounds like you're trying to make a RESTful service.

Using a RESTful service, the /api/OrderProcessor/GetAllOrders URI would return your JSON objects.

If that's the case, I would use WCF instead of ASP.NET MVC.

To get started with WCF, REST, and JSON, check out the WCF REST Starter Kit Preview 2 on Codeplex. There's a quick example of returning JSON from a WCF service in this blog post I found by Ben Dewey.

Good luck!

Kevin Swiber
I did look a bit into using WCF service and using normal WebMethod with ScriptService. But that's not exactly the way I want to do it. I want the Service layer to be clean, not littered by attributes.Thanks anyway, Kevin.
Ikhwan
jrista
I'm talking based on experience with NHibernate Mapping Attributes vs FluentNHibernate, the latter is much better for me. Attributes are acceptable when you have 1 for method/class, but more than that tend to clutter the code.
Ikhwan
A: 

You could use something like PostSharp's OnMethodInvocationAspect to intercept every call to your controller method and act as a relay to similarly named methods on a designated proxy object...

Andrew Matthews
I did thought about using AOP, maybe I'll look deeper into it later. Thanks!
Ikhwan