SharePoint Saturday San Diego - Writing Testable Code in SharePoint

19
#SPSSAN June 30, 2012 San Diego Convention Center WRITING TESTABLE CODE In SharePoint

Transcript of SharePoint Saturday San Diego - Writing Testable Code in SharePoint

Page 1: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

June 30, 2012

San Diego Convention Center

WRITING TESTABLE

CODE

In SharePoint

Page 2: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

Agenda

Introduction

Repository Pattern

IoC and Dependency Injection Patterns

Model View Presenter Pattern

Unit Testing strategies

Summary

Page 3: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

Introduction

Tightly-coupled SharePoint Code

Difficult to test Web Parts and Pages

Need a better way to organize the code

The answer: Design Patterns!

Page 4: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

What is the Repository Pattern?

Isolates the domain model from data access

Encapsulates the domain object persistence

Persistence Ignorance is bliss!

Works well when used with interfaces…

Page 5: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

Demo

Repository Pattern

Page 6: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

What is IoC?Inversion of Control

Depend upon abstract types, not concrete types

Page 7: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

Benefits of Inversion of Control

Can swap out implementations later (such as different

repositories)

Allows parts of the application to be built independently

with no complicated dependencies

Can work in ASP.NET and switch over to SharePoint later

Page 8: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

What is DI?

DI = Dependency Injection

One solution to the problem instantiating abstract types

Available DI frameworks

Castle

Unity

MEF

Etc.

Page 9: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

Benefits of Dependency Injection

Can write more granular unit tests

Don’t need to hit the database for testing UI logic

Allows parts of an application to be easily swapped out

without re-compiling

Using a DI framework makes it almost seamless!

Page 10: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

Demo

IoC and Dependency Injection

Page 11: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

Model View Presenter Pattern

Gets the logic out of your UI so it can be tested!

Forced separation of concerns

Can enable UI logic to be shared

Side Point: Why not use MVC in SharePoint?

Page 12: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

Demo

Model View Presenter

Page 13: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

Unit Testing Strategies

Test the presenters separately

Use mock views and mock repositories when testing the

presenters

Test the concrete repositories separately

Page 14: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

Demo

Unit Tests

Page 15: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

Summary

Design Patterns are the key to better SharePoint code!

Repository, IoC and DI patterns make testing way easier

MVP pattern gets the logic out of your UI code

Page 16: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

June 30, 2012

San Diego Convention Center

GET THE SOURCE

CODE!

Source Code Link

Page 17: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

June 30, 2012

San Diego Convention Center

CONTACT INFO

Tim McCarthy

[email protected]

Page 18: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

The After-Party: SharePint

Karl Strauss Brewing Company

1157 Columbia Street

San Diego, CA 92101

Phone: 619-234-2739

Immediately following event closing & prize drawings (@6:30 pm)

Directions (.9 miles):

1. Head northeast on 1st Ave

2. Turn left onto W B St

3. Turn left onto Columbia St

Karl Strauss will be on the left

Page 19: SharePoint Saturday San Diego - Writing Testable Code in SharePoint

#SPSSAN

June 30, 2012

San Diego Convention Center

THANK OUR SPONSORS

Please be sure to fill out your session evaluation!