views:

43

answers:

2

Am I doing something wrong if I need code like this in a Controller? Should I be doing something differently?

public ActionResult Details(int id)
{
    var svc = new ServiceClient();
    var model = new MyViewModel();
    model.ObjectA = svc.GetObjectA(id);
    model.ObjectB = svc.GetObjectB(id);
    model.ObjectC = svc.GetObjectC(id);
    return View(model);
}

The reason I ask, is because I've got Linq-To-Sql on the back end and a WCF Service which exposes functionality through a set of DTOs which are NOT the Linq-To-Sql generated classes and thus do not have the parent/child properties; but in the detail view, I would like to see some of the parent/child data.

+1  A: 

You might consider replacing your multiple methods with a single factory method that will generate a pre-populated object and return it. WCF calls have significant overhead, and you should be minimizing them if at all possible.

Dan Story
The problem is that the Service has no knowledge of the class `MyViewModel` where would this factory method live? Would it be a method on the WCF Service Interface? Its not out of the question for the WCF Service to define some of the more common ViewModels and have the abbility to return those, would that be a better design?
Nate Bross
Assuming that MyViewModel is a purely client-side object, then you're faced with something of a tradeoff between design purism and efficiency. My preference in most systems would be to define common view models server side to reduce service calls, but it may be inappropriate for your particular application.
Dan Story
DTO vs ViewModel, same class, different name.
jfar
A: 

Hi Nate,

Am I doing something wrong if I need code like this in a Controller? Should I be doing something differently?

You approach looks good. congrats.

but in the detail view, I would like to see some of the parent/child data.

you can expose an specialized method in your WCF service to bring you a set of ObjectA based in a set of ObjectB (or vice-versa)

SDReyes