if the usercontrol calls this service directly (bypassing a controller piece), aren't you breaking MVC?
Yes and no. Yes in terms of the WebController concept - no in terms of a "ServiceController" concept. If you wanted to keep with the literal MVC here you could - you could have your control use something like Ajax or jQuery to call back to a "StatsController'
and make a call to a List method that returns links. This is clean and nice - and works much the same way.
I readily break rules to do what makes sense - Gravatar is an example of this. If you have an app that uses Gravatar, it has to go out to an external service after hashing a user's email, so the process has to go through a code layer in your app. This doesn't
fit the MVC pattern exactly - but I think (my opinion) that this is OK, The Service model and MVC can fit together nicely.
It's all up to you and what you think fits your scenario. The good thing is that there are ways to do it elegantly :).
Coming from Monorail they have a similar concept with their ViewComponents. The component has a Initialize and Render method that can be overridden, so you can give your components/controls some logic
Two things strike me as a smelly:
1) Code Behind. I don't think there should be any, it will just get confusing and you will end with people doing some crazy stuff trying to mush webforms into mvc.
2) User Controllers. Out of the box there is no IoC so the default factory will new them up with Activator.Createinstance... bad, bad, bad
So what would my solution be.
I would create a ViewComponent base class for handling all my setup. It would probably just have an Initialize method taking ControllerContext. All my presentation logic would go in there so it is testable. all my domain logic would be done in services
that get injected into the constructor of my ViewComponent.
I would have a base ViewComponentControl that simply used IoC to locate the correct ViewComponent class and Initialize it. Now my UserControls just inherit from my ViewComponentControl, use the view like normal, and create a separate ViewComponent subclass
to handle any setup, config, and presentation logic.
I would probably run fxcop rules to ensure my code behind files were empty.
Other than the defect in MVC (which is easily fixed... see the link in my last post above), make your MasterViewData the base of your controller specific ViewData types (you could even populate this, if common, in a shared MyControllerBase).
robconery
Participant
852 Points
195 Posts
Re: Passing data to Master pages in ASP.NET MVC
Dec 15, 2007 07:24 AM|LINK
Yes and no. Yes in terms of the WebController concept - no in terms of a "ServiceController" concept. If you wanted to keep with the literal MVC here you could - you could have your control use something like Ajax or jQuery to call back to a "StatsController' and make a call to a List method that returns links. This is clean and nice - and works much the same way.
I readily break rules to do what makes sense - Gravatar is an example of this. If you have an app that uses Gravatar, it has to go out to an external service after hashing a user's email, so the process has to go through a code layer in your app. This doesn't fit the MVC pattern exactly - but I think (my opinion) that this is OK, The Service model and MVC can fit together nicely.
It's all up to you and what you think fits your scenario. The good thing is that there are ways to do it elegantly :).
abombss
Member
575 Points
164 Posts
Re: Passing data to Master pages in ASP.NET MVC
Dec 18, 2007 10:01 PM|LINK
Coming from Monorail they have a similar concept with their ViewComponents. The component has a Initialize and Render method that can be overridden, so you can give your components/controls some logic
Two things strike me as a smelly:
1) Code Behind. I don't think there should be any, it will just get confusing and you will end with people doing some crazy stuff trying to mush webforms into mvc.
2) User Controllers. Out of the box there is no IoC so the default factory will new them up with Activator.Createinstance... bad, bad, bad
So what would my solution be.
I would create a ViewComponent base class for handling all my setup. It would probably just have an Initialize method taking ControllerContext. All my presentation logic would go in there so it is testable. all my domain logic would be done in services that get injected into the constructor of my ViewComponent.
I would have a base ViewComponentControl that simply used IoC to locate the correct ViewComponent class and Initialize it. Now my UserControls just inherit from my ViewComponentControl, use the view like normal, and create a separate ViewComponent subclass to handle any setup, config, and presentation logic.
I would probably run fxcop rules to ensure my code behind files were empty.
[C.I.] Reman
Member
158 Points
44 Posts
Re: Passing data to Master pages in ASP.NET MVC
Mar 31, 2008 05:55 PM|LINK
English Blog: Code-Inside International
rjcox
Contributor
7064 Points
1444 Posts
Re: Passing data to Master pages in ASP.NET MVC
Apr 01, 2008 09:06 AM|LINK
What's wrong with ViewData?
(But see here if you want it strongly typed.)
[C.I.] Reman
Member
158 Points
44 Posts
Re: Passing data to Master pages in ASP.NET MVC
Apr 01, 2008 09:21 AM|LINK
The problem with viewdata is, that i only use viewdata on view pages.
Each Controller has different Views (with there viewdata) - but the masterpage has no real "master-controller".
That means, each controller must pass the "master-viewdata" for the masterpage - this isn´t a great solution in my opionion.
English Blog: Code-Inside International
rjcox
Contributor
7064 Points
1444 Posts
Re: Passing data to Master pages in ASP.NET MVC
Apr 01, 2008 09:43 AM|LINK
Other than the defect in MVC (which is easily fixed... see the link in my last post above), make your MasterViewData the base of your controller specific ViewData types (you could even populate this, if common, in a shared MyControllerBase).
[C.I.] Reman
Member
158 Points
44 Posts
Re: Passing data to Master pages in ASP.NET MVC
Apr 01, 2008 10:00 AM|LINK
Thanks - this could be a nice solution.
English Blog: Code-Inside International
maartenba
Member
368 Points
76 Posts
Re: Passing data to Master pages in ASP.NET MVC
Apr 02, 2008 06:28 AM|LINK
Check http://weblogs.asp.net/mikebosch/ for using ComponentController class. It's very useful for displaying small blocks of data in a master page!
Order my book ASP.NET MVC 1.0 Quickly via http://www.packtpub.com/asp-net-model-view-controller-1-0-quickly/book
eliperelman
Member
2 Points
1 Post
Re: Passing data to Master pages in ASP.NET MVC
Aug 27, 2010 01:44 AM|LINK
I know it's a little late for a reply, but for anyone else who runs across this:
theuday
Member
2 Points
1 Post
Re: Passing data to Master pages in ASP.NET MVC
Mar 10, 2011 11:57 AM|LINK
I came across the same situation when i wanted to create a menu in mastepage dynamically with data from a entity data model. I used