SlideShare a Scribd company logo
Sitecore MVC
How to implement Sitecore using MVC Presented by Ruud van Falier
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
The basic concept of MVC
How we used to roll…
The basic concept of MVC
View
Model
Controller
User
Uses
Manipulates
Updates
Sees
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.
The basic concept of MVC
Browser URL Routing Controller Model View
Request
Invoke action
Initialize
Lookup view
Render
HTML
An ASP.NET MVC request
The basic concept of MVC
A Sitecore MVC request
Source: Martina Welander
Request
httpBeginRequest
pipeline
MVC
route?
Layout
specified?
Is it an MVC
view file?
Controller
specified?
MVC
request
WebForms
request
No No No
No
Yes Yes Yes
Yes
VIEWS
Source: Phil Haack
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
Sitecore renderings related to MVC
 View Rendering
Renders a View using a built-in controller action.
The controller passes a model of type RenderingModel to the View.
 Controller Rendering
Calls an action on a controller and lets the controller handle the View rendering.
Can you demo that?!
MODELS
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.
}
}
Models
Do
 Hold data
 Provide logic to manipulate
data
Don’t
 Add presentation elements to
data
 Use for application logic
CONTROLLERS
Controllers
PageController
About
Portfolio
News
Request
/page/news
var model = repository.GetNews();
return View(model);
/Views/News.cshtml
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
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.
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.
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
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
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)
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;
}
}
OH MY GOD, THAT’S SO NOT FAIR!
MVC vs. WebForms ?!
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
References
 Follow me on Twitter: @BrruuD
 Contact me by e-mail: ruud@partechit.nl
 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

More Related Content

What's hot

Asp.net mvc 5 course module 1 overview
Asp.net mvc 5 course   module 1 overviewAsp.net mvc 5 course   module 1 overview
Asp.net mvc 5 course module 1 overview
Sergey Seletsky
 
Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013
Thomas Robbins
 
ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015
Hossein Zahed
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentation
Bhavin Shah
 
Using MVC with Kentico 8
Using MVC with Kentico 8Using MVC with Kentico 8
Using MVC with Kentico 8
Thomas Robbins
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
Surbhi Panhalkar
 
MSDN - ASP.NET MVC
MSDN - ASP.NET MVCMSDN - ASP.NET MVC
MSDN - ASP.NET MVC
Maarten Balliauw
 
Kentico and MVC
Kentico and MVCKentico and MVC
Kentico and MVC
Cheryl MacDonald
 
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
Aaron Jacobson
 
ASP .NET MVC
ASP .NET MVC ASP .NET MVC
ASP .NET MVC
eldorina
 
ASP .net MVC
ASP .net MVCASP .net MVC
ASP .net MVC
Divya Sharma
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
Ashton Feller
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
Er. Kamal Bhusal
 
Flux architecture
Flux architectureFlux architecture
Flux architecture
Boyan Mihaylov
 
iOS viper presentation
iOS viper presentationiOS viper presentation
iOS viper presentation
Rajat Datta
 
Asp.net mvc basic introduction
Asp.net mvc basic introductionAsp.net mvc basic introduction
Asp.net mvc basic introduction
Bhagath Gopinath
 
Asp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin Sawant
Nitin Sawant
 
Using the Kentico CMS API
Using the Kentico CMS APIUsing the Kentico CMS API
Using the Kentico CMS API
Thomas Robbins
 
MVC 6 Introduction
MVC 6 IntroductionMVC 6 Introduction
MVC 6 Introduction
Sudhakar Sharma
 
ASP .Net MVC 5
ASP .Net MVC 5ASP .Net MVC 5
ASP .Net MVC 5
Nilachal sethi
 

What's hot (20)

Asp.net mvc 5 course module 1 overview
Asp.net mvc 5 course   module 1 overviewAsp.net mvc 5 course   module 1 overview
Asp.net mvc 5 course module 1 overview
 
Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013
 
ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentation
 
Using MVC with Kentico 8
Using MVC with Kentico 8Using MVC with Kentico 8
Using MVC with Kentico 8
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
MSDN - ASP.NET MVC
MSDN - ASP.NET MVCMSDN - ASP.NET MVC
MSDN - ASP.NET MVC
 
Kentico and MVC
Kentico and MVCKentico and MVC
Kentico and MVC
 
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
 
ASP .NET MVC
ASP .NET MVC ASP .NET MVC
ASP .NET MVC
 
ASP .net MVC
ASP .net MVCASP .net MVC
ASP .net MVC
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
Flux architecture
Flux architectureFlux architecture
Flux architecture
 
iOS viper presentation
iOS viper presentationiOS viper presentation
iOS viper presentation
 
Asp.net mvc basic introduction
Asp.net mvc basic introductionAsp.net mvc basic introduction
Asp.net mvc basic introduction
 
Asp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin Sawant
 
Using the Kentico CMS API
Using the Kentico CMS APIUsing the Kentico CMS API
Using the Kentico CMS API
 
MVC 6 Introduction
MVC 6 IntroductionMVC 6 Introduction
MVC 6 Introduction
 
ASP .Net MVC 5
ASP .Net MVC 5ASP .Net MVC 5
ASP .Net MVC 5
 

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

Using a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsUsing a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS apps
allanh0526
 
ASP.NET MVC Controllers & Actions
ASP.NET MVC Controllers & ActionsASP.NET MVC Controllers & Actions
ASP.NET MVC Controllers & Actions
onsela
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
nagarajupatangay
 
Building Modern Websites with ASP.NET by Rachel Appel
Building Modern Websites with ASP.NET by Rachel AppelBuilding Modern Websites with ASP.NET by Rachel Appel
Building Modern Websites with ASP.NET by Rachel Appel
.NET Conf UY
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
jinaldesailive
 
MVC Training Part 1
MVC Training Part 1MVC Training Part 1
MVC Training Part 1
Lee Englestone
 
CodeIgniter 101 Tutorial
CodeIgniter 101 TutorialCodeIgniter 101 Tutorial
CodeIgniter 101 Tutorial
Konstantinos Magarisiotis
 
Spring Web MVC
Spring Web MVCSpring Web MVC
Spring Web MVC
zeeshanhanif
 
Simple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnanSimple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnan
Gigin Krishnan
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
narendrakumar406336
 
LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
Akhil Mittal
 
Developing ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller PatternDeveloping ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller Pattern
goodfriday
 
Mvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaMvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senja
alifha12
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
Ilio Catallo
 
Jinal desai .net
Jinal desai .netJinal desai .net
Jinal desai .net
rohitkumar1987in
 
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe
 
Controllers & actions
Controllers & actionsControllers & actions
Controllers & actions
Eyal Vardi
 
WPF and Prism 4.1 Workshop at BASTA Austria
WPF and Prism 4.1 Workshop at BASTA AustriaWPF and Prism 4.1 Workshop at BASTA Austria
WPF and Prism 4.1 Workshop at BASTA Austria
Rainer Stropek
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
Hùng Nguyễn Huy
 
Tightly coupled view (model bounded view)
Tightly coupled view (model bounded view)Tightly coupled view (model bounded view)
Tightly coupled view (model bounded view)
IT PROGRAMMING WORLD
 

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

Using a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsUsing a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS apps
 
ASP.NET MVC Controllers & Actions
ASP.NET MVC Controllers & ActionsASP.NET MVC Controllers & Actions
ASP.NET MVC Controllers & Actions
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Building Modern Websites with ASP.NET by Rachel Appel
Building Modern Websites with ASP.NET by Rachel AppelBuilding Modern Websites with ASP.NET by Rachel Appel
Building Modern Websites with ASP.NET by Rachel Appel
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
 
MVC Training Part 1
MVC Training Part 1MVC Training Part 1
MVC Training Part 1
 
CodeIgniter 101 Tutorial
CodeIgniter 101 TutorialCodeIgniter 101 Tutorial
CodeIgniter 101 Tutorial
 
Spring Web MVC
Spring Web MVCSpring Web MVC
Spring Web MVC
 
Simple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnanSimple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnan
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
 
Developing ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller PatternDeveloping ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller Pattern
 
Mvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaMvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senja
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
 
Jinal desai .net
Jinal desai .netJinal desai .net
Jinal desai .net
 
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
 
Controllers & actions
Controllers & actionsControllers & actions
Controllers & actions
 
WPF and Prism 4.1 Workshop at BASTA Austria
WPF and Prism 4.1 Workshop at BASTA AustriaWPF and Prism 4.1 Workshop at BASTA Austria
WPF and Prism 4.1 Workshop at BASTA Austria
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
Tightly coupled view (model bounded view)
Tightly coupled view (model bounded view)Tightly coupled view (model bounded view)
Tightly coupled view (model bounded view)
 

More from Ruud van Falier

Sitecore Experience Accelerator (SxA)
Sitecore Experience Accelerator (SxA)Sitecore Experience Accelerator (SxA)
Sitecore Experience Accelerator (SxA)
Ruud van Falier
 
The Art of Sitecore Upgrades
The Art of Sitecore UpgradesThe Art of Sitecore Upgrades
The Art of Sitecore Upgrades
Ruud van Falier
 
Introducing Sitecore Habitat - SUGCON EU 2016
Introducing Sitecore Habitat - SUGCON EU 2016Introducing Sitecore Habitat - SUGCON EU 2016
Introducing Sitecore Habitat - SUGCON EU 2016
Ruud van Falier
 
Sitecore Habitat (User Group NL, February 11th 2016)
Sitecore Habitat (User Group NL, February 11th 2016)Sitecore Habitat (User Group NL, February 11th 2016)
Sitecore Habitat (User Group NL, February 11th 2016)
Ruud van Falier
 
Managing your user data with Sitecore xDB
Managing your user data with Sitecore xDBManaging your user data with Sitecore xDB
Managing your user data with Sitecore xDB
Ruud van Falier
 
Sitecore - Onder de motorkop van ParTechIT.nl
Sitecore - Onder de motorkop van ParTechIT.nlSitecore - Onder de motorkop van ParTechIT.nl
Sitecore - Onder de motorkop van ParTechIT.nl
Ruud van Falier
 

More from Ruud van Falier (6)

Sitecore Experience Accelerator (SxA)
Sitecore Experience Accelerator (SxA)Sitecore Experience Accelerator (SxA)
Sitecore Experience Accelerator (SxA)
 
The Art of Sitecore Upgrades
The Art of Sitecore UpgradesThe Art of Sitecore Upgrades
The Art of Sitecore Upgrades
 
Introducing Sitecore Habitat - SUGCON EU 2016
Introducing Sitecore Habitat - SUGCON EU 2016Introducing Sitecore Habitat - SUGCON EU 2016
Introducing Sitecore Habitat - SUGCON EU 2016
 
Sitecore Habitat (User Group NL, February 11th 2016)
Sitecore Habitat (User Group NL, February 11th 2016)Sitecore Habitat (User Group NL, February 11th 2016)
Sitecore Habitat (User Group NL, February 11th 2016)
 
Managing your user data with Sitecore xDB
Managing your user data with Sitecore xDBManaging your user data with Sitecore xDB
Managing your user data with Sitecore xDB
 
Sitecore - Onder de motorkop van ParTechIT.nl
Sitecore - Onder de motorkop van ParTechIT.nlSitecore - Onder de motorkop van ParTechIT.nl
Sitecore - Onder de motorkop van ParTechIT.nl
 

Recently uploaded

Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Dutch Power
 
2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf
Frederic Leger
 
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
OECD Directorate for Financial and Enterprise Affairs
 
ASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdfASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdf
ToshihiroIto4
 
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussionArtificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
OECD Directorate for Financial and Enterprise Affairs
 
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
Suzanne Lagerweij
 
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
OECD Directorate for Financial and Enterprise Affairs
 
Competition and Regulation in Professions and Occupations – ROBSON – June 202...
Competition and Regulation in Professions and Occupations – ROBSON – June 202...Competition and Regulation in Professions and Occupations – ROBSON – June 202...
Competition and Regulation in Professions and Occupations – ROBSON – June 202...
OECD Directorate for Financial and Enterprise Affairs
 
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Dutch Power
 
Updated diagnosis. Cause and treatment of hypothyroidism
Updated diagnosis. Cause and treatment of hypothyroidismUpdated diagnosis. Cause and treatment of hypothyroidism
Updated diagnosis. Cause and treatment of hypothyroidism
Faculty of Medicine And Health Sciences
 
XP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to LeadershipXP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to Leadership
samililja
 
Carrer goals.pptx and their importance in real life
Carrer goals.pptx  and their importance in real lifeCarrer goals.pptx  and their importance in real life
Carrer goals.pptx and their importance in real life
artemacademy2
 
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
SkillCertProExams
 
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussionArtificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
OECD Directorate for Financial and Enterprise Affairs
 
Burning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdfBurning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdf
kkirkland2
 
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie WellsCollapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Rosie Wells
 
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
gpww3sf4
 
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPointMẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
1990 Media
 
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
 
Gregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics PresentationGregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics Presentation
gharris9
 

Recently uploaded (20)

Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
 
2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf
 
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
 
ASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdfASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdf
 
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussionArtificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
 
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
 
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
 
Competition and Regulation in Professions and Occupations – ROBSON – June 202...
Competition and Regulation in Professions and Occupations – ROBSON – June 202...Competition and Regulation in Professions and Occupations – ROBSON – June 202...
Competition and Regulation in Professions and Occupations – ROBSON – June 202...
 
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
 
Updated diagnosis. Cause and treatment of hypothyroidism
Updated diagnosis. Cause and treatment of hypothyroidismUpdated diagnosis. Cause and treatment of hypothyroidism
Updated diagnosis. Cause and treatment of hypothyroidism
 
XP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to LeadershipXP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to Leadership
 
Carrer goals.pptx and their importance in real life
Carrer goals.pptx  and their importance in real lifeCarrer goals.pptx  and their importance in real life
Carrer goals.pptx and their importance in real life
 
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
 
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussionArtificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
 
Burning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdfBurning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdf
 
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie WellsCollapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
 
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
 
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPointMẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
 
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
 
Gregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics PresentationGregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics Presentation
 

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

  • 1. Sitecore MVC How to implement Sitecore using MVC Presented by Ruud van Falier
  • 2. 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
  • 3. The basic concept of MVC How we used to roll…
  • 4.
  • 5. The basic concept of MVC View Model Controller User Uses Manipulates Updates Sees
  • 6. 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.
  • 7. The basic concept of MVC Browser URL Routing Controller Model View Request Invoke action Initialize Lookup view Render HTML An ASP.NET MVC request
  • 8. The basic concept of MVC A Sitecore MVC request Source: Martina Welander Request httpBeginRequest pipeline MVC route? Layout specified? Is it an MVC view file? Controller specified? MVC request WebForms request No No No No Yes Yes Yes Yes
  • 10.
  • 12. 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
  • 13. Sitecore renderings related to MVC  View Rendering Renders a View using a built-in controller action. The controller passes a model of type RenderingModel to the View.  Controller Rendering Calls an action on a controller and lets the controller handle the View rendering.
  • 14. Can you demo that?!
  • 16. 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. } }
  • 17. Models Do  Hold data  Provide logic to manipulate data Don’t  Add presentation elements to data  Use for application logic
  • 19. Controllers PageController About Portfolio News Request /page/news var model = repository.GetNews(); return View(model); /Views/News.cshtml
  • 20. 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
  • 21. 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.
  • 22. 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.
  • 23. 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
  • 24. 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
  • 25. 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)
  • 26. 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; } }
  • 27. OH MY GOD, THAT’S SO NOT FAIR! MVC vs. WebForms ?!
  • 28. 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
  • 29. References  Follow me on Twitter: @BrruuD  Contact me by e-mail: ruud@partechit.nl  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