views:

51

answers:

3

Hello,

Actually im doing a home page that only have an action called Index() that returns the view Index.ascx.

This index page will be composed by lastest news and lastest registered users, i think that create two partial views is the best idea (this way i could use it in other views).

for other hand i have a data access class that calls to database for get stuff (get last news, get last users, etc...)

My question is simple, should i call to the this data access class in the Index() action of my HomeController, and add to the ViewData the data obtained?

I think that this index() action shouldnt be the responsable of passing this data to the partial views, right?

Could you give me a hand?

im messing too much? ;-)

Thanks in advance.

Best Regards.

Jose

+2  A: 

Hi Jose,

you should use the Repository pattern to encapsulate the data access and separate it from the Logic / UI. The controller of the Index() method should access the repository and receive data from there.

A great tutorial to learn ASP.NET MVC is this one: NerdDinner. There, the usage of a repository class is explained, too.

By using a repository you access other advantages such as the possibility of Dependency Injection which is a great enhancement for testability ...

Marius Schulz
NerdDinner is indeed a great start. I also used it to get familiar with asp.net mvc.
Carra
+1  A: 

Hi Jose,

You're spot on in thinking it's ideal to separate the news and user list out to partial views as you can reuse them in other parts.

It's also ideal to separate the data functions for these out too into a separate class often call repository class. So you might have a News repository class which would have a GetNews function on there. You can then call that function from inside your news list. Anywhere else you want to get those lists you can just call that function and you're not writing the same data retrieval code again.

tooba
+3  A: 

Hi Jose ,

I suggest you use Action methods to control only to decide display content and not what it displays.

I would say encapsulate the Data Access logic in one more layer , so that it is loosely coupled from the controller actions.

This results in having 'Thinner actions and Coarser Models' . You might want to use a repository pattern as described in Scotts blog or follow Domain Driven Design which illustrates the responsibility of each of the layers.

Hope this helps.

Thanks , Vijay

vijaysylvester