Sitecore MVC (User Group Conference, May 23rd 2014)

Ruud van Falier
Ruud van FalierSoftware Engineer
#sugcon
#sitecore
Sitecore MVC
Ruud van Falier
ParTech IT
Topics
• The basic concept
• Traditional ASP.NET MVC
• Sitecore MVC
• Sitecore renderings
• Controllers
• Models
• Model binding
• Views
• SitecoreHelper
• Inversion of Control
• Dependency Injection
• MVC vs. WebForms
2
How we used to roll…
The basic concept of (Sitecore) MVC
3
4
The basic concept of (Sitecore) MVC
5
View
Model
Controller
User
Uses
Manipulates
Updates
Sees
The basic concept of (Sitecore) 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.
6
The basic concept of (Sitecore) MVC
A traditional ASP.NET MVC request
7
Browser URL Routing Controller Model View
Request
Invoke action
Initialize
Lookup view
Render
HTML
The basic concept of (Sitecore) MVC
A Sitecore MVC request
8
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
Source: Martina Welander
Sitecore Renderings
•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.
9
Controllers
10
Controllers
11
PageController
About
Portfolio
News
Request to:
/page/news
var model = repository.GetNews();
return View(model);
/Views/News.cshtml
Controllers
12
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
});
}
}
Controllers
13
public class MvcDemoController : Controller
{
public ViewResult SearchNews(string keyword, int page)
{
// Perform search and return NewsOverViewModel with results.
var model = new NewsOverviewModel();
return this.View(model);
}
}
/MvcDemo/SearchNews?keyword=my_search_terms&page=1
Controllers
14
/MvcDemo/SearchNews?keyword=my_search_terms&page=1
public class MvcDemoController : Controller
{
public ViewResult SearchNews(SearchRequestModel request)
{
// Perform search and return NewsOverViewModel with results.
var model = new NewsOverviewModel();
return this.View(model);
}
}
Controllers
15
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
Models
16
Models
17
public class ContentPageModel
{
public string Title { get; set; }
public string Intro { get; set; }
public string Body { get; set; }
}
Models
18
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; }
}
Models
19
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.
}
}
Model Binding
20
public class SearchRequestModel
{
public string Keyword { get; set; }
public int Page { get; set; }
public bool OrderByRelevance { get; set; }
}
public class MvcDemoController : Controller
{
public ViewResult SearchNews(SearchRequestModel request)
{
// Perform search and return NewsOverViewModel with results.
var model = new NewsOverviewModel();
return this.View(model);
}
}
/MvcDemo/SearchNews?keyword=my_search_terms&page=1
Models
In Sitecore MVC:
• View Models usually represent Templates from Sitecore.
• This way, View Models can contain content from
Sitecore items.
• There are several Object-Relational Mappers that are
created specifically for that task.
Make sure to attend Mike Edwards’ and Robin
Hermanussen’s session for more on ORM!
21
Models
22
Do
• Hold data
• Provide logic to
manipulate data
Don’t
• Add presentation
elements to data
• Use for application
logic
Views
23
Razor
Syntax
24
Source: http://haacked.com
Views
25
@model RenderingModel
<section id="main_content">
<h3>
@Model.Item["Title"]
</h3>
<p>
<strong>
@Model.Item["Intro"]
</strong>
</p>
@Model.Item["Body"]
</section>
Views
26
@model RenderingModel
<section id="main_content">
<h3>
@Model.Item["Title"]
</h3>
<p>
<strong>
@Model.Item["Intro"]
</strong>
</p>
@Model.Item["Body"]
</section>
@model RenderingModel
<section id="main_content">
<h3>
@Model.Item["Title"]
</h3>
<p>
<strong>
@Html.Raw(Model.Item["Intro"])
</strong>
</p>
@Html.Raw(Model.Item["Body"])
</section>
Views
27
@model RenderingModel
<section id="main_content">
<h3>
@Html.Sitecore().Field("Title")
</h3>
<p>
<strong>
@Html.Sitecore().Field("Intro")
</strong>
</p>
@Html.Sitecore().Field("Body")
</section>
Views
28
using System.Web;
using Sitecore.Mvc.Helpers;
/// <summary>
/// Extension methods for the SitecoreHelper class.
/// </summary>
public static class SitecoreHelperExtensions
{
public static HtmlString WhoAreYou(this SitecoreHelper helper)
{
return new HtmlString("I'm your father!");
}
}
@Html.Sitecore().WhoAreYou()
@*
Output: "I'm your father"
*@
Views
29
public class ContentController : Controller
{
public ViewResult ContentPage()
{
var model = new ContentPageModel();
// Populate model properties with data from Sitecore.
return this.View("~/Views/Content page.cshtml", model);
}
}
public class ContentPageModel
{
public string Title { get; set; }
public string Intro { get; set; }
public string Body { get; set; }
}
Views
30
public class ContentController : Controller
{
public ViewResult ContentPage()
{
var model = new ContentPageModel();
// Populate model properties with data from Sitecore.
return this.View("~/Views/Content page.cshtml", model);
}
}
public class ContentPageModel
{
public string Title { get; set; }
public string Intro { get; set; }
public string Body { get; set; }
}
@model ContentPageModel
<section id="main_content">
<h3>
@Model.Title
</h3>
<p>
<strong>
@Model.Intro
</strong>
</p>
@Model.Body
</section>
Views
31
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
Inversion of Control (IoC)
32
“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 (IoC)
IoC 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 (interfaces).
• To prevent side effects when replacing a
module.
33
Inversion of Control (IoC)
34
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
});
}
}
Inversion of Control (IoC)
35
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
Inversion of Control (IoC)
36
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
});
}
}
Inversion of Control (IoC)
37
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
Dependency Injection (DI)
There are several methods for DI,
some examples are:
• Constructor injection (as seen in the example)
• Parameter injection
• Setter injection
Use a framework that handles DI for you:
• Windsor container (because it ships with Glass)
• Ninject
• Autofac
38
MVC vs. WebForms
39
Why MVC is better
• Strict separation of responsibilities
• Simpler page lifecycle.
• Stateless (No more ViewState).
• No more server controls.
• Very easy to work with AJAX.
• Not bound to generated markup.
• Multiple forms on a page.
When to stick to WebForms
• Legacy application
• Team knowledge
• Prototyping
WebForms is just an extremely complex abstraction
over HTML/JS, invented before the introduction of
jQuery and AJAX;
We don’t need this abstraction anymore.
Join us!
Are you an awesome Sitecore Developer?
ParTech is looking for people to expand their team!
We offer you an excellent salary and benefits and
the chance to work on enterprise Sitecore projects
together with the most skilled developers available.
Join the Sitecore elite,
apply at www.partechit.nl !
40
References
• Follow me on Twitter:
@BrruuD
• Contact me by e-mail:
ruud@partechit.nl
• Read our blog:
www.partechit.nl/blog
41
Thank
you
42
1 de 42

Recomendados

Sitecore MVC: Converting Web Forms sublayouts por
Sitecore MVC: Converting Web Forms sublayoutsSitecore MVC: Converting Web Forms sublayouts
Sitecore MVC: Converting Web Forms sublayoutsnonlinear creations
4.5K visualizações15 slides
Sitecore MVC (London User Group, April 29th 2014) por
Sitecore MVC (London User Group, April 29th 2014)Sitecore MVC (London User Group, April 29th 2014)
Sitecore MVC (London User Group, April 29th 2014)Ruud van Falier
915 visualizações29 slides
Sitecore MVC: What it is and why it's important por
Sitecore MVC: What it is and why it's importantSitecore MVC: What it is and why it's important
Sitecore MVC: What it is and why it's importantnonlinear creations
6.7K visualizações14 slides
Sitecore MVC Advanced por
Sitecore MVC AdvancedSitecore MVC Advanced
Sitecore MVC AdvancedKevin Brechbühl
3.8K visualizações50 slides
Sitecore mvc por
Sitecore mvcSitecore mvc
Sitecore mvcpratik satikunvar
1.5K visualizações16 slides
Asp 1a-aspnetmvc por
Asp 1a-aspnetmvcAsp 1a-aspnetmvc
Asp 1a-aspnetmvcFajar Baskoro
19 visualizações22 slides

Mais conteúdo relacionado

Mais procurados

Getting started with MVC 5 and Visual Studio 2013 por
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
14.9K visualizações40 slides
Asp.net mvc 5 course module 1 overview por
Asp.net mvc 5 course   module 1 overviewAsp.net mvc 5 course   module 1 overview
Asp.net mvc 5 course module 1 overviewSergey Seletsky
2.8K visualizações20 slides
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5 por
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
885 visualizações13 slides
ASP.NET MVC 5 - EF 6 - VS2015 por
ASP.NET MVC 5 - EF 6 - VS2015ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015Hossein Zahed
3.4K visualizações148 slides
ASP .NET MVC por
ASP .NET MVC ASP .NET MVC
ASP .NET MVC eldorina
1K visualizações13 slides
MVC 6 Introduction por
MVC 6 IntroductionMVC 6 Introduction
MVC 6 IntroductionSudhakar Sharma
1.5K visualizações45 slides

Mais procurados(20)

Getting started with MVC 5 and Visual Studio 2013 por Thomas Robbins
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 Robbins14.9K visualizações
Asp.net mvc 5 course module 1 overview por Sergey Seletsky
Asp.net mvc 5 course   module 1 overviewAsp.net mvc 5 course   module 1 overview
Asp.net mvc 5 course module 1 overview
Sergey Seletsky2.8K visualizações
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5 por Aaron Jacobson
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 Jacobson885 visualizações
ASP.NET MVC 5 - EF 6 - VS2015 por Hossein Zahed
ASP.NET MVC 5 - EF 6 - VS2015ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015
Hossein Zahed3.4K visualizações
ASP .NET MVC por eldorina
ASP .NET MVC ASP .NET MVC
ASP .NET MVC
eldorina1K visualizações
MVC 6 Introduction por Sudhakar Sharma
MVC 6 IntroductionMVC 6 Introduction
MVC 6 Introduction
Sudhakar Sharma1.5K visualizações
MSDN - ASP.NET MVC por Maarten Balliauw
MSDN - ASP.NET MVCMSDN - ASP.NET MVC
MSDN - ASP.NET MVC
Maarten Balliauw6K visualizações
Mvc architecture por Surbhi Panhalkar
Mvc architectureMvc architecture
Mvc architecture
Surbhi Panhalkar44.4K visualizações
ASP .net MVC por Divya Sharma
ASP .net MVCASP .net MVC
ASP .net MVC
Divya Sharma5.4K visualizações
Using MVC with Kentico 8 por Thomas Robbins
Using MVC with Kentico 8Using MVC with Kentico 8
Using MVC with Kentico 8
Thomas Robbins4.2K visualizações
Kentico and MVC por Cheryl MacDonald
Kentico and MVCKentico and MVC
Kentico and MVC
Cheryl MacDonald1.5K visualizações
Asp 1-mvc introduction por Fajar Baskoro
Asp 1-mvc introductionAsp 1-mvc introduction
Asp 1-mvc introduction
Fajar Baskoro2.1K visualizações
Asp.net mvc por Er. Kamal Bhusal
Asp.net mvcAsp.net mvc
Asp.net mvc
Er. Kamal Bhusal1.6K visualizações
ASP.NET MVC Presentation por ivpol
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
ivpol23.6K visualizações
MVC 1.0 als alternative Webtechnologie por OPEN KNOWLEDGE GmbH
MVC 1.0 als alternative WebtechnologieMVC 1.0 als alternative Webtechnologie
MVC 1.0 als alternative Webtechnologie
OPEN KNOWLEDGE GmbH324 visualizações
MVC ppt presentation por Bhavin Shah
MVC ppt presentationMVC ppt presentation
MVC ppt presentation
Bhavin Shah14K visualizações
Asp.net mvc presentation by Nitin Sawant por Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin Sawant
Nitin Sawant2.3K visualizações
Asp.net mvc basic introduction por Bhagath Gopinath
Asp.net mvc basic introductionAsp.net mvc basic introduction
Asp.net mvc basic introduction
Bhagath Gopinath295 visualizações
Mvc4 por Muhammad Younis
Mvc4Mvc4
Mvc4
Muhammad Younis8.5K visualizações
Mvc por abhigad
MvcMvc
Mvc
abhigad579 visualizações

Similar a Sitecore MVC (User Group Conference, May 23rd 2014)

Aspnetmvc 1 por
Aspnetmvc 1Aspnetmvc 1
Aspnetmvc 1Fajar Baskoro
2K visualizações22 slides
Using a model view-view model architecture for iOS apps por
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
582 visualizações29 slides
Jinal desai .net por
Jinal desai .netJinal desai .net
Jinal desai .netrohitkumar1987in
321 visualizações4 slides
Web engineering - MVC por
Web engineering - MVCWeb engineering - MVC
Web engineering - MVCNosheen Qamar
1.2K visualizações18 slides
MVC por
MVCMVC
MVCRavi Bansal
755 visualizações10 slides
Spring MVC Framework por
Spring MVC FrameworkSpring MVC Framework
Spring MVC FrameworkHùng Nguyễn Huy
3.9K visualizações24 slides

Similar a Sitecore MVC (User Group Conference, May 23rd 2014)(20)

Aspnetmvc 1 por Fajar Baskoro
Aspnetmvc 1Aspnetmvc 1
Aspnetmvc 1
Fajar Baskoro2K visualizações
Using a model view-view model architecture for iOS apps por allanh0526
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
allanh0526582 visualizações
Jinal desai .net por rohitkumar1987in
Jinal desai .netJinal desai .net
Jinal desai .net
rohitkumar1987in321 visualizações
Web engineering - MVC por Nosheen Qamar
Web engineering - MVCWeb engineering - MVC
Web engineering - MVC
Nosheen Qamar1.2K visualizações
MVC por Ravi Bansal
MVCMVC
MVC
Ravi Bansal755 visualizações
Spring MVC Framework por Hùng Nguyễn Huy
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
Hùng Nguyễn Huy3.9K visualizações
Mvc interview questions – deep dive jinal desai por jinaldesailive
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
jinaldesailive12.7K visualizações
MVC Framework por Ashton Feller
MVC FrameworkMVC Framework
MVC Framework
Ashton Feller1.4K visualizações
MVC Seminar Presantation por Abhishek Yadav
MVC Seminar PresantationMVC Seminar Presantation
MVC Seminar Presantation
Abhishek Yadav6.8K visualizações
Asp.net c# MVC-5 Training-Day-1 of Day-9 por AHM Pervej Kabir
Asp.net c# MVC-5 Training-Day-1 of Day-9Asp.net c# MVC-5 Training-Day-1 of Day-9
Asp.net c# MVC-5 Training-Day-1 of Day-9
AHM Pervej Kabir923 visualizações
Knockout implementing mvvm in java script with knockout por Andoni Arroyo
Knockout implementing mvvm in java script with knockoutKnockout implementing mvvm in java script with knockout
Knockout implementing mvvm in java script with knockout
Andoni Arroyo1.2K visualizações
Architectural Design & Patterns por Inocentshuja Ahmad
Architectural Design&PatternsArchitectural Design&Patterns
Architectural Design & Patterns
Inocentshuja Ahmad679 visualizações
Top 40 MVC Interview Questions and Answers | Edureka por Edureka!
Top 40 MVC Interview Questions and Answers | EdurekaTop 40 MVC Interview Questions and Answers | Edureka
Top 40 MVC Interview Questions and Answers | Edureka
Edureka!250 visualizações
Lecture 05 - Creating a website with Razor Pages.pdf por Lê Thưởng
Lecture 05 - Creating a website with Razor Pages.pdfLecture 05 - Creating a website with Razor Pages.pdf
Lecture 05 - Creating a website with Razor Pages.pdf
Lê Thưởng1 visão
Building Modern Websites with ASP.NET by Rachel Appel por .NET Conf UY
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 UY1K visualizações
Introduction to Struts 1.3 por Ilio Catallo
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
Ilio Catallo16.3K visualizações
DDD, CQRS and testing with ASP.Net MVC por Andy Butland
DDD, CQRS and testing with ASP.Net MVCDDD, CQRS and testing with ASP.Net MVC
DDD, CQRS and testing with ASP.Net MVC
Andy Butland7.7K visualizações
Mvc 130330091359-phpapp01 por Jennie Gajjar
Mvc 130330091359-phpapp01Mvc 130330091359-phpapp01
Mvc 130330091359-phpapp01
Jennie Gajjar264 visualizações
Principles of MVC for PHP Developers por Edureka!
Principles of MVC for PHP DevelopersPrinciples of MVC for PHP Developers
Principles of MVC for PHP Developers
Edureka!3.1K visualizações

Mais de Ruud van Falier

Sitecore Experience Accelerator (SxA) por
Sitecore Experience Accelerator (SxA)Sitecore Experience Accelerator (SxA)
Sitecore Experience Accelerator (SxA)Ruud van Falier
2.2K visualizações29 slides
The Art of Sitecore Upgrades por
The Art of Sitecore UpgradesThe Art of Sitecore Upgrades
The Art of Sitecore UpgradesRuud van Falier
262 visualizações36 slides
Introducing Sitecore Habitat - SUGCON EU 2016 por
Introducing Sitecore Habitat - SUGCON EU 2016Introducing Sitecore Habitat - SUGCON EU 2016
Introducing Sitecore Habitat - SUGCON EU 2016Ruud van Falier
469 visualizações34 slides
Sitecore Habitat (User Group NL, February 11th 2016) por
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
1K visualizações26 slides
Managing your user data with Sitecore xDB por
Managing your user data with Sitecore xDBManaging your user data with Sitecore xDB
Managing your user data with Sitecore xDBRuud van Falier
1.9K visualizações12 slides
Sitecore - Onder de motorkop van ParTechIT.nl por
Sitecore - Onder de motorkop van ParTechIT.nlSitecore - Onder de motorkop van ParTechIT.nl
Sitecore - Onder de motorkop van ParTechIT.nlRuud van Falier
1.4K visualizações15 slides

Mais de Ruud van Falier(6)

Sitecore Experience Accelerator (SxA) por Ruud van Falier
Sitecore Experience Accelerator (SxA)Sitecore Experience Accelerator (SxA)
Sitecore Experience Accelerator (SxA)
Ruud van Falier2.2K visualizações
The Art of Sitecore Upgrades por Ruud van Falier
The Art of Sitecore UpgradesThe Art of Sitecore Upgrades
The Art of Sitecore Upgrades
Ruud van Falier262 visualizações
Introducing Sitecore Habitat - SUGCON EU 2016 por Ruud van Falier
Introducing Sitecore Habitat - SUGCON EU 2016Introducing Sitecore Habitat - SUGCON EU 2016
Introducing Sitecore Habitat - SUGCON EU 2016
Ruud van Falier469 visualizações
Sitecore Habitat (User Group NL, February 11th 2016) por 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)
Ruud van Falier1K visualizações
Managing your user data with Sitecore xDB por Ruud van Falier
Managing your user data with Sitecore xDBManaging your user data with Sitecore xDB
Managing your user data with Sitecore xDB
Ruud van Falier1.9K visualizações
Sitecore - Onder de motorkop van ParTechIT.nl por Ruud van Falier
Sitecore - Onder de motorkop van ParTechIT.nlSitecore - Onder de motorkop van ParTechIT.nl
Sitecore - Onder de motorkop van ParTechIT.nl
Ruud van Falier1.4K visualizações

Último

BLogSite (Web Programming) (1).pdf por
BLogSite (Web Programming) (1).pdfBLogSite (Web Programming) (1).pdf
BLogSite (Web Programming) (1).pdfFiverr
11 visualizações9 slides
OSMC 2023 | Know your data: The stats behind your alerts by Dave McAllister por
OSMC 2023 | Know your data: The stats behind your alerts by Dave McAllisterOSMC 2023 | Know your data: The stats behind your alerts by Dave McAllister
OSMC 2023 | Know your data: The stats behind your alerts by Dave McAllisterNETWAYS
10 visualizações38 slides
OSMC 2023 | IGNITE: Serving Server-Side WASM with Web Awareness with NGINX Un... por
OSMC 2023 | IGNITE: Serving Server-Side WASM with Web Awareness with NGINX Un...OSMC 2023 | IGNITE: Serving Server-Side WASM with Web Awareness with NGINX Un...
OSMC 2023 | IGNITE: Serving Server-Side WASM with Web Awareness with NGINX Un...NETWAYS
11 visualizações19 slides
Thanks Giving Encouragement Wednesday.pptx por
Thanks Giving Encouragement Wednesday.pptxThanks Giving Encouragement Wednesday.pptx
Thanks Giving Encouragement Wednesday.pptxFamilyWorshipCenterD
10 visualizações13 slides
231121 SP slides - PAS workshop November 2023.pdf por
231121 SP slides - PAS workshop November 2023.pdf231121 SP slides - PAS workshop November 2023.pdf
231121 SP slides - PAS workshop November 2023.pdfPAS_Team
150 visualizações15 slides
Helko van den Brom - VSL por
Helko van den Brom - VSLHelko van den Brom - VSL
Helko van den Brom - VSLDutch Power
63 visualizações18 slides

Último(20)

BLogSite (Web Programming) (1).pdf por Fiverr
BLogSite (Web Programming) (1).pdfBLogSite (Web Programming) (1).pdf
BLogSite (Web Programming) (1).pdf
Fiverr11 visualizações
OSMC 2023 | Know your data: The stats behind your alerts by Dave McAllister por NETWAYS
OSMC 2023 | Know your data: The stats behind your alerts by Dave McAllisterOSMC 2023 | Know your data: The stats behind your alerts by Dave McAllister
OSMC 2023 | Know your data: The stats behind your alerts by Dave McAllister
NETWAYS10 visualizações
OSMC 2023 | IGNITE: Serving Server-Side WASM with Web Awareness with NGINX Un... por NETWAYS
OSMC 2023 | IGNITE: Serving Server-Side WASM with Web Awareness with NGINX Un...OSMC 2023 | IGNITE: Serving Server-Side WASM with Web Awareness with NGINX Un...
OSMC 2023 | IGNITE: Serving Server-Side WASM with Web Awareness with NGINX Un...
NETWAYS11 visualizações
Thanks Giving Encouragement Wednesday.pptx por FamilyWorshipCenterD
Thanks Giving Encouragement Wednesday.pptxThanks Giving Encouragement Wednesday.pptx
Thanks Giving Encouragement Wednesday.pptx
FamilyWorshipCenterD10 visualizações
231121 SP slides - PAS workshop November 2023.pdf por PAS_Team
231121 SP slides - PAS workshop November 2023.pdf231121 SP slides - PAS workshop November 2023.pdf
231121 SP slides - PAS workshop November 2023.pdf
PAS_Team150 visualizações
Helko van den Brom - VSL por Dutch Power
Helko van den Brom - VSLHelko van den Brom - VSL
Helko van den Brom - VSL
Dutch Power63 visualizações
New Microsoft Word Document.docx por apomahendranagarmudd
New Microsoft Word Document.docxNew Microsoft Word Document.docx
New Microsoft Word Document.docx
apomahendranagarmudd7 visualizações
The Throne of Your Heart 11-26-23 PPT.pptx por FamilyWorshipCenterD
The Throne of Your Heart 11-26-23 PPT.pptxThe Throne of Your Heart 11-26-23 PPT.pptx
The Throne of Your Heart 11-26-23 PPT.pptx
FamilyWorshipCenterD5 visualizações
Yin Sun - Shell por Dutch Power
Yin Sun - ShellYin Sun - Shell
Yin Sun - Shell
Dutch Power62 visualizações
OSMC | SNMP Monitoring at scale by Rocco Pezzani & Thomas Gelf por NETWAYS
OSMC | SNMP Monitoring at scale by Rocco Pezzani & Thomas Gelf OSMC | SNMP Monitoring at scale by Rocco Pezzani & Thomas Gelf
OSMC | SNMP Monitoring at scale by Rocco Pezzani & Thomas Gelf
NETWAYS11 visualizações
Post-event report intro session-1.docx por RohitRathi59
Post-event report intro session-1.docxPost-event report intro session-1.docx
Post-event report intro session-1.docx
RohitRathi5912 visualizações
Roozbeh Torkzadeh - TU Eindhoven por Dutch Power
Roozbeh Torkzadeh - TU EindhovenRoozbeh Torkzadeh - TU Eindhoven
Roozbeh Torkzadeh - TU Eindhoven
Dutch Power62 visualizações
CitSciOz MOUA Inspiring Change Through Art por Christian Bartens
CitSciOz MOUA Inspiring Change Through ArtCitSciOz MOUA Inspiring Change Through Art
CitSciOz MOUA Inspiring Change Through Art
Christian Bartens43 visualizações
Christan van Dorst - Hyteps por Dutch Power
Christan van Dorst - HytepsChristan van Dorst - Hyteps
Christan van Dorst - Hyteps
Dutch Power65 visualizações
OSMC 2023 | Icinga for Windows – Age of PowerShell by Christian Stein por NETWAYS
OSMC 2023 | Icinga for Windows – Age of PowerShell by Christian SteinOSMC 2023 | Icinga for Windows – Age of PowerShell by Christian Stein
OSMC 2023 | Icinga for Windows – Age of PowerShell by Christian Stein
NETWAYS8 visualizações
Speaking with confidence-converted.pdf por Abdul salam
Speaking with confidence-converted.pdfSpeaking with confidence-converted.pdf
Speaking with confidence-converted.pdf
Abdul salam 16 visualizações
OSMC 2023 | IGNITE: Metrics, Margins, Mutiny – How to make your SREs (not) ru... por NETWAYS
OSMC 2023 | IGNITE: Metrics, Margins, Mutiny – How to make your SREs (not) ru...OSMC 2023 | IGNITE: Metrics, Margins, Mutiny – How to make your SREs (not) ru...
OSMC 2023 | IGNITE: Metrics, Margins, Mutiny – How to make your SREs (not) ru...
NETWAYS7 visualizações
falsettos por RenzoCalandra
falsettosfalsettos
falsettos
RenzoCalandra6 visualizações
SOA PPT ON SEA TURTLES.pptx por EuniceOseiYeboah
SOA PPT ON SEA TURTLES.pptxSOA PPT ON SEA TURTLES.pptx
SOA PPT ON SEA TURTLES.pptx
EuniceOseiYeboah9 visualizações

Sitecore MVC (User Group Conference, May 23rd 2014)

  • 2. Topics • The basic concept • Traditional ASP.NET MVC • Sitecore MVC • Sitecore renderings • Controllers • Models • Model binding • Views • SitecoreHelper • Inversion of Control • Dependency Injection • MVC vs. WebForms 2
  • 3. How we used to roll… The basic concept of (Sitecore) MVC 3
  • 4. 4
  • 5. The basic concept of (Sitecore) MVC 5 View Model Controller User Uses Manipulates Updates Sees
  • 6. The basic concept of (Sitecore) 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. 6
  • 7. The basic concept of (Sitecore) MVC A traditional ASP.NET MVC request 7 Browser URL Routing Controller Model View Request Invoke action Initialize Lookup view Render HTML
  • 8. The basic concept of (Sitecore) MVC A Sitecore MVC request 8 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 Source: Martina Welander
  • 9. Sitecore Renderings •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. 9
  • 11. Controllers 11 PageController About Portfolio News Request to: /page/news var model = repository.GetNews(); return View(model); /Views/News.cshtml
  • 12. Controllers 12 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 }); } }
  • 13. Controllers 13 public class MvcDemoController : Controller { public ViewResult SearchNews(string keyword, int page) { // Perform search and return NewsOverViewModel with results. var model = new NewsOverviewModel(); return this.View(model); } } /MvcDemo/SearchNews?keyword=my_search_terms&page=1
  • 14. Controllers 14 /MvcDemo/SearchNews?keyword=my_search_terms&page=1 public class MvcDemoController : Controller { public ViewResult SearchNews(SearchRequestModel request) { // Perform search and return NewsOverViewModel with results. var model = new NewsOverviewModel(); return this.View(model); } }
  • 15. Controllers 15 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
  • 17. Models 17 public class ContentPageModel { public string Title { get; set; } public string Intro { get; set; } public string Body { get; set; } }
  • 18. Models 18 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; } }
  • 19. Models 19 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. } }
  • 20. Model Binding 20 public class SearchRequestModel { public string Keyword { get; set; } public int Page { get; set; } public bool OrderByRelevance { get; set; } } public class MvcDemoController : Controller { public ViewResult SearchNews(SearchRequestModel request) { // Perform search and return NewsOverViewModel with results. var model = new NewsOverviewModel(); return this.View(model); } } /MvcDemo/SearchNews?keyword=my_search_terms&page=1
  • 21. Models In Sitecore MVC: • View Models usually represent Templates from Sitecore. • This way, View Models can contain content from Sitecore items. • There are several Object-Relational Mappers that are created specifically for that task. Make sure to attend Mike Edwards’ and Robin Hermanussen’s session for more on ORM! 21
  • 22. Models 22 Do • Hold data • Provide logic to manipulate data Don’t • Add presentation elements to data • Use for application logic
  • 26. Views 26 @model RenderingModel <section id="main_content"> <h3> @Model.Item["Title"] </h3> <p> <strong> @Model.Item["Intro"] </strong> </p> @Model.Item["Body"] </section> @model RenderingModel <section id="main_content"> <h3> @Model.Item["Title"] </h3> <p> <strong> @Html.Raw(Model.Item["Intro"]) </strong> </p> @Html.Raw(Model.Item["Body"]) </section>
  • 28. Views 28 using System.Web; using Sitecore.Mvc.Helpers; /// <summary> /// Extension methods for the SitecoreHelper class. /// </summary> public static class SitecoreHelperExtensions { public static HtmlString WhoAreYou(this SitecoreHelper helper) { return new HtmlString("I'm your father!"); } } @Html.Sitecore().WhoAreYou() @* Output: "I'm your father" *@
  • 29. Views 29 public class ContentController : Controller { public ViewResult ContentPage() { var model = new ContentPageModel(); // Populate model properties with data from Sitecore. return this.View("~/Views/Content page.cshtml", model); } } public class ContentPageModel { public string Title { get; set; } public string Intro { get; set; } public string Body { get; set; } }
  • 30. Views 30 public class ContentController : Controller { public ViewResult ContentPage() { var model = new ContentPageModel(); // Populate model properties with data from Sitecore. return this.View("~/Views/Content page.cshtml", model); } } public class ContentPageModel { public string Title { get; set; } public string Intro { get; set; } public string Body { get; set; } } @model ContentPageModel <section id="main_content"> <h3> @Model.Title </h3> <p> <strong> @Model.Intro </strong> </p> @Model.Body </section>
  • 31. Views 31 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
  • 32. Inversion of Control (IoC) 32 “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.
  • 33. Inversion of Control (IoC) IoC 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 (interfaces). • To prevent side effects when replacing a module. 33
  • 34. Inversion of Control (IoC) 34 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 }); } }
  • 35. Inversion of Control (IoC) 35 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
  • 36. Inversion of Control (IoC) 36 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 }); } }
  • 37. Inversion of Control (IoC) 37 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
  • 38. Dependency Injection (DI) There are several methods for DI, some examples are: • Constructor injection (as seen in the example) • Parameter injection • Setter injection Use a framework that handles DI for you: • Windsor container (because it ships with Glass) • Ninject • Autofac 38
  • 39. MVC vs. WebForms 39 Why MVC is better • Strict separation of responsibilities • Simpler page lifecycle. • Stateless (No more ViewState). • No more server controls. • Very easy to work with AJAX. • Not bound to generated markup. • Multiple forms on a page. When to stick to WebForms • Legacy application • Team knowledge • Prototyping WebForms is just an extremely complex abstraction over HTML/JS, invented before the introduction of jQuery and AJAX; We don’t need this abstraction anymore.
  • 40. Join us! Are you an awesome Sitecore Developer? ParTech is looking for people to expand their team! We offer you an excellent salary and benefits and the chance to work on enterprise Sitecore projects together with the most skilled developers available. Join the Sitecore elite, apply at www.partechit.nl ! 40
  • 41. References • Follow me on Twitter: @BrruuD • Contact me by e-mail: ruud@partechit.nl • Read our blog: www.partechit.nl/blog 41