SlideShare ist ein Scribd-Unternehmen logo
1 von 29
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

Weitere ähnliche Inhalte

Was ist angesagt?

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 overviewSergey 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 2013Thomas 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 - VS2015Hossein Zahed
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentationBhavin Shah
 
Using MVC with Kentico 8
Using MVC with Kentico 8Using MVC with Kentico 8
Using MVC with Kentico 8Thomas Robbins
 
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 5Aaron Jacobson
 
ASP .NET MVC
ASP .NET MVC ASP .NET MVC
ASP .NET MVC eldorina
 
iOS viper presentation
iOS viper presentationiOS viper presentation
iOS viper presentationRajat Datta
 
Asp.net mvc basic introduction
Asp.net mvc basic introductionAsp.net mvc basic introduction
Asp.net mvc basic introductionBhagath 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 SawantNitin Sawant
 
Using the Kentico CMS API
Using the Kentico CMS APIUsing the Kentico CMS API
Using the Kentico CMS APIThomas Robbins
 

Was ist angesagt? (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
 

Ähnlich wie 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 appsallanh0526
 
ASP.NET MVC Controllers & Actions
ASP.NET MVC Controllers & ActionsASP.NET MVC Controllers & Actions
ASP.NET MVC Controllers & Actionsonsela
 
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 desaijinaldesailive
 
Simple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnanSimple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnanGigin Krishnan
 
LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLAkhil 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 Patterngoodfriday
 
Mvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaMvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaalifha12
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3Ilio Catallo
 
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 & actionsEyal 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 AustriaRainer Stropek
 
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
 

Ähnlich wie 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)
 

Mehr von 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 UpgradesRuud van Falier
 
Introducing Sitecore Habitat - SUGCON EU 2016
Introducing Sitecore Habitat - SUGCON EU 2016Introducing Sitecore Habitat - SUGCON EU 2016
Introducing Sitecore Habitat - SUGCON EU 2016Ruud 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 xDBRuud 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.nlRuud van Falier
 

Mehr von 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
 

Kürzlich hochgeladen

Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Delhi Call girls
 
Microsoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AIMicrosoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AITatiana Gurgel
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxraffaeleoman
 
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Hasting Chen
 
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptxMohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptxmohammadalnahdi22
 
Air breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animalsAir breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animalsaqsarehman5055
 
Presentation on Engagement in Book Clubs
Presentation on Engagement in Book ClubsPresentation on Engagement in Book Clubs
Presentation on Engagement in Book Clubssamaasim06
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar TrainingKylaCullinane
 
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night EnjoyCall Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night EnjoyPooja Nehwal
 
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort ServiceDelhi Call girls
 
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, YardstickSaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, Yardsticksaastr
 
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxNikitaBankoti2
 
Mathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMoumonDas2
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaKayode Fayemi
 
George Lever - eCommerce Day Chile 2024
George Lever -  eCommerce Day Chile 2024George Lever -  eCommerce Day Chile 2024
George Lever - eCommerce Day Chile 2024eCommerce Institute
 
Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Chameera Dedduwage
 
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Kayode Fayemi
 
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024eCommerce Institute
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...Sheetaleventcompany
 
Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Vipesco
 

Kürzlich hochgeladen (20)

Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
 
Microsoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AIMicrosoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AI
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
 
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
 
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptxMohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptx
 
Air breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animalsAir breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animals
 
Presentation on Engagement in Book Clubs
Presentation on Engagement in Book ClubsPresentation on Engagement in Book Clubs
Presentation on Engagement in Book Clubs
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar Training
 
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night EnjoyCall Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
 
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
 
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, YardstickSaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
 
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
 
Mathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptx
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
 
George Lever - eCommerce Day Chile 2024
George Lever -  eCommerce Day Chile 2024George Lever -  eCommerce Day Chile 2024
George Lever - eCommerce Day Chile 2024
 
Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)
 
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
 
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
 
Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510
 

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