Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I working on an ASP.NET MVC which is loading its data completely via a WCF service. What my model does is just call the WCF service. The controller then passes the model to the view. I read in this article that it is always better to do async calls if you can. However I am still not sure which call to use. What would happen if the data form the service takes a while to load? Does it mean that my controller code will keep running and if the data is not there when the return statement is reached my controller will return an empty view, which could also cause a null pointer exception because of the view accessing unpopulated model fields?

share|improve this question
    
The model calls the WCF service? Meaning the view model? Something is definitely not right there. The model should just be holding the data for the view - if you need to call a service this should be happening through the controller (or some injected dependency of the controller). –  Ant P Jul 3 at 6:10
    
@AntP He doesn't mean view model. The purpose of the model is to represent the backend, the business logic. It does not mean some classes that you pass to the view. In fact in a well designed MVC application the model is very fat and the controller has almost no code in it. However designing an adequate service layer (which is part of the model) is often an overkill for most applications which have almost no logic and just CRUD operations. –  Stilgar Jul 3 at 6:16
    
@Stilgar Yeah. Except... "The controller then passes the model to the view." Meaning he has a view model with business logic and WCF calls in it. Which is just wrong. And, yes, sometimes an abstracted service layer is overkill, which is exactly why I already said "if you need to call a service this should be happening through the controller (or some injected dependency of the controller)." –  Ant P Jul 3 at 7:30
    
If a view model does the WCF call this is indeed wrong. However I think this is just a misunderstanding due to terms that are loosely defined. In fact this is one of the reasons I dislike MVC. There is so much freedom and so many decisions to make. A good pattern should be more obvious. –  Stilgar Jul 3 at 7:40
    
ASP.NET MVC isn't a pattern, though. It's just a framework that happens to be named after a pattern because it (loosely) resembles it. The pattern itself is very well defined. –  Ant P Jul 3 at 7:50

1 Answer 1

up vote 1 down vote accepted

If your controller returns a Task then you will be fine. If you are on C# 5.0 you can use async/await to make your code more maintainable than what is shown in the article. Basically your code will look like this

public async Task<ActionResult> SomeAction()
{
    var someData = await wcfServiceProxy.GetDataAsync() //alternatively an async call to your model service which should also be async
    //some work with the data here
    return View(someData);
}

or you can just not care and use all synchronous calls. The downside of using synchronous calls is that your throughput would suffer. You will be able to handle 200 requests per second instead of 50K requests per second (and I am pulling these numbers out of thin air). Of course in most cases you simply don't care because your project never does more than 20 requests per second.

share|improve this answer
    
Is there any difference between your approach and the one described here: msdn.microsoft.com/en-us/library/ee728598%28v=VS.98%29.aspx (I am new to C# and ASP.NET) –  Georgi Georgiev Jul 3 at 6:28
1  
The end result is not much different however my approach (using Tasks and async/await) is much easier to understand and read. It looks like normal synchronous code. The approach described in the article is the old approach before C# supported async/await. The newer versions of Visual Studio should support generating proxies with *Async methods that return Tasks and are friendly to my approach. –  Stilgar Jul 3 at 6:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.