+ All Categories
Home > Presentations & Public Speaking > Sitecore MVC (London User Group, April 29th 2014)

Sitecore MVC (London User Group, April 29th 2014)

Date post: 12-Nov-2014
Category:
Upload: ruud-van-falier
View: 137 times
Download: 3 times
Share this document with a friend
Description:
The slides from my presentation about Sitecore MVC during the Sitecore Technical User Group on the 19th of April in London, United Kingdom.
Popular Tags:
29
Sitecore MVC How to implement Sitecore using MVC Presented by Ruud van Falier
Transcript
Page 1: Sitecore MVC (London User Group, April 29th 2014)

Sitecore MVC

How to implement Sitecore using MVC Presented by Ruud van Falier

Page 2: Sitecore MVC (London User Group, April 29th 2014)

Topics

The basic concept of (Sitecore) MVC

Sitecore renderings related to MVC

Views

Models

Controllers

Inversion of Control

Dependency Injection

MVC vs. WebForms

Need input: named parameters / URL routing

Page 3: Sitecore MVC (London User Group, April 29th 2014)

The basic concept of MVCHow we used to roll…

Page 4: Sitecore MVC (London User Group, April 29th 2014)
Page 5: Sitecore MVC (London User Group, April 29th 2014)

The basic concept of MVC

View

Model

Controller

UserUses

ManipulatesUpdates

Sees

Page 6: Sitecore MVC (London User Group, April 29th 2014)

The basic concept of MVC

Views display a certain set of data.They do not know where the data comes from.

Models are classes that hold data.They may also execute logic for managing this data.They do not know how the data is displayed.

Controllers are classes that execute logic that controls what data is seen and which view is used to display it.

Page 7: Sitecore MVC (London User Group, April 29th 2014)

The basic concept of MVC

Browser URL Routing Controller Model View

RequestInvoke action

Initialize

Lookup view

Render

HTML

An ASP.NET MVC request

Page 8: Sitecore MVC (London User Group, April 29th 2014)

The basic concept of MVCA Sitecore MVC request

Source: Martina Welander

Request

httpBeginRequest pipeline

MVC route

?

Layout specified?

Is it an MVC view file?

Controller specified?

MVC request

WebFormsrequest

No No No

No

Yes Yes Yes

Yes

Page 9: Sitecore MVC (London User Group, April 29th 2014)

VIEWS

Page 10: Sitecore MVC (London User Group, April 29th 2014)
Page 11: Sitecore MVC (London User Group, April 29th 2014)

Source: Phil Haack

Page 12: Sitecore MVC (London User Group, April 29th 2014)

Views

Do

Display data from a model

Use simple flow logic that is required to present data (if / foreach / retrieval methods)

Don’t

Add complex logic

Go nuts with inline code

Page 13: Sitecore MVC (London User Group, April 29th 2014)

Sitecore renderings related to MVC

View RenderingRenders a View using a built-in controller action.The controller passes a model of type RenderingModel to the View.

Controller RenderingCalls an action on a controller and lets the controller handle the View rendering.

Page 14: Sitecore MVC (London User Group, April 29th 2014)

Can you demo that?!

Page 15: Sitecore MVC (London User Group, April 29th 2014)

MODELS

Page 16: Sitecore MVC (London User Group, April 29th 2014)

Models

public class ContentPageModel{ public string Title { get; set; }

public string Intro { get; set; }

public string Body { get; set; }}

public class ContentPageModel{ public string Title { get; set; }

public string Intro { get; set; }

public string Body { get; set; }

public ContentPageModel Parent { get; set; }

public IEnumerable<ContentPageModel> SubPages { get; set; }}

public class ContentPageModel{ /* Snipped properties */

public void CreateSubPage(ContentPageModel model) { // Logic for creating a sub page. }

public void DeleteSubPage(ContentPageModel model) { // Logic for deleting a sub page. }}

Page 17: Sitecore MVC (London User Group, April 29th 2014)

Models

Do

Hold data

Provide logic to manipulate data

Don’t

Add presentation elements to data

Use for application logic

Page 18: Sitecore MVC (London User Group, April 29th 2014)

CONTROLLERS

Page 19: Sitecore MVC (London User Group, April 29th 2014)

Controllers

PageController

About

Portfolio

News

Request/page/news

var model = repository.GetNews();

return View(model);

/Views/News.cshtml

Page 20: Sitecore MVC (London User Group, April 29th 2014)

Controllers

Do

Retrieve data required to initialize models

Initialize models

Return appropriate View (or other ActionResult)

Don’t

Cramp them with logic

Use them if it’s not necessary

Page 21: Sitecore MVC (London User Group, April 29th 2014)

Inversion of Control

“A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the reusable code that calls into the custom, or problem-specific, code.”, Wikipedia

The decoupling of dependencies by isolating code for certain responsibilities into separate libraries and referring to those libraries using their interfaces instead of their concrete implementation.

Page 22: Sitecore MVC (London User Group, April 29th 2014)

Inversion of Control

Inversion of control serves the following design purposes:

To decouple the execution of a task from implementation.

To focus a module on the task it is designed for.

To free modules from assumptions about how other systems do what they do and instead rely on contracts.

To prevent side effects when replacing a module.

Page 23: Sitecore MVC (London User Group, April 29th 2014)

public class MvcDemoController : Controller{ public ViewResult NewsOverview() { // Get news root item from Sitecore. Item newsRoot = Sitecore.Context.Database.GetItem("{NEWS-ROOT-GUID}"); IEnumerable<Item> newsItems = newsRoot.Children;

// Get temperature from weather service. var weatherService = new WeatherService(); int temperature = weatherService.GetTemperature();

// Initialize model for News Overview page. return this.View(new NewsOverviewModel { NewsItems = newsItems, Temperature = temperature }); }}

TIGHT COUPLING

Page 24: Sitecore MVC (London User Group, April 29th 2014)

public class MvcDemoController : Controller{ private readonly ISitecoreContext sitecoreContext; private readonly IWeatherService weatherService;

public MvcDemoController(ISitecoreContext sitecoreContext, IWeatherService weatherService) { this.sitecoreContext = sitecoreContext; this.weatherService = weatherService; }

public ViewResult NewsOverview() { // Get news root item from Sitecore. Item newsRoot = this.sitecoreContext.ItemManager.GetItem("{NEWS-ROOT-GUID}"); IEnumerable<Item> newsItems = newsRoot.Children;

// Get temperature from weather service. int temperature = this.weatherService.GetTemperature();

// Initialize model for News Overview page. return this.View(new NewsOverviewModel { NewsItems = newsItems, Temperature = temperature }); }}

LOOSE COUPLING

Page 25: Sitecore MVC (London User Group, April 29th 2014)

Dependecy Injection

There are several methods for Dependency Injection, some examples are:

Constructor injection (as seen in the example)

Parameter injection

Setter injection

.. and more

Use a framework that handles Dependency Injection for you

• Windsor container (because it ships with Glass)• Ninject• Autofac (TODO: Check)

Page 26: Sitecore MVC (London User Group, April 29th 2014)

public class MvcDemoController : Controller{ private readonly ISitecoreContext sitecoreContext; private readonly IWeatherService weatherService;

public MvcDemoController() : this(new SitecoreContext(), new WeatherService()) { }

public MvcDemoController(ISitecoreContext sitecoreContext, IWeatherService weatherService) { this.sitecoreContext = sitecoreContext; this.weatherService = weatherService; }}

Page 27: Sitecore MVC (London User Group, April 29th 2014)

OH MY GOD, THAT’S SO NOT FAIR!

MVC vs. WebForms ?!

Page 28: Sitecore MVC (London User Group, April 29th 2014)

MVC vs. WebForms

Why MVC is better

• Simpler page lifecycle.• No more server controls.• Multiple forms on a page.• No more ViewState.• Very easy to work with AJAX.• Not bound to generated markup.

WebForms is just an extremely complex abstraction over HTML/JS, made up before the birth of jQuery and AJAX; we don’t need this abstraction anymore.

Let me know if you need more reasons

When to stick to WebForms

• Legacy application• Team knowledge• Prototyping

Page 29: Sitecore MVC (London User Group, April 29th 2014)

References

Follow me on Twitter: @BrruuD

Contact me by e-mail: [email protected]

Read our blog: www.partechit.nl/blog

This presentation will become available online after the Sitecore User Group Conference on the 23rd of May in Utrecht, The Netherlands.

More info and tickets: www.sugnl.net


Recommended