开发者

ASP.NET MVC where does one write query logic

I'm studying for a MS certificate. I already bumped into 3 mistakes in the test and found confirmation online.

Now I didn't really find a clear answer, so I'm posting it here.

Here's the question:

You are maintaining an existing MVC application. Users report that when querying the database for a list of products that meet specific criterion, some products do not appear in the list. The original application developer followed common MVC structure conventions. You need to locate the file that is causing the problem.

In which folder should you look for the file?

The possible answers are:

  • Controllers
  • Models
  • Views
  • Content

I would just like to know if it is I who is confused or开发者_Go百科 if the question/answer itself is confusing.


Controllers is most likely the correct answer

This is a very confusing question, because its answer largely depends on your DAL strategy/library you're using. If it said you're using EF then the answer would clearly be Controllers, but if you'd be using anything more programmatic (ie repositories) then things would become more confusing.

But. When you'd be using repositories you'd probably put them in a separate project (assembly) anyway so it wouldn't be any of listed folders (if we're talking about RAW queries).

So in the case of repositories, you could still argue that data fetching/manipulation would still be done in Controllers even though it probably wouldn't be direct data querying (SQL, eSQL or LINQ) but rather calling into repositories (or even better Services that call into repositories).

What's in Models anyway?

Models usually contain classes. Either very very simple ones (as POCOs and/or DTCs) or maybe they do contain some more intelligent classes (as Entities). But in any case, you wouldn't be writing queries in classes inside Models folder but rather in Controllers that would use Models classes. Either thin or rich ones.


I would answer 'controllers' since the model (to me anyway) is dumb data the view needs in order to present itself. However, the actuall query would be implemented in a query layer.

Now there are folks that regard models as a higher concept representing business data and data access --- I just happen to not be one of them :)


I would say the answer is the models folder. I don't think it's confusing. The models would be where your data models would be and would most likely deal with interacting with the database. If nothing else I think the answer is obvious by process of elimination.


Models - where data access services / data models are defined. As opposed to Controllers which orchestrate UI logic for Views which may, in turn, use images and styles found in Content.


The answer would be models. This is what is/was known as your Data Access Layer. Controllers are you Business Logic that orchestrate data passing to and from DAL to presentation layer (in MVC this is Views). Views, as I just stated are presentation layer. Content usually contains images and CSS file(s).

Regards,

Huske


It can be either Controller or Model, depending on the way query is called , but if you read the question, it gives you the answer itself as it emphasize on the use of mvc pattern , which means all the data access logic is written in model , therefore the problem is in the model.


I think the answer should be Models, but it could be Controllers as you need to check both places really. There should be code in a Controller which calls the Model and says something like "Please give me a list of products satisfying these criterion".

It is of course possible that the code in the Controller is asking the Model for the wrong thing, of course! That's why I hate exam questions like this!

I and lots of other people tend to put this code (the Model stuff)in a Domain layer (separate project) as it is business-specific, meaning it would not change even if you changed the front end from web to winforms or whatever.


Idealy controllers should be as lean as possible. So except small ones, writing queries to controllers is not wellcomed in my opinion. You can check The Repository Pattern to have an example.

ASP.NET MVC where does one write query logic

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜