16
Sitecore MVC

Sitecore mvc

Embed Size (px)

Citation preview

Page 1: Sitecore mvc

Sitecore MVC

Page 2: Sitecore mvc

What we will see today? Are you excited?

• What is Sitecore MVC

• How does it differ from regular .NET MVC?

• Using Web forms and MVC within one solution Sitecore

• Should we use Sitecore MVC?

• Installing and Configuring Sitecore MVC

• Renderings

• @Html.Sitecore() Helper and creating custom Sitecore helper

• Using your own model instead of RenderingModel

Page 3: Sitecore mvc

What is Sitecore MVC?• A View, It is the component that displays the application's user interface (UI) (all of the things the user can see and respond to on the screen, such as buttons, display boxes, and so forth).

• A Model, which represents the underlying, logical structure of data in a software application and the high-level class associated with it at data domain. This object model does not contain any information about the user interface and the implementation.

• A Controller, which represents the underlying, logical structure of data in a software application and the high-level class associated with it. This object model does not contain any information about the user interface.

Page 4: Sitecore mvc

A traditional ASP.NET MVC request

Page 5: Sitecore mvc

Using Web forms and MVC within one solution Sitecore• Yes. We can use both the approach, a traditional web form and even a MVC in a single solution of Sitecore project.

• I know you guys will have question,

How it will differentiate a traditional Web form request and MVC request in Sitecore?

Page 6: Sitecore mvc

Sitecore MVC request

Page 7: Sitecore mvc

Should we use Sitecore MVC?• Yes

• No

• Both are true answers.

We should ask question instead,

When we should use Sitecore MVC?

Page 8: Sitecore mvc

MVC Web Forms

• Shorter page lifecycle, better performance • Extra built-in security

• Steep learning curve for web forms developers and junior developers • Faster to build, less classes

• Easy to add DI and more flexible unit testing • Easier to understand for less seasoned .NET developers

* Forces a convention and cleaner coding • Larger knowledge base/ documentation and more tutorials available

• RAZOR view engine is very convenient for front-end integration. • More developers out there with experience in coding with web forms & Sitecore

• Will be supported by Sitecore going forward (we have confirmed this from the very top) • Will be supported by Sitecore going forward

• Helps pull new talent, newer framework (with Sitecore) • Will not be made redundant by MVC any time soon

• We would advice that you have a fairly senior .NET development team (That we have, without any doubt).

• More Sitecore partners are familiar with web forms, so more support available if short-staffed or something goes wrong

• There are some very large Sitecore clients implementing solutions with MVC, some of which are already live. • Tried and tested, most existing Sitecore solutions are built with web forms.

• Very stable, MVC has been around with .NET for a long time, there was not a lot of core code Sitecore needed to change to support MVC • Very stable

• Good de-coupling from HTML, no need to worry about automatic ID's generated by web-forms controls, resulting in cleaner HTML output.

• Fastest approach to get a clean application live for a company with predominantly web forms experience.

Page 9: Sitecore mvc

RenderingsCommon Renderings

• Method Rendering

• Url Rendering

• Item rendering

• Webcontrol

• Xsl Rendering

MVC specific renderings

• View renderings

• Controller renderings

Page 10: Sitecore mvc

View Rendering A view rendering consists of a view that takes a RenderingModel by default. The model is assembled by the Sitecore MVC pipeline, and you can create your own, custom models. Because the pipeline expects an Initialize() method on your model, you can either inherit from RenderingModel or implement IRenderingModel.

To post a view rendering, specify the controller and action that you want to post to on the component’s definition item.

Advantages

• They are simple to use – especially if you do not use a custom model. Most of the time, you only want to display some content from the context or data source item without requiring any business logic – a simple ‘page’ will often consist of a title and some text, which you can do very easily with Sitecore’s RenderingModel and the @Html.Sitecore() helper. There isn’t much to unit test here (you can certainly do web tests), and creating a controller action seems extraneous.

Page 11: Sitecore mvc

• When you post a form from a view rendering, it will only post the action you have specified – after which you can redirect to another page or trigger the Sitecore rendering pipeline. This is not the case with controller renderings, which make posting forms a bit more difficult.

Disadvantages

• If you need to do any business logic when retrieving your model, you will want to do that in a separate layer. However, because you do not have access to a controller, the only place you can do this logic is in the Initialize() method of a custom model – and putting business logic in the model itself is not ideal.

• It’s just not very ASP.NET MVC. Developers are used to having a controller and an action, and view renderings do not work like that. However, you must keep in mind that Sitecore MVC is an implementation of ASP.NET MVC that needs to support the same features as its Web Forms counterpart. The modular nature of a Sitecore page introduces challenges.

Page 12: Sitecore mvc

Controller Rendering Rather than specifying a view, the component definition item for a controller rendering specifies a controller name and an action name. When the rendering is added to a placeholder, this action is executed and the view is returned.

Advantages

• They look ASP.NET MVC. You can use dependency injection and common patterns (like the repository pattern) as you normally would, and create your own actions.

• Keeps your business logic separate from your model; you can create plain POCO classes that are hydrated by a business logic layer.

Page 13: Sitecore mvc

Disadvantages• Although they look ASP.NET MVC, they are still renderings and will not behave exactly as a controller

action might in a standard ASP.NET MVC application. This is particularly evident when you try to have more than one post on the page, or attempt to RedirectToAction.

• They are executed after the main view has already been rendered, which results in some unexpected behavior – again, this might cause some strange behavior when you try to post a form, and with TempData (if you use that).

• In some instances, a controller rendering is a lot of unnecessary work

Page 14: Sitecore mvc

@Html.Sitecore() Helper

Page 15: Sitecore mvc

References• http://www.hhogdev.com/blog/2012/august/sitecore-mvc-prototype-part1.aspx part - 1 • http://www.hhogdev.com/blog/2012/august/~/link.aspx?_id=F97F95290694409A99655C2F3D90021D&

_z=z part – 2

• http://www.hhogdev.com/blog/2012/august/~/~/link.aspx?_id=B8303C775D554C2A82A337FD4AD48BE1&_z=z part – 3

• http://mhwelander.net/2014/05/28/posting-forms-in-sitecore-mvc-part-1-view-renderings/• http://mhwelander.net/2014/05/30/posting-forms-in-sitecore-mvc-part-2-controller-renderings/ • http://mhwelander.net/2014/06/13/view-renderings-vs-controller-renderings/

• Video Tutorials:• https://www.youtube.com/watch?v=i3Mwcphtz4w part - 1 • https://www.youtube.com/watch?v=dW_rQp9bMmE part – 2

Page 16: Sitecore mvc

Thank YouCREATED BY PRATIK SATIKUNVAR