SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Optimizing Code Reusability For 
SharePoint Using LINQ and the 
MVP Design Pattern 
Houston Tech Fest 2014
Introduction – Ted Wagner 
BS in Game Software Development 
SharePoint SME for over 3 Years. 
Conducted multiple training sessions for 
SharePoint 2010 and 2013. 
Worked for Sparkhound over a year. 
2
“Be a hero. Grow a beard.” 
During September, the Sparkhound Foundation is 
sponsoring the growth of facial hair. 
Why? To help raise funds for research and 
treatment of prostate cancer. Learn more at 
Septembeard.org. 
$680,380 
raised since 
2011
4 
Thank you for being a part of the 
8th Annual Houston TechFest! 
Please turn off all electronic devices or set them to vibrate. 
If you must take a phone call, please do so in the lobby so as 
not to disturb others. 
Thanks to our Diamond Sponsors:
5 
Overview 
Review the MVP pattern 
Setting up models to promote 
encapsulation and testing 
Setting up Presenters and Views for 
separation of functionality to promote 
testing
Outcome 
Reusable SharePoint solutions to be used between 
various applications and SharePoint projects. 
6
What is MVP? 
A design pattern used to separate functionality 
from controls to maximize the amount of code 
that can be tested automatically. 
Separation of the logic of events and the 
updates made to the application data. 
Uses an Interface to reference the view 
instead of the view class 
Many different variations 
7
Microsoft Corporation. (2014). The Model-View-Presenter (MVP) Pattern. Retrieved September 3, 2014, from Microsoft Developer Network: http://msdn.microsoft.What is MVP? 
MVP image (Microsoft 
8 
Supervising Presenter 
Corporation, 2014)
Why use MVP with SharePoint? 
MVC is technically “not supported” 
Designs are usually scoped to one view – 
Web Parts 
Increase Unit Testing 
Encapsulation of SharePoint Methods and 
Assemblies 
9
Model 
Data Model 
1. Data Model 
1. Representation of columns within a List or Library 
2. Business Model 
1. Represented as an interface 
2. Data access object used to query database 
10
View 
Class Containing visual controls 
Handles events 
Contains Page Events, JavaScript, CSS, 
JQuery and any other Front-End functionality 
Views do not have knowledge of the model 
An interface is designated to the view for 
updating the view state when the model is 
updated. 
11
Presenter 
The class that combines the view to the data 
models. 
Updates the view. 
One presenter per view 
Easily attach to Unit testing projects for 
automation. 
12
LINQ to SharePoint 
Utilize LINQ to easily query SharePoint Lists 
Provides quick way to cast objects to generics 
for encapsulation 
Quickly generate models representing 
SharePoint Lists using SPMetal 
13
Setting up Solution 
Create a Class Library 
Project for storing Models 
Create Project for SharePoint 
Solutions 
Sign key 
Add assembly to package 
Package->Advanced->Add 
14
Setting up your Models 
‱ Create a separate project for you models only 
‱ Setup your data models 
‱ Setup Data Access Objects and business model 
interfaces 
Best Practices 
‱ Keep all SharePoint references in the models 
‱ Keep error handling to a minimum in the 
models 
15
Data Model Example 
16 
public class CustomerModel 
{ 
public string CustId { get; set; } 
public string FirstName { get; set; } 
public string LastName { get; set; } 
public string Address { get; set; } 
public string City { get; set; } 
public string State { get; set; } 
public string Zip { get; set; } 
public bool Vendor { get; set; } 
}
Generate DataContext With SPMetal 
‱ SPMetal – located in the BIN folder in Hive 
‱ Create Environment Variable referencing 
SPMetal in hive folder 
‱ System Properties -> Advanced Settings -> 
Advanced 
‱ Create Folder for storing DataContext Files 
17 
C:SPMetal /web:http://sparkhound.ted.local 
/code:CustomerDataContext.cs
Using DataContext Model 
Using Preconfigured data models from SPMetal 
Pros 
Generates exact column names 
No need to create data models 
Cons 
Cannot keep SharePoint functionality within the model 
Hard to find and update models 
18
Example 
19 
public List<CustomerItem> GetCustomers() 
{ 
using (TechfestdatacontextDataContext dc = new 
TechfestdatacontextDataContext(SPContext.Current.Site.Url)) 
{ 
EntityList<CustomerItem> customers = 
dc.GetList<CustomerItem>("Customer"); 
var allCustomers = from c in customers 
select c; 
return allCustomers.ToList<CustomerItem>(); 
} 
}
Using Custom Data Model 
Designing your own Data Model 
Pros 
Flexibility to control what data is passed 
Control names 
Encapsulate SharePoint references 
Cons 
Must create each data model manually 
20
Example 
21 
using(TechfestdatacontextDataContext dc = new 
TechfestdatacontextDataContext(SPContext.Current.Site.Url)) 
{ 
EntityList<RegisterItem> register = dc.GetList<RegisterItem>("Register"); 
var customerDetails = from r in register 
where r.CustID == CustId 
select new RegisterModel 
{ 
CustId = r.CustID, 
Credit = Convert.ToDouble(r.Credit), 
Debit = Convert.ToDouble(r.Debit), 
Date = Convert.ToDateTime(r.Date), 
RegisterId = r.Title 
}; 
return customerDetails.ToList<RegisterModel>(); 
}
Setting up the presenter 
The presenter will reference the view through an 
interface 
Overload the constructor to pass the view 
All functionality is handled by the presenter 
Best Practices 
Use bool functions to catch errors. 
Return false for errors to keep other functionality from 
continuing. 
22
Example 
Constructor accepting view and assigning it to a 
variable 
23 
public CustomerInfoPresenter(ICustomerInfoView View) 
{ 
view = View; 
}
Example 
24 
public bool GetCustomerInfo() 
{ 
try 
{ 
CustomerBusinessObject model = new 
CustomerBusinessObject(); 
view.GetCustomers = model.GetCustomers(); 
return true; 
} 
catch (Exception ex) 
{ 
view.ErrorMsg = "Error Getting Customer Info: " + ex.Message; 
return false; 
} 
}
Setting up the view 
Views use properties to assign values and populate 
controls 
Minimum functionality in views 
Represent data in an interface to pass data 
25 
public interface ICustomerInfoView 
{ 
List<CustomerItem> GetCustomers {get; set;} 
List<RegisterModel> CustomerData {get; set;} 
string ErrorMsg { get; set; } 
}
Example 
26 
public List<CustomerItem> GetCustomers 
{ 
get 
{ 
return CustomerDDL.DataSource as List<CustomerItem>; 
} 
set 
{ 
CustomerDDL.DataTextField = "LastName"; 
CustomerDDL.DataValueField = "Title"; 
CustomerDDL.DataSource = value; 
CustomerDDL.DataBind(); 
CustomerDDL.Items.Insert(0, "Please select a customer..."); 
} 
}
Example – Page Load 
27 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!Page.IsPostBack) 
{ 
CustomerInfoPresenter presenter = new 
CustomerInfoPresenter(this); 
presenter.GetCustomerInfo(); 
} 
}
Pros/Cons 
Pros 
Encapsulation 
Easily give code to other developers 
Reusability with other projects and applications 
Ability to manipulate and bind data after query 
Cons 
Issues with cross site collection queries 
LINQ is not Thread Safe 
Slower than CAML 
Extra coding overhead 
28
Overcoming cross site collection queries 
1. Extend DataContext Model to include a new 
constructor that accepts different site 
collections. 
2. Change the Current Context to the site 
collection being queried. 
29
Overcoming cross site collection queries 
30 
var CurrentContext = HttpContext.Current; 
(using SPSite site = new SPSite(SiteCollectionUrl)) 
{ 
(using(SPWeb web = site.OpenWeb()) 
{ 
HttpRequest request = new HttpRequest(“”, SiteCollectionURL, “”); 
HttpContext.Current = new HttpContext(request, new 
HttpRespone(new StringWriter())); 
//Perform Linq Query 
} 
} 
HttpContext.Current = CurrentContext;
Questions 
31
Please Leave Feedback 
32 
If you leave session 
feedback and 
provide contact 
information in the 
survey, you will be 
qualified for a prize 
Scan the QR Code 
to the right or go to 
http://bit.ly/1p13f3n
Contact Info 
Ted Wagner 
Ted.wagner@sparkhound.com 
33
34 
Thanks to all our Sponsors!
Works Cited 
Microsoft Corporation. (2014). The Model-View-Presenter (MVP) Pattern. Retrieved 
September 3, 2014, from Microsoft Developer Network: http://msdn.microsoft.com/en-us/ 
library/ff649571.aspx 
35
36

Weitere Àhnliche Inhalte

Was ist angesagt?

Orion Context Broker workshop (CPMX5)
Orion Context Broker workshop (CPMX5)Orion Context Broker workshop (CPMX5)
Orion Context Broker workshop (CPMX5)
Fermin Galan
 
Business objects integration kit for sap crystal reports 2008
Business objects integration kit for sap   crystal reports 2008Business objects integration kit for sap   crystal reports 2008
Business objects integration kit for sap crystal reports 2008
Yogeeswar Reddy
 
Services ax2012
Services ax2012Services ax2012
Services ax2012
Pranav Gupta
 
Encompassing Information Integration
Encompassing Information IntegrationEncompassing Information Integration
Encompassing Information Integration
nguyenfilip
 
Orion context broker webminar 2013 06-19
Orion context broker webminar 2013 06-19Orion context broker webminar 2013 06-19
Orion context broker webminar 2013 06-19
Fermin Galan
 
Portfolio Genet
Portfolio GenetPortfolio Genet
Portfolio Genet
Genet Tadesse
 

Was ist angesagt? (14)

Smart application on Azure at Vattenfall - Rens Weijers & Peter van 't Hof
Smart application on Azure at Vattenfall - Rens Weijers & Peter van 't HofSmart application on Azure at Vattenfall - Rens Weijers & Peter van 't Hof
Smart application on Azure at Vattenfall - Rens Weijers & Peter van 't Hof
 
Angular2
Angular2Angular2
Angular2
 
Orion Context Broker webminar 2014 01-22
Orion Context Broker webminar 2014 01-22Orion Context Broker webminar 2014 01-22
Orion Context Broker webminar 2014 01-22
 
Building N Tier Applications With Entity Framework Services 2010
Building N Tier Applications With Entity Framework Services 2010Building N Tier Applications With Entity Framework Services 2010
Building N Tier Applications With Entity Framework Services 2010
 
Orion Context Broker workshop (CPMX5)
Orion Context Broker workshop (CPMX5)Orion Context Broker workshop (CPMX5)
Orion Context Broker workshop (CPMX5)
 
Business objects integration kit for sap crystal reports 2008
Business objects integration kit for sap   crystal reports 2008Business objects integration kit for sap   crystal reports 2008
Business objects integration kit for sap crystal reports 2008
 
Services ax2012
Services ax2012Services ax2012
Services ax2012
 
Encompassing Information Integration
Encompassing Information IntegrationEncompassing Information Integration
Encompassing Information Integration
 
How to create an InfoCube
How to create an InfoCubeHow to create an InfoCube
How to create an InfoCube
 
Hybrid provideer
Hybrid provideerHybrid provideer
Hybrid provideer
 
Orion context broker webminar 2013 06-19
Orion context broker webminar 2013 06-19Orion context broker webminar 2013 06-19
Orion context broker webminar 2013 06-19
 
prashat resume
prashat resumeprashat resume
prashat resume
 
Sap business objects 4 quick start manual
Sap business objects 4 quick start manualSap business objects 4 quick start manual
Sap business objects 4 quick start manual
 
Portfolio Genet
Portfolio GenetPortfolio Genet
Portfolio Genet
 

Andere mochten auch

Wireframes and Interaction Design Documents
Wireframes and Interaction Design DocumentsWireframes and Interaction Design Documents
Wireframes and Interaction Design Documents
piksels
 

Andere mochten auch (13)

Lync, Exchange, Sharepoint and Office Web Apps, the Fantastic 4 of Communi...
Lync, Exchange, Sharepoint  and Office Web Apps, the  Fantastic 4 of  Communi...Lync, Exchange, Sharepoint  and Office Web Apps, the  Fantastic 4 of  Communi...
Lync, Exchange, Sharepoint and Office Web Apps, the Fantastic 4 of Communi...
 
Session ID1 Lecture 1 -What is Interaction Design
Session ID1 Lecture 1 -What is Interaction DesignSession ID1 Lecture 1 -What is Interaction Design
Session ID1 Lecture 1 -What is Interaction Design
 
Introduction to Interaction Design Course 2017 (5 ECTS)
Introduction to Interaction Design Course 2017 (5 ECTS)Introduction to Interaction Design Course 2017 (5 ECTS)
Introduction to Interaction Design Course 2017 (5 ECTS)
 
Design in motion. The new frontier of interaction design
Design in motion. The new frontier of interaction designDesign in motion. The new frontier of interaction design
Design in motion. The new frontier of interaction design
 
What's new in SharePoint 2016 for IT Professionals Webinar with CrowCanyon
What's new in SharePoint 2016 for IT Professionals Webinar with CrowCanyonWhat's new in SharePoint 2016 for IT Professionals Webinar with CrowCanyon
What's new in SharePoint 2016 for IT Professionals Webinar with CrowCanyon
 
The Future of Adaptive Content
The Future of Adaptive ContentThe Future of Adaptive Content
The Future of Adaptive Content
 
Wireframes and Interaction Design Documents
Wireframes and Interaction Design DocumentsWireframes and Interaction Design Documents
Wireframes and Interaction Design Documents
 
Foundations of Interaction Design
Foundations of Interaction DesignFoundations of Interaction Design
Foundations of Interaction Design
 
A Deep Dive into SharePoint 2016 architecture and deployment
A Deep Dive into SharePoint 2016 architecture and deploymentA Deep Dive into SharePoint 2016 architecture and deployment
A Deep Dive into SharePoint 2016 architecture and deployment
 
Interaction Design History
Interaction Design HistoryInteraction Design History
Interaction Design History
 
aOS Canadian Tour 2017 - Toronto- What do YOU get from SharePoint Hybrid?
aOS Canadian Tour 2017 - Toronto-   What do YOU get from SharePoint Hybrid?aOS Canadian Tour 2017 - Toronto-   What do YOU get from SharePoint Hybrid?
aOS Canadian Tour 2017 - Toronto- What do YOU get from SharePoint Hybrid?
 
Introduction to SharePoint Information Architecture
Introduction to SharePoint Information ArchitectureIntroduction to SharePoint Information Architecture
Introduction to SharePoint Information Architecture
 
The Role of Metaphor in Interaction Design
The Role of Metaphor in Interaction DesignThe Role of Metaphor in Interaction Design
The Role of Metaphor in Interaction Design
 

Ähnlich wie Optimizing Code Reusability for SharePoint using Linq to SharePoint & the MVP Design Pattern

C# .NET Developer Portfolio
C# .NET Developer PortfolioC# .NET Developer Portfolio
C# .NET Developer Portfolio
cummings49
 
Pratk kambe rac
Pratk kambe racPratk kambe rac
Pratk kambe rac
Pratik Kambe
 
ASP.NET MVC Fundamental
ASP.NET MVC FundamentalASP.NET MVC Fundamental
ASP.NET MVC Fundamental
ldcphuc
 
Sap business objects bobi training
Sap business objects bobi trainingSap business objects bobi training
Sap business objects bobi training
FuturePoint Technologies
 

Ähnlich wie Optimizing Code Reusability for SharePoint using Linq to SharePoint & the MVP Design Pattern (20)

ASP.NET Lecture 4
ASP.NET Lecture 4ASP.NET Lecture 4
ASP.NET Lecture 4
 
Mvc summary
Mvc summaryMvc summary
Mvc summary
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentation
 
Work with data in ASP.NET
Work with data in ASP.NETWork with data in ASP.NET
Work with data in ASP.NET
 
Supercharge your data analytics with BigQuery
Supercharge your data analytics with BigQuerySupercharge your data analytics with BigQuery
Supercharge your data analytics with BigQuery
 
Developing Next-Gen Enterprise Web Application
Developing Next-Gen Enterprise Web ApplicationDeveloping Next-Gen Enterprise Web Application
Developing Next-Gen Enterprise Web Application
 
C# .NET Developer Portfolio
C# .NET Developer PortfolioC# .NET Developer Portfolio
C# .NET Developer Portfolio
 
Pratk kambe rac
Pratk kambe racPratk kambe rac
Pratk kambe rac
 
O365 Saturday - Deepdive SharePoint Client Side Rendering
O365 Saturday - Deepdive SharePoint Client Side RenderingO365 Saturday - Deepdive SharePoint Client Side Rendering
O365 Saturday - Deepdive SharePoint Client Side Rendering
 
Tdd,Ioc
Tdd,IocTdd,Ioc
Tdd,Ioc
 
ASP.NET MVC Fundamental
ASP.NET MVC FundamentalASP.NET MVC Fundamental
ASP.NET MVC Fundamental
 
Sap business objects bobi training
Sap business objects bobi trainingSap business objects bobi training
Sap business objects bobi training
 
Simplify Feature Engineering in Your Data Warehouse
Simplify Feature Engineering in Your Data WarehouseSimplify Feature Engineering in Your Data Warehouse
Simplify Feature Engineering in Your Data Warehouse
 
Sap bo xi r4.0
Sap bo xi r4.0Sap bo xi r4.0
Sap bo xi r4.0
 
Sap bo xi r4.0
Sap bo xi r4.0Sap bo xi r4.0
Sap bo xi r4.0
 
Sap bo xi r4.0
Sap bo xi r4.0Sap bo xi r4.0
Sap bo xi r4.0
 
Sap bo xi r4.0
Sap bo xi r4.0Sap bo xi r4.0
Sap bo xi r4.0
 
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
 
J2EE pattern 5
J2EE pattern 5J2EE pattern 5
J2EE pattern 5
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015
 

Mehr von Sparkhound Inc.

Integrating the BCS with Search in SharePoint 2013
Integrating the BCS with Search in SharePoint 2013Integrating the BCS with Search in SharePoint 2013
Integrating the BCS with Search in SharePoint 2013
Sparkhound Inc.
 

Mehr von Sparkhound Inc. (20)

IT Strategy: Aligning IT and the Business
IT Strategy: Aligning IT and the BusinessIT Strategy: Aligning IT and the Business
IT Strategy: Aligning IT and the Business
 
Quality Assurance: What is it and what are the Business Benefits?
Quality Assurance: What is it and what are the Business Benefits?Quality Assurance: What is it and what are the Business Benefits?
Quality Assurance: What is it and what are the Business Benefits?
 
SQL Server Reporting Services (SSRS) 101
 SQL Server Reporting Services (SSRS) 101 SQL Server Reporting Services (SSRS) 101
SQL Server Reporting Services (SSRS) 101
 
Dashboards for Everyone with Microsoft Power BI & Excel
Dashboards for Everyone with Microsoft Power BI &  ExcelDashboards for Everyone with Microsoft Power BI &  Excel
Dashboards for Everyone with Microsoft Power BI & Excel
 
Spinning Brown Donuts: Why Storage Still Counts
Spinning Brown Donuts: Why Storage Still CountsSpinning Brown Donuts: Why Storage Still Counts
Spinning Brown Donuts: Why Storage Still Counts
 
Intro to AngularJS
Intro to AngularJS Intro to AngularJS
Intro to AngularJS
 
Leveraging SharePoint 2013 Search and CSR
Leveraging SharePoint 2013 Search and CSRLeveraging SharePoint 2013 Search and CSR
Leveraging SharePoint 2013 Search and CSR
 
Ensuring Quality Mobile Apps with Testing and Crash Reporting
Ensuring Quality Mobile Apps with Testing and Crash ReportingEnsuring Quality Mobile Apps with Testing and Crash Reporting
Ensuring Quality Mobile Apps with Testing and Crash Reporting
 
Managing Customer Expectations
Managing Customer ExpectationsManaging Customer Expectations
Managing Customer Expectations
 
Virtualize All The Things!
Virtualize All The Things!Virtualize All The Things!
Virtualize All The Things!
 
What is "Next Generation" Analytics? How does it fit with my Business Vision?
What is "Next Generation" Analytics? How does it fit with my Business Vision?What is "Next Generation" Analytics? How does it fit with my Business Vision?
What is "Next Generation" Analytics? How does it fit with my Business Vision?
 
What is the right SharePoint Cloud Strategy for My Business?
What is the right SharePoint Cloud Strategy for My Business? What is the right SharePoint Cloud Strategy for My Business?
What is the right SharePoint Cloud Strategy for My Business?
 
Identity Management for Office 365 and Microsoft Azure
Identity Management for Office 365 and Microsoft AzureIdentity Management for Office 365 and Microsoft Azure
Identity Management for Office 365 and Microsoft Azure
 
Integrating the BCS with Search in SharePoint 2013
Integrating the BCS with Search in SharePoint 2013Integrating the BCS with Search in SharePoint 2013
Integrating the BCS with Search in SharePoint 2013
 
Htf2014 managing share point projects with agile and tfs andy
Htf2014 managing share point projects with agile and tfs   andyHtf2014 managing share point projects with agile and tfs   andy
Htf2014 managing share point projects with agile and tfs andy
 
SQL Server Admin Best Practices with DMV's
SQL Server Admin Best Practices with DMV'sSQL Server Admin Best Practices with DMV's
SQL Server Admin Best Practices with DMV's
 
PowerShell Fundamentals for SharePoint
PowerShell Fundamentals for SharePointPowerShell Fundamentals for SharePoint
PowerShell Fundamentals for SharePoint
 
Introduction to the SharePoint 2013 REST API
Introduction to the SharePoint 2013 REST APIIntroduction to the SharePoint 2013 REST API
Introduction to the SharePoint 2013 REST API
 
Introduction to JSLink in 2013
Introduction to JSLink in 2013Introduction to JSLink in 2013
Introduction to JSLink in 2013
 
Cross-Platform Mobile Development with PCLs
Cross-Platform Mobile Development with PCLsCross-Platform Mobile Development with PCLs
Cross-Platform Mobile Development with PCLs
 

KĂŒrzlich hochgeladen

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

KĂŒrzlich hochgeladen (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

Optimizing Code Reusability for SharePoint using Linq to SharePoint & the MVP Design Pattern

  • 1. Optimizing Code Reusability For SharePoint Using LINQ and the MVP Design Pattern Houston Tech Fest 2014
  • 2. Introduction – Ted Wagner BS in Game Software Development SharePoint SME for over 3 Years. Conducted multiple training sessions for SharePoint 2010 and 2013. Worked for Sparkhound over a year. 2
  • 3. “Be a hero. Grow a beard.” During September, the Sparkhound Foundation is sponsoring the growth of facial hair. Why? To help raise funds for research and treatment of prostate cancer. Learn more at Septembeard.org. $680,380 raised since 2011
  • 4. 4 Thank you for being a part of the 8th Annual Houston TechFest! Please turn off all electronic devices or set them to vibrate. If you must take a phone call, please do so in the lobby so as not to disturb others. Thanks to our Diamond Sponsors:
  • 5. 5 Overview Review the MVP pattern Setting up models to promote encapsulation and testing Setting up Presenters and Views for separation of functionality to promote testing
  • 6. Outcome Reusable SharePoint solutions to be used between various applications and SharePoint projects. 6
  • 7. What is MVP? A design pattern used to separate functionality from controls to maximize the amount of code that can be tested automatically. Separation of the logic of events and the updates made to the application data. Uses an Interface to reference the view instead of the view class Many different variations 7
  • 8. Microsoft Corporation. (2014). The Model-View-Presenter (MVP) Pattern. Retrieved September 3, 2014, from Microsoft Developer Network: http://msdn.microsoft.What is MVP? MVP image (Microsoft 8 Supervising Presenter Corporation, 2014)
  • 9. Why use MVP with SharePoint? MVC is technically “not supported” Designs are usually scoped to one view – Web Parts Increase Unit Testing Encapsulation of SharePoint Methods and Assemblies 9
  • 10. Model Data Model 1. Data Model 1. Representation of columns within a List or Library 2. Business Model 1. Represented as an interface 2. Data access object used to query database 10
  • 11. View Class Containing visual controls Handles events Contains Page Events, JavaScript, CSS, JQuery and any other Front-End functionality Views do not have knowledge of the model An interface is designated to the view for updating the view state when the model is updated. 11
  • 12. Presenter The class that combines the view to the data models. Updates the view. One presenter per view Easily attach to Unit testing projects for automation. 12
  • 13. LINQ to SharePoint Utilize LINQ to easily query SharePoint Lists Provides quick way to cast objects to generics for encapsulation Quickly generate models representing SharePoint Lists using SPMetal 13
  • 14. Setting up Solution Create a Class Library Project for storing Models Create Project for SharePoint Solutions Sign key Add assembly to package Package->Advanced->Add 14
  • 15. Setting up your Models ‱ Create a separate project for you models only ‱ Setup your data models ‱ Setup Data Access Objects and business model interfaces Best Practices ‱ Keep all SharePoint references in the models ‱ Keep error handling to a minimum in the models 15
  • 16. Data Model Example 16 public class CustomerModel { public string CustId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public string City { get; set; } public string State { get; set; } public string Zip { get; set; } public bool Vendor { get; set; } }
  • 17. Generate DataContext With SPMetal ‱ SPMetal – located in the BIN folder in Hive ‱ Create Environment Variable referencing SPMetal in hive folder ‱ System Properties -> Advanced Settings -> Advanced ‱ Create Folder for storing DataContext Files 17 C:SPMetal /web:http://sparkhound.ted.local /code:CustomerDataContext.cs
  • 18. Using DataContext Model Using Preconfigured data models from SPMetal Pros Generates exact column names No need to create data models Cons Cannot keep SharePoint functionality within the model Hard to find and update models 18
  • 19. Example 19 public List<CustomerItem> GetCustomers() { using (TechfestdatacontextDataContext dc = new TechfestdatacontextDataContext(SPContext.Current.Site.Url)) { EntityList<CustomerItem> customers = dc.GetList<CustomerItem>("Customer"); var allCustomers = from c in customers select c; return allCustomers.ToList<CustomerItem>(); } }
  • 20. Using Custom Data Model Designing your own Data Model Pros Flexibility to control what data is passed Control names Encapsulate SharePoint references Cons Must create each data model manually 20
  • 21. Example 21 using(TechfestdatacontextDataContext dc = new TechfestdatacontextDataContext(SPContext.Current.Site.Url)) { EntityList<RegisterItem> register = dc.GetList<RegisterItem>("Register"); var customerDetails = from r in register where r.CustID == CustId select new RegisterModel { CustId = r.CustID, Credit = Convert.ToDouble(r.Credit), Debit = Convert.ToDouble(r.Debit), Date = Convert.ToDateTime(r.Date), RegisterId = r.Title }; return customerDetails.ToList<RegisterModel>(); }
  • 22. Setting up the presenter The presenter will reference the view through an interface Overload the constructor to pass the view All functionality is handled by the presenter Best Practices Use bool functions to catch errors. Return false for errors to keep other functionality from continuing. 22
  • 23. Example Constructor accepting view and assigning it to a variable 23 public CustomerInfoPresenter(ICustomerInfoView View) { view = View; }
  • 24. Example 24 public bool GetCustomerInfo() { try { CustomerBusinessObject model = new CustomerBusinessObject(); view.GetCustomers = model.GetCustomers(); return true; } catch (Exception ex) { view.ErrorMsg = "Error Getting Customer Info: " + ex.Message; return false; } }
  • 25. Setting up the view Views use properties to assign values and populate controls Minimum functionality in views Represent data in an interface to pass data 25 public interface ICustomerInfoView { List<CustomerItem> GetCustomers {get; set;} List<RegisterModel> CustomerData {get; set;} string ErrorMsg { get; set; } }
  • 26. Example 26 public List<CustomerItem> GetCustomers { get { return CustomerDDL.DataSource as List<CustomerItem>; } set { CustomerDDL.DataTextField = "LastName"; CustomerDDL.DataValueField = "Title"; CustomerDDL.DataSource = value; CustomerDDL.DataBind(); CustomerDDL.Items.Insert(0, "Please select a customer..."); } }
  • 27. Example – Page Load 27 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { CustomerInfoPresenter presenter = new CustomerInfoPresenter(this); presenter.GetCustomerInfo(); } }
  • 28. Pros/Cons Pros Encapsulation Easily give code to other developers Reusability with other projects and applications Ability to manipulate and bind data after query Cons Issues with cross site collection queries LINQ is not Thread Safe Slower than CAML Extra coding overhead 28
  • 29. Overcoming cross site collection queries 1. Extend DataContext Model to include a new constructor that accepts different site collections. 2. Change the Current Context to the site collection being queried. 29
  • 30. Overcoming cross site collection queries 30 var CurrentContext = HttpContext.Current; (using SPSite site = new SPSite(SiteCollectionUrl)) { (using(SPWeb web = site.OpenWeb()) { HttpRequest request = new HttpRequest(“”, SiteCollectionURL, “”); HttpContext.Current = new HttpContext(request, new HttpRespone(new StringWriter())); //Perform Linq Query } } HttpContext.Current = CurrentContext;
  • 32. Please Leave Feedback 32 If you leave session feedback and provide contact information in the survey, you will be qualified for a prize Scan the QR Code to the right or go to http://bit.ly/1p13f3n
  • 33. Contact Info Ted Wagner Ted.wagner@sparkhound.com 33
  • 34. 34 Thanks to all our Sponsors!
  • 35. Works Cited Microsoft Corporation. (2014). The Model-View-Presenter (MVP) Pattern. Retrieved September 3, 2014, from Microsoft Developer Network: http://msdn.microsoft.com/en-us/ library/ff649571.aspx 35
  • 36. 36

Hinweis der Redaktion

  1. Why are we doing this? In honor of the Sparkies and Sparkie family members who have (or continue to fight) fought prostate cancer Background: - The Sparkhound Foundation is our non-profit charitable giving organization - Prostate cancer is the second leading cause of death in American men.
  2. SLIDE PURPOSE: This is where everything starts – us defining with our customers how we can make an impact in their business regardless of the technology! SPIEL: Technology should be transformative, not a to-do list. Because, really, just about any firm can change the way your IT works. Our mission is for IT to change the way your business works. The difference is a game-changer.
  3. SLIDE PURPOSE: This is where everything starts – us defining with our customers how we can make an impact in their business regardless of the technology! SPIEL: Technology should be transformative, not a to-do list. Because, really, just about any firm can change the way your IT works. Our mission is for IT to change the way your business works. The difference is a game-changer.
  4. Error handling to a minimum to keep errors from being passed over. Example: pass error in customer first name, but do not use customer first name in persenter/view
  5. Code overhead – presenter basically replacing code behind on page