Why Orchard doesn't have model view controller? - asp.net-mvc

I am working with orchard. writing such a project is my dream!.. so i started a research a bout that. which interested me about orchard, is while orchard is mvc project but why it doesn't have any model , view and controller in his web layer solution? does it use CleanArchitecture or some specific architecture like this ?
i tried to know about this case in orchards documents but i didn't find any description about it.

Actually, Orchard being an MVC based project, it intrinsically does have models, views and controllers. But it provides much more than that.
The key thing to understand, in my mind, is that at the very core, requests are handled by a given controller, which builds a model, and returns a view that uses this model. What Orchard adds to that is how it builds up a particular model, and how it selects the view to display that model.
Consider requesting a content item by navigating to, say, a content item with alias "/about".
What happens is that the ASP.NET routing will kick in, which has been configured to match against aliases of all content items. The route for "/about" will be found, which is handled by the ItemController of the Contents module (modules in Orchard are conceptually the same as MVC areas). The ItemController will build a dynamic model for the requested content item, which is called a shape. The shape is an instance of the Shape class, which contains metadata about the shape, such as the name of the shape. Based on this information, Orchard leverages the view engine to select the appropriate Razor view to render the shape object.
So you see, all of the basic MVC stuff comes into play. Orchard simply adds a powerful infrastructure on top of it to provide an advanced and flexible rendering system, like turning content items into shapes, which are then turned into HTML via the view engine. But at the end of the day, it's primarily about controllers creating models used by views.

Related

What is the right solution for Menu from database => base view model for layout vs model in httpcontext ()

I am trying to figure out what is the right solution for creating a menu in a ASP.NET. The menu has items and subitems and are subject to user rights. So I have a MenuService that returns the menu for the current user. However I do not know how to implement the menu.
So far I have found 3 possible solutions:
Define a base view model to add on the Layout, and every page view model should derived from this (This would mean that the IMenuService needs to be on every controller)
Use a base controller where I attach the IMenuService and that is going to be attached to the HttpContext.Items before every action (based on this https://dtoncode.wordpress.com/2012/07/30/strongly-typed-models-on-your-layout/)
Define a Controller for menu and in layout call a partial view to place the menu on it.
Which of these solutions are the right one for the job? Are there others?
You could use MvcSiteMapProvider for this - it is very similar to the Web Forms SiteMap provider model, but made for MVC.
Basically, the entire SiteMap hierarchy is loaded into a cache and shared between users. You can configure the nodes in many ways - to load them from a database, you can use one or more dynamic node providers. It has a security trimming feature that automatically reads the [Authorize] attribute on each request and changes visibility of individual items accordingly.
The menu and breadcrumb HTML helpers it uses are templated, so you can change them to output any HTML you need.
Full Disclosure: I made a major contribution to this project.

Developing web applications with widgets (or re-usable components) in ASP.NET MVC

I have experience in writing code... but new to developing web applications.
I am in process of choosing a framework for my project. Based on my initial research almost everything is very VIEW centric where after all business logic has been executed a model is populated with data and passed on to the VIEW.
So the lowest level of granularity is the VIEW.
But I wonder what is the right technology to use if I wanted to develop re-usable widgets or controls. and then reuse them across multiple VIEWS.
i would prefer if the controls are in JavaScript and then they can easily be reused across pages. (so no asp.net server controls or web forms).
So if I were to select ASP.NET MVC 4 ... does it have anything to help me write code in reusable widgets... or will it simply ask me to write the VIEWS which work with the data provided by the model?
Sorry if this is newbie question
to eloborate on what I mean by a widget.
Suppose I am writing a discussion forum web appliction. I want to write a widget called post which has 3 views. View 1 is edit mode. View 2 is summary mode. view 3 is Detail mode.
I throw in this widget in a page called QuestionStream and in this page the widget appears in summary view. I perform data binding so that i get a list of questions.
I throw in this widget in a page called ThreadView and in this page the widget appear in detailed view. I perform data binding and I get all the details of the question.
I throw in this widget in a page called NewQuestion and in this page the widget appears in edit view.
So its a self contained control... but is reused in multiple places in different modes (so to speak).
Here is summary list on how to create reusable components/pages/widgets in MVC. There are many more ways, these are just examples to give you starting point on what to look for or how to make one.
1. Partial Views
Typically used for breaking down large views, into smaller views then combined at runtime
View-centric approach, the controller can choose a View to generate.
Tutorial
2. Render Action / Render Partial
Invokes an child ActionMethod directly in the view page as inline
expression.
Useful for partial views that requires a business logic.
3. Custom DisplayFor/EditorFor Templates
Model-centric, which means they take model metadata (Attributes/Annotations).
Cool tutorial
4. Custom HTML Helpers
"Commonly used to generate boilerplate HTML" (LINK)
The following link shows how to create page "widgets" in MVC. The author doesn't mention "widgets", but it is the same as you are looking for.
http://www.mikesdotnetting.com/Article/105/ASP.NET-MVC-Partial-Views-and-Strongly-Typed-Custom-ViewModels
Based on the tutorial mentioned, below is the image on what it should look like (notice the "Most Popular" section, that widget is completely reusable across all pages)
ASP.NET MVC 4 has the ability to write methods that are re-usable. The View is expressed in mark-up (typically) and will call methods from the Controller to populate the View.
To be honest though, even though you're asking a very simple question, I think what you're trying to get at is the re-usability. All the technologies you listed: server controls, web forms and mvc4 have that capability. It all depends what you want to do.

Umbraco MVC Implementation - View does not depend on Umbraco implementations

I have followed this nice tutorial which explains how you can take even more advantage of using Umbraco as a content delivery system. Tutorial MVC Umbraco => Your model and views should not depend on specific Umbraco implementations which is a huge advantage for real front-end developers.
The controller inherited from Umbraco.Web.Mvc.RenderMvcController in order to access the data from the CMS.
Now my problem is that we can't use #HTML.actionlink for navigation, it seems that this is only supported in SurfaceController.
Now my question is how would we implement navigation inside Umbraco.Web.Mvc.RenderMvcController? Can we still use the native #HTML.actionlink tag?
No you can't. Simply because all requests pass through a single action. In order to retrieve a path to a CMS-managed page, you need to use the node/content traversal the #Model provides. See here for more details on this.
Edit
To clarify, the author of the article is suggesting that the Umbraco implementation should be more in line with a traditional MVC implementation with little or no logic in the views. Therefore, any querying of node data should happen prior to the view (e.g. in the Mappers). So this is where you would have to retrieve the links.
Umbraco's default MVC implementation forces all requests to go via a single action on a single controller. The author's implementation allows the requests to be shared across one controller per document type - which is better IMO. But it still means that things like Html.ActionLink are redundant in the views since there isn't an action per page.
Further edit
If you wanted to build a navigation list with a combination of Umbraco-managed pages and non-Umbraco pages, regardless of the implementation, I would:
Create a child action and view for the navigation in a separate NavigationController that inherits from the SurfaceController
Use the this.CurrentPage property of the inherited SurfaceController to traverse the Umbraco content cache to retrieve the relevant Umbraco-managed pages. You can then use the Url property of each page result to get its path, and the Name property to get the page title
Use this.Url.Action("action", "controller") to retrieve the paths to specific non-Umbraco actions. Alternatively, if the pages are database-managed, use you data layer (e.g. EF, NHibernate, PetaPoco) at this point
Combine these in a Dictionary to make the list you require where the Key is the path and the Value is the page title
Pass this down to the view as the view model.
Of course there any many more things to consider like caching, but in a nutshell, that's a fairly basic implementation.

Nested layouts in Orchard pages

I'm trying to migrate an existing ASP.NET MVC 3 website into Orchard, so I can continue to build the site while taking advantage of the extended features offered by Orchard. I'm attempting to wrap the entire existing site into an Orchard Module, so I can keep all the existing functionality intact, and continue adding features, or porting old features into the new paradigm as needed.
The main issue I'm having with a straight copy into Orchard is that I've taken advantage of the Layout hierarchy in MVC 3 to specify the same parent layout for related pages, which in turn may have its own parent layout, and so on.
When I copied in my existing views, I get the error that Layout is a read-only property on the custom Orchard WebPageView, but when I change the base view type back to the standard MVC 3 base type, the paths do not match up to my views because of the Module folder structure.
Is the ability to specify nested layouts simply not supported for sites running in Orchard, or is there a built-in work-around that I'm missing?
Thanks!
-J
Orchard's layout system predates MVC's and works a little differently. I'm afraid you will have to refactor things a bit. The views in Orchard rely on the idea of a shape, which is a dynamic object analogous to a view model. When the time comes to render a shape, the system looks for the most appropriate template to transform it into html. Most controller actions return a shape result that will be rendered as a child of the content zone that must be defined in the layout shape. The layout shape rendering can be changed by specifying alternates for it (see http://weblogs.asp.net/bleroy/archive/2010/12/14/switching-the-layout-in-orchard-cms.aspx).
So in the end Orchard doesn't use the sort of inverted master-page-like layout system that Razor/ASP.NET Web Pages/MVC3 promotes. Instead you use shapes and their alternates to get similar results, but the viewpoint is different as shapes get added into nested containers rather than have contained objects specify what they get inserted into.

What is an ASP.Net MVC View Engine?

There are quite a lot of questions on SO regarding View Engines in ASP.Net MVC, and about using "custom" ones instead of the "default" one. For me as a hobby programmer, the term "View Engine" is new, and I have not been able to understand what it means. So, my questions are:
What is a View Engine?
What does the View Engine do, and which role in the MVC pattern does it play? (Closely related to 1...)
What are the main characteristics/properties of the default View Engine that programmers want to change/avoid by switching to a different View Engine?
What are the main benefits of common other View Engines out there that developers are after when they choose to use a different one than standard? (I've seen the name "Spark View Engine" a bunch of times, and I bet there are others too).
When (in what scenarios) would I want to develop my own View Engine?
There, I think that is all I want to ask (for now). Give me View Engines 101! =)
The "view engine" handles the rendering of the view to html, xml or whatever content type it is created to emit. Within "MVC", it would be an aspect of the View (V).
Different view engines have different syntaxes, etc. to manage rendering. The decision to use another view engine is most likely very project/programmer specific. In some cases they may see an actual or perceived limitation of the default view engine; in other cases it may simply be a different design goal or focus.
As far as Spark goes, their focus is to be much more terse than the default view engine and to remain in HTML-like syntax as much as possible instead of dropping into ASP.NET script blocks.
As an end user, the only time you would want to create your own view engine is probably never. ;) It's not a task to be taken lightly, and you'll probably end up re-implementing functionality that already exists in an existing view engine.
Edit
OK. So are the View.aspx files part of
the View Engine, or is the View Engine
a set of classes that help choose
which View.aspx (or other type of
response) that should be rendered? How
does it work?
The MVC pattern tells you that your model, view and controller will be separate "things". In ASP.NET MVC, the default view engine uses the existing ASP.NET framework, which includes master pages, ASPX files, etc. Spark does something similar, but it's a different engine so it doesn't work exactly the same. So in a general sense the view files are not engine-specific, but the specific files, their layout on disk and their contents are view-engine specific.
In addition to wanting to completely replace the view engine (e.g. Spark), you also might make your own view engine just to make a small change to the behavior of the default view engine. You could do this by creating a new view engine that inherits from the default engine and overrides some behavior.
One common example is tweaking where the view engine looks on the hard drive for the .aspx files. For example, you might want to create a view engine that automatically switches which view.aspx file is used based on whether the user is on a mobile device.
See Mobile Web Sites with ASP.NET MVC and the Mobile Browser Definition File for a really good example implementation of this.
Update:
Scott just posted an updated ViewEngine implementation that has important improvements vs the initial implementation linked above.

Resources