How about a very complex page which need many "MVC modules/parts" ?http://forums.asp.net/t/1196142.aspx/1?How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Wed, 26 May 2010 23:16:50 -040011961422067944http://forums.asp.net/p/1196142/2067944.aspx/1?How+about+a+very+complex+page+which+need+many+MVC+modules+parts+How about a very complex page which need many "MVC modules/parts" ? <p>How about a very complex page which is composited by many different modules? e.g. a MSN home page alike web page, it need to display latest news, latest weather forecast, latest blog, forum updates, ...</p> <p>For each module, it's pretty clean to make it a full &quot;MVC&quot; cycle, but if we mix multiple M together, mix multiple V&nbsp; together and make it one MVC cycle, it not that neat.&nbsp; Any good idea on design web sites like this?</p> <p>&nbsp;<br> --</p> <p>Some similar idea is, a master page could contain multiple place holders for different piece of contents, it's clean if we handle each part of them in a MVC cycle, but how to do it in current ASP.NET MVC?</p> <p>&nbsp;</p> <p>&nbsp;</p> <p><br> &nbsp;</p> 2007-12-19T10:36:06-05:002068321http://forums.asp.net/p/1196142/2068321.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>Sounds like you want &quot;MVC for Web Parts&quot; where the rendering of the overall page, and separately the parts goes through Controller &#43; Model View.&nbsp;</p> <p>However web parts are closer to user controls than pages in their own right, and there is a lot of provider support associated with their use, so I'm not sure that MVC CTP #1 is a viaiable starting point for this.</p> <p>However MVC view user controls could be a starting point (seem to be ASP.NET User Controls &#43; Html, Url etc. helpers plus access to ViewData).<br> </p> 2007-12-19T13:42:08-05:002068719http://forums.asp.net/p/1196142/2068719.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>&nbsp;This has always been a small paint point for me and MVC web frameworks in general.<br> </p> <p>Coming from the Monorail world I have solved this in the past by using ViewComponents very similar ot UserControls.</p> <p>Lets call my controller/view Dashboard/Index.&nbsp; My Index view page would mostly delegate to usercontrols passing ViewData that I set in my controller.&nbsp; Monorail has the concept of a dynamic action, meaning I can decorate my controllers with actions from other places without actually writing methods on my controller. So my options now become.<br> </p> <ol> <li>Have UserControls provide actions on the controller.&nbsp; This gets a little crazy with naming collisions in large apps.</li><li>Create full blown controllers for the logic of your user controls and you would have the framework partially render them.&nbsp; Anything they had to do would callback to their dedicated controller.</li><li>Add actions to your controller to handle everything with AJAX.&nbsp; The UserController would call actions on your controller that would have to be there.<br> </li></ol> <p>Option 1 is confusing, it would require a little plumbing, and I am not totally sold on it.</p> <p>Option 2 I have personally always liked option 2 the best, the problem is now my UserControllers need to be aware of my application to do anything involving persistence, security, etc.&nbsp; Check this <a href="/t/1194413.aspx">thread</a>, I have a late response on how I would handle user controls without code behind.</p> <p>Option 3 Don't know why, but I just don't like option 3.&nbsp; Seems like too much client side setup with script. &nbsp;</p> <p>Following Option 2, if I wanted some really reusable controls, I think I would just make my usercontrol's controller virtual and use a template method approach for creating protected methods that the end users can override to do what they need.&nbsp; Now I just drop in my user control, I tell it where its controller is and I am done.<br> </p> <p>I am interested to see what MS recommends for third party components.&nbsp; I hope to God it does not involve code behind.&nbsp;</p> 2007-12-19T17:29:39-05:002068794http://forums.asp.net/p/1196142/2068794.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>You'd want to use UserControls (similar to Rails partials) that you can Render using RenderUserControl(); In terms of where the logic resides - I always tell people to create Services that do this (similar, in a way, to Rail's plugins).</p> <p>In Rails if you want to have a certain bit of functionality - like GoogleMaps or something - you download a Plugin and then &quot;strap it&quot; to the ApplicationController (or whichever controller you're using it in). In a sense what you're doing is extendin the controller by doing this.</p> <p>You can use this same concept in C#/MVC by using a &quot;Service&quot; - an encapsulated bit of logic that does something specific, and is &quot;horizontal&quot; to your application (meaning it can be used everywhere). So if you wanted to get NYSE quotes, you could by 1) creating a NYSEService class (or call it what makes sense to you) that handles the logic and 2)Create a UserControl that exposes it.</p> <p>I did this with the new SubSonic forums (not release yet) because I needed Akismet (for spam) and CSharpFormat for posting. You can put this stuff in your project, or keep it in a DLL externally.</p> 2007-12-19T18:01:25-05:002068873http://forums.asp.net/p/1196142/2068873.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>robconery</h4> I always tell people to create Services that do this </blockquote> &nbsp;[Yes] <p></p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>robconery</h4> then &quot;strap it&quot; to the ApplicationController </blockquote> <p></p> <p>When did C# get mixins?</p> <p>I think the problem comes when you have portal like pages where you want to drop in functionality in some type of portlet.&nbsp; You can't add methods on your controller for every scenario.&nbsp; What happens when my usercontrol has some ajax capabilities, what controller/action is it calling to?&nbsp; What happens when my usercontrol wants to persist perferences or options?<br> <br> &nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> 2007-12-19T18:47:36-05:002069164http://forums.asp.net/p/1196142/2069164.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>&nbsp;Thanks Abombss, Rob. </p> <p>&nbsp;Yes, this is a general problem...so I am wondering if ASP.NET will have some neat solution. <br> </p> <p>&nbsp; As my understanding partials in RoR is just a piece of presentation code (should belongs to View), so actually I had same problem when I played on RoR before. (though it's possible to put some M&amp;C code inside partial, it's ugly)&nbsp; As you mentioned we can archive the same goal with RenderUserControl() in ASP.NET. </p> <p>&nbsp; I think the key problem is not in the view part, it's inside M &amp; C.&nbsp; A dashboard web page could pull all kind of data in the database, if we mix all those inside the M&amp;C, it will be a mess.&nbsp; Yes, we can put those modulized logic into &quot;services&quot;, we can even encapsulate them into different classes / assemblies / ..., however we need a place to put them together...then we may get a messed up &quot;action&quot;. Image a real complex application, many actions may contain similar codes though those are just simple calls to some encapsulated services. <br> </p> <p>&nbsp; e.g. </p> <p>&nbsp;&nbsp;&nbsp; public class BigWebController : Controller {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ControllerAction]</p> <p>&nbsp;&nbsp;&nbsp;&nbsp; public void Index() </p> <p>&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LatestNewsService latestNews = new LatestNewsService(); </p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StockQuotoService stock = new StockQutoService(); </p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...&nbsp;&nbsp; <br> </p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // we at least need to create and call those services somewhere, right?</p> <p><br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; RenderView(...); <br> </p> <p>&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;&nbsp;&nbsp;&nbsp; public void MyCustomizedDashboard() </p> <p>&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // we may have similar code here, right? <br> &nbsp;&nbsp; </p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RenderView(&quot;...&quot;)<br> &nbsp; </p> <p>&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp; </p> <p>&nbsp;&nbsp;&nbsp; }<br> </p> <p>For the views, we can use user control, or simple &quot;include&quot; some aspx files which render partial content (is the include a good idea? I am very new to asp.net, but I did quite a lot such include in Java web develop)&nbsp; Since we have prepared all required data in M, C,&nbsp; we will get whatever we want in the V. <br> </p> <p>What if we need to add some &quot;component&quot; to a web page?&nbsp; Oh, besides change the view aspx files, we have to change the controller code, even those component are remain exactly same in another page! &nbsp;</p> <p>This is not a neat enough idea sounds to me, because we not treat it as &quot;component&quot;, we separated something which could done perfectly in a full M-V-C cycle into&nbsp; &quot;service&quot; and &quot;partial&quot; . <br> </p> <p>If we need to consider the cache, the logic could be much complex: we may have a bunch of condition code somewhere to get &quot;Model&quot; from cache, but how about the view? if Model can be get from cache, why we render view again? would it better to directly pull the result from cache? <br> </p> <p>&nbsp;<br> -----</p> <p>What I did before is: (I didn't use asp.net before, so write some Java related stuff, but I believe it similar) <br> </p> <p>(1) Keep MVC stuff simple and <b>only do one task</b> in each MVC cycle. (It's not conflict to implement stuff as service)</p> <p>(2) Use another container page to &quot;include&quot; each part of the &quot;components&quot;, this could be a simple jsp page or another &quot;MVC&quot; cycle. </p> <p>I think my solution have those advatange:<br> <br> *&nbsp; it's like real &quot;component&quot;, they are self-contained. </p> <p>* change the finally page is simple, just change the external contain page, you get different result. (the project I worked before, the web design changes from time to time, maybe we just have poor design and decision but that's real in many teams I believe... ) &nbsp;</p> <p>* it's easy to switch from &quot;service side include&quot; to &quot;ajax include&quot;, just change the container page, all component remain unchanged!&nbsp; In one of my project, we even switch between the 2, if user agent support javascript, we use ajax, if not we use server side include,&nbsp; and it's very SEO friendly. </p> <p>* <b>The huge benefit I think is CACHE</b>. We can have many level cache to archive best performance, in the container page level, we can use cached result if nothing changed from any inside component, for&nbsp; &quot;component&quot; they have cache for themselves, and the cache mechanism is same for the whole page and the &quot;component&quot;, it's simple and very efficient. </p> <p>&nbsp;I think the solution I have is like a &quot;reversed&quot; master page: the &quot;container&quot; page dispatch to each &quot;component&quot; and composite into a final web page.&nbsp; ASP.NET's master page is a great idea, however I am wonder if how to archive the above dash board alike application easily and keep the design neat and clean. </p> <p>---</p> <p>Of course, maybe I am wrong or maybe there are much better solution for this, that's what I am looking for and why I asked the question here. &nbsp;&nbsp;</p> 2007-12-19T21:25:52-05:002069462http://forums.asp.net/p/1196142/2069462.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>&nbsp;I've got the same problem with master page which include M-V-C modules. I search on the net, ROR or PHP MVC have method: render_component which will render a view to solve this problem<br> </p> <p>&lt;html&gt;</p> <p>&lt;body</p> <p>&lt;div id=&quot;leftCol&quot;&gt;</p> <p>&nbsp;&nbsp; render_component(Controller=&quot;Category&quot;, Action = &quot;List&quot;, {param1=value1, param2=value2})&nbsp; </p> <p>&lt;/div&gt;&nbsp;</p> <p>&lt;div id=&quot;mainCol&quot;&gt;</p> <p>&nbsp;&nbsp; render_component(Controller=&quot;Product&quot;, Action = &quot;List&quot;, {param1=value1, param2=value2, param3=value3}) <br> </p> <p>&lt;/div&gt;&nbsp;</p> <p>&lt;/body&gt;&nbsp;</p> <p>&lt;/html&gt;&nbsp;</p> <p>Can we have any same solutions for asp.net mvc ?</p> <p>&nbsp;</p> <p>&nbsp;</p> &nbsp; <p>&nbsp;</p> 2007-12-20T01:39:35-05:002069513http://forums.asp.net/p/1196142/2069513.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>convit</h4> ROR or PHP MVC have method: render_component which will render a view to solve this problem</blockquote> <p></p> <p>I like this, conceptually, they are all seperate actions, so it's very natural, this gives you a good way of composing complex views without breaking the mvc. Got my vote too.</p> 2007-12-20T02:20:37-05:002069548http://forums.asp.net/p/1196142/2069548.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>How is that any different than &lt;uc:MyUserControl /&gt;?</p> <p>How does your CategoryController/List action know what view to render, and know that it only needs to render a partial?<br> </p> <p>&nbsp;</p> 2007-12-20T02:44:52-05:002069575http://forums.asp.net/p/1196142/2069575.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>If we use &lt;uc:MyUserControl&gt; : we will breaking the MVC pattern because of view is direct accessed not throught its controller, &nbsp;</p> <p>CategoryController/List, List Action already know what view to render, I dont know how to implement this idea :(</p> <p>I have another question:</p> <p>How to pass data cross modules ? With classic asp.net mode, I use query string to pass data. </p> <p>But with MVC URL is very important to determine Controller, Action, with one URL, we only determine one controller, one action deal with that URL.</p> <p>But in complex View scene, we have to instance many controller, each controller mush have data to process itself.</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> 2007-12-20T03:00:31-05:002069592http://forums.asp.net/p/1196142/2069592.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>convit</h4> But in complex View scene, khi have to instance many controller, each controller mush have data to process it selt.</blockquote> <p></p> <p>The flow will look something&nbsp;like this. Take a portal type page with multiple components, most likely you hit the home controler/action first, inside the action you&nbsp;read the db and&nbsp;load up some profile settings for each individual component on your portal page, pass that to the view, inside the view, appropriate component is rendered with the setting you read from the db. preserves mvc. Basically, you either pass it through the original url when appropriate, or count on the first action to figure it out. Think of it as a parent action with a bunch of child actions, parent will tell the children what to do.</p> 2007-12-20T03:16:35-05:002069652http://forums.asp.net/p/1196142/2069652.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>&nbsp;</p> <blockquote><span class="icon-blockquote"></span> <h4>convit</h4> If we use &lt;uc:MyUserControl&gt; : we will breaking the MVC pattern because of view is direct accessed not throught its controller, &nbsp;</blockquote> <p></p> <p>Where does it say a view cannot call another view in MVC?&nbsp; We get our MVC separation because the model does not depend on the view.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>convit</h4> How to pass data cross modules ? With classic asp.net mode, I use query string to pass data.</blockquote> &nbsp; <p></p> <p>Through parameters, everything has access to the RequestContext and if using UserControls you can use parameters.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>convit</h4> But in complex View scene, khi have to instance many controller, each controller mush have data to process itself.</blockquote> &nbsp; <p></p> <p>This is why this is a weird idea.&nbsp; Why would you call an action on a 2nd controller just to render output?&nbsp; You call a controller to take action.&nbsp; Outputing a component will have no side effects.&nbsp; Only if the component needs to modify or presist state in some way does it need a controller to call into an action.&nbsp; I only want one action happening per request, otherwise it will turn into a bloody nightmare.&nbsp;</p> 2007-12-20T03:58:45-05:002069661http://forums.asp.net/p/1196142/2069661.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>abombss</h4> How is that any different than &lt;uc:MyUserControl /&gt;?</blockquote> <p></p> <p>Unlike user controls that will break the mvc pattern, render component will not.</p> <p>Take implementing this forum for example, there's a top 10 poster list on the right panel. The obvious solution is we will make a master page that contains a Top10UserControl. now let's say we hit</p> <p><a href="http://asp.net/forums/index">http://asp.net/forums/index</a></p> <p>how do we load the data into this Top10UserControl, one option is have the user control do it, but that is not mvc. option 2 is have ForumsController.Index load up the data and pass it on, but there's a problem, what if we hit other pages</p> <p><a href="http://asp.net/forums/1">http://asp.net/forums/1</a><br> <a href="http://asp.net/forums/1/posts/index">http://asp.net/forums/1/posts/index</a><br> <a href="http://asp.net/forums/1/posts/1000/comments/new">http://asp.net/forums/1/posts/1000/comments/new</a><br> ...</p> <p>all these actions ForumsController.Show(forumId), PostsController.Index(forumId), CommentsController.New(postId) etc all have to know to load up the Top10 data for the master page. What if we decided to add something else to the master page? Code has to be changed in lots of places. We can refactor and make an utility function called GetMasterPageData(), but every action still have to remember to invoke that, not very clean. Maybe we use&nbsp;the template pattern so you will never forget to call GetMasterPageData(), but that's an extra class on the inheritance hierarchy.</p> <p>With the render component approach, you don't have to worry about any of that, the master page will call render component with { controller = &quot;top10&quot;, action = &quot;index&quot;, forumId = forumId }. Just like you could alternatively have an ajax call on the master page that invokes <a href="http://asp.net/top10/index/1">http://asp.net/top10/index/1</a> and retreve a html fragment, except render component is done in the same request.</p> <p>When you think about it, it's a pretty simple and neat solution.</p> 2007-12-20T04:04:47-05:002070024http://forums.asp.net/p/1196142/2070024.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>Thanks <b>shinakuma</b> for the reply.<br> Could you please help me to implement a helper function with prototype like this:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>ControllerHelper.ExecuteAction(string url, object defaults, bool isAjaxRender) ;</b></p> <p><b>url</b>: we will build a url string to simulate a call to action,<br> <b>defaults</b>: we will provide some other information for action<br> <b>isAjaxRender</b>: true if we want content will be render on client side, false if we want to execute action and render html in server side<br> </p> <p>Example: </p> <p>&lt;div&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;% ControllerHelper.ExecuteAction(&quot;Post/Top10&quot;, {Controller=Post}, false);%&gt;<br> &lt;/div&gt;&nbsp;</p> <p>or a control like this</p> <p>&lt;div id=&quot;leftCol&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;mvc:ExecuteAction runat=&quot;server&quot; Url=&quot;...&quot; Defaults=&quot;...&quot; id=&quot;mvcCategory&quot; /&gt; <br> &lt;/div&gt;<br> &lt;div id=&quot;mainCol&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;mvc:ExecuteAction runat=&quot;server&quot; Url=&quot;...&quot; Defaults=&quot;...&quot; id=&quot;mvcListProduct&quot; /&gt; <br> &lt;/div&gt;<br> </p> <p>&nbsp;<br> DTK&nbsp; </p> 2007-12-20T08:33:00-05:002070036http://forums.asp.net/p/1196142/2070036.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>&nbsp;</p> <blockquote><span class="icon-blockquote"></span> <h4>convit</h4> <p></p> <p>&lt;div id=&quot;mainCol&quot;&gt;</p> <p>&nbsp;&nbsp; render_component(Controller=&quot;Product&quot;, Action = &quot;List&quot;, {param1=value1, param2=value2, param3=value3}) <br> </p> <p>&lt;/div&gt;&nbsp;</p> <p></p> </blockquote> <p></p> <p>Yes,&nbsp; I also love this idea.&nbsp; </p> <p>This is exactly like what I said, use a container page to separate a whole page into many small M-V-C components. </p> <p>We can use user controls to archive that, since we can put whatever parameter inside Request, Response, or even session objects, but <b>that is not neat</b>. <br> </p> <p>I wish asp.net could bring us something looks neat, simple, easy to understand.&nbsp; </p> <p>I don't think rendercomponent() will break the M-V-C, it's more straight forward.&nbsp; </p> <p>&nbsp;</p> <p><br> &nbsp;</p> 2007-12-20T08:39:04-05:002070520http://forums.asp.net/p/1196142/2070520.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>I think there are a couple valid MVC methodologies being debated here:</p> <ol> <li>&lt;div mce_keep=&quot;true&quot;&gt;<strong>The Primary-Controller Method</strong>, where one main Controller is essentially responsible for the start-to-finish processing of the Request/Response<br> This is the method that the ASP.Net MVC CTP is currently geared towards, and understandably so. It resembles classic ASP.Net page processing while adding on basic MVC concepts and architecture. In this scenario the primary controller is responsible for loading all data and passing it to the view(s) for rendering. The main view in this instance is the .aspx Page (and optional Master Page templating). Subcomponent rendering can be delegated to partial views (i.e. .ascx User Controls) but the problem becomes how these partial views get the data they need for rendering. One way is to have the Primary Controller load the necessary data and pass it to the User Control for rendering. Another is to have the User Control call a &quot;Service&quot; to generate the data itself. Another is to simply break the MVC boundaries and load the data directly in the User Control itself.<br> &nbsp;&lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;<strong>The&nbsp;Multiple-MVC&nbsp;Method</strong>, where processing is handled initially by an MVC set, which can, in turn, call other MVC&nbsp;sets to handle sub-component processing<br> This is the methodology that I believe (correct me if I am wrong) shinakuma and others are describing. In this scenario page processing starts at an initial Controller just as with methodology #1. However, views can optionally&nbsp;delegate subcomponent rendering&nbsp;to an entire sub-MVC set which is responsible for&nbsp;its own available actions and&nbsp;output. In fact, this sub-MVC set could also call other MVC sets to do nested rendering.&lt;/div&gt;</li></ol> <p>I think the main difference between the two lies in the definition of the &quot;MVC&quot; concept. #1 has a more rigid scope: MVC is a&nbsp;Page-specific rendering concept. #2 defines it more generally. I tend to lean towards #2 because I agree that it allows for a more natural separation of responsibilities.&nbsp;I&nbsp;feel some changes to the ASP.NET MVC framework would make developing in that way easier to manage:</p> <ul> <li>&lt;div mce_keep=&quot;true&quot;&gt;As others have suggested, implement a way to delegate processing to a separate MVC set. The current MVC Toolkit is close to this with its Html.RenderUserControl() method. What is needed is the ability to specify a MVC resource by URI and to&nbsp;have that URI processed by the same Route engine as other MVC routes. Something like:<br> Html.RenderComponent(String uri, object postData). The second parameter would be optional, allowing for data to be passed directly&nbsp;to the destination URI that would normally have been sent&nbsp;via a form.post.&lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;When working&nbsp;with this method, there is no tangible difference between .ascx and .aspx controls. I would recommend getting rid of this distinction by creating a new extension to define &quot;Views&quot; such as .view. This extension should also have no code-behind page requirement (although it could be optional). Instead of adding an @Page or @Control directive, we could have an @View directive instead... with a potential MasterView property to allow for nested View templates.&lt;/div&gt;</li></ul> <p>I think a compounding factor in this discussion is the concept of Master Pages. As currently structured, Master Page templates are essentially partial Pages themselves. The problem is that this relationship is backwards from an MVC standpoint (at least in my mind). The Page refers to its Master... and thus has to pass data up the chain in order to get data to the Master Page for rendering. As others have noticed, this presents an odd problem in the MVC world as the Master has no Controller for itself, and thus any data it needs must ultimately come from the Page Controller. Note that this problem remains with both #1 and #2 so long as Master Pages are involved in the way they are currently architected. The best solution I have come up with for this is to have a base Controller class that handles data generation required for a particular Master template. Any Views that reference this Master would require a Controller that inherits from the MasterController base class. This allows one central data-loading point for all data required by the Master view. Not ideal... but workable for now. </p> <p>Thinking more about #2 it almost seems like the entire Master concept isn't needed at all. If you can define a parent MVC set, and then dynamically call sub-MVC sets then you essentially have a templating framework already. The only catch is that you are forcing the relationship to go the other way (instead of View referencing Master, Master dynamically references View). In order to do that it seems like another level of Controller (similar to the Route&nbsp;controller) &nbsp;would be needed to handle the linkage. I guess that would be the third thing I would like to see added to the MVC framework at some point in time.</p> <p>-Ian</p> <p>&nbsp;</p> 2007-12-20T13:35:37-05:002070583http://forums.asp.net/p/1196142/2070583.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>&nbsp;Ian, great summary of above discussion!</p> <p><br> &nbsp;</p> 2007-12-20T14:00:36-05:002071602http://forums.asp.net/p/1196142/2071602.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>&nbsp;By the way Render_Component was <a href="http://rubyonrails.org/deprecation"> deprecated </a>in RoR in favor of partials which are simply UserControls in MS MVC.&nbsp; Apparently they never belonged there and caused <a href="http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails"> problems</a>.<br> </p> 2007-12-21T00:02:00-05:002071674http://forums.asp.net/p/1196142/2071674.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p>I've read the arcticle, so many people replied that they want to use component for reuse purpose,<br> I think the issue of using render component is performance only. But render component will help us alot in seperate business, view into smallcomponents and reuse it.</p> <p>&nbsp;</p> <blockquote><span class="icon-blockquote"></span> <p></p> <p><cite><b>Urbanus</b>:</cite><br> Components != partials<br> Partials are view-related: they render information within the context of the current controller.<br> Components are controllers &#43; views that you can put into a view that belong to some other controller (or controllers). They are different from partials in that they are designed to gather and present their own data separately from the controller thats actually preparing the current page.<br> Partials dont replace components. Partials are not a better sort of component.<br> If components ever get deprecated, the DRY implications for applications that genuinely need them will be disastrous. All that controller &#43; view code will have to be copied across multiple different controllers.<br> With respect kev, if you dont see the importance of components then you just havent personally found a legitimate use for them yet. But that doesnt mean theyre not a powerful and important part of Rails.<br> &nbsp;<br> <cite><b><a href="http://gabrito.com/">Todd Huss</a></b> </cite><br> </p> <p>Great article, it helps to have it summarized in one place!<br> However, I agree with the aforementioned comments that components are extremely useful. They make developing portal like functionality where you want an area of dynamic content repeated on different pages very simple and DRY.<br> I have been unable to find a similar approach to combining business logic and view in a reusable component in an equally DRY manner without using components. It would be helpful if someone could provide an example of how to effectively rewrite a reusable component (such as a web poll) without using components. &nbsp;</p> </blockquote> <p>&nbsp;</p> 2007-12-21T01:06:15-05:002071758http://forums.asp.net/p/1196142/2071758.aspx/1?Re+How+about+a+very+complex+page+which+need+many+MVC+modules+parts+Re: How about a very complex page which need many "MVC modules/parts" ? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>abombss</h4> By the way Render_Component was <a href="http://rubyonrails.org/deprecation">deprecated </a>in RoR in favor of partials which are simply UserControls in MS MVC.</blockquote> <p></p> <p>Explains why Monorail didn't have this concept either. That's very unfortunate. I understand that the problem overusing it will cause. And I also agree with the sentiment that probably 95% of the time, partials are a better way for view partitioning. But I still think that there is definitely a place for components. I like this controller-action seperation over before filters.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>convit</h4> Could you please help me to implement a helper function with prototype like this:</blockquote> <p></p> <p>At first, I wasn't planning on doing this. I briefly played around with the routing features of the drop. Other than that, I haven't had the chance to really dig into this ctp. But curiosity got the best of me and I decided to give it a shot. Gave me a chance to understand the internals of ms-mvc a little better. Bewarned, it's just a proof of concept. There might be a better way of doing it. </p> <p>In your ViewPage or ViewMasterPage, just do something like&nbsp;&lt;%= this.RenderComponent( new { Controller=&quot;Top10&quot;, Action=&quot;Index&quot;, ForumId = 1 } ) %&gt;, that will jump out, invoke Top10Controller.Index(1) and spit out the fragments. Inside the action, you should have access to everything in the context.</p> <font color="#0000ff" size="1">public</font><font size="1"> </font><font color="#0000ff" size="1">static</font><font size="1"> </font><font color="#0000ff" size="1">class</font><font size="1"> </font><font color="#2b91af" size="1">RenderComponentExtension<br> </font><font size="1">{<br> </font><font size="1"><br> </font><font color="#0000ff" size="1">public</font><font size="1"> </font><font color="#0000ff" size="1">static</font><font size="1"> </font><font color="#0000ff" size="1">string</font><font size="1"> RenderComponent(</font><font color="#0000ff" size="1">this</font><font size="1"> </font><font color="#2b91af" size="1">ViewPage</font><font size="1"> viewPage, </font> <font color="#0000ff" size="1">object</font><font size="1"> values)<br> {<br> </font><font color="#0000ff" size="1">string</font><font size="1"> path = viewPage.Url.Action(values);<br> </font><font color="#2b91af" size="1">IHttpContext</font><font size="1"> context = viewPage.ViewContext.HttpContext;<br> </font><font color="#0000ff" size="1">return</font><font size="1"> RenderComponent(path, context);<br> }</font><font size="1"> <p></font><font color="#0000ff" size="1">public</font><font size="1"> </font><font color="#0000ff" size="1">static</font><font size="1"> </font><font color="#0000ff" size="1">string</font><font size="1"> RenderComponent(</font><font color="#0000ff" size="1">this</font><font size="1"> </font><font color="#2b91af" size="1">ViewMasterPage</font><font size="1"> masterPage, </font><font color="#0000ff" size="1">object</font><font size="1"> values)<br> {<br> </font><font color="#0000ff" size="1">string</font><font size="1"> path = masterPage.Url.Action(values);<br> </font><font color="#2b91af" size="1">IHttpContext</font><font size="1"> context = masterPage.ViewContext.HttpContext;<br> </font><font color="#0000ff" size="1">return</font><font size="1"> RenderComponent(path, context);<br> }</p> </font><font color="#0000ff" size="1">private</font><font size="1"> </font><font color="#0000ff" size="1">static</font><font size="1"> </font><font color="#0000ff" size="1">string</font><font size="1"> RenderComponent(</font><font color="#0000ff" size="1">string</font><font size="1"> path, </font><font color="#2b91af" size="1">IHttpContext</font><font size="1"> context)<br> </font><font size="1">{<br> </font><font color="#0000ff" size="1">string</font><font size="1"> oldPath = context.Request.Path;<br> </font><font size="1"><br> context.RewritePath(path);<br> <br> </font><font color="#2b91af" size="1">RouteData</font><font size="1"> routeData = </font><font color="#2b91af" size="1">RouteTable</font><font size="1">.Routes.GetRouteData(context);<br> </font><font color="#2b91af" size="1">Type</font><font size="1"> handlerType = routeData.Route.RouteHandler;<br> </font><font color="#2b91af" size="1">IRouteHandler</font><font size="1"> routehandler = (</font><font color="#2b91af" size="1">IRouteHandler</font><font size="1">)</font><font color="#2b91af" size="1">Activator</font><font size="1">.CreateInstance(handlerType);<br> </font><font color="#2b91af" size="1">RequestContext</font><font size="1"> requestContext = </font><font color="#0000ff" size="1">new</font><font size="1"> </font><font color="#2b91af" size="1">RequestContext</font><font size="1">(context, routeData);<br> </font><font color="#2b91af" size="1">IHttpHandler</font><font size="1"> httpHandler = (</font><font color="#2b91af" size="1">MvcHandler</font><font size="1">)routehandler.GetHttpHandler(requestContext);<br> </font><font size="1"><br> <font color="#2b91af">TextWriter</font><font size="1"> oldWriter = context.Response.Output;</font><br> </font><font color="#2b91af" size="1">StringWriter</font><font size="1"> newWriter = </font><font color="#0000ff" size="1">new</font><font size="1"> </font><font color="#2b91af" size="1">StringWriter</font><font size="1">();<br> context.Response.SwitchWriter(newWriter);<br> httpHandler.ProcessRequest(</font><font color="#2b91af" size="1">HttpContext</font><font size="1">.Current);<br> <br> context.RewritePath(oldPath);<br> context.Response.SwitchWriter(oldWriter);<br> <br> </font><font color="#0000ff" size="1">return</font><font size="1"> newWriter.ToString();<br> }</font><font size="1"> <p>}</p> </font> 2007-12-21T02:40:07-05:00