Transcript
Page 1: Writing Testable Code in SharePoint

Writing Testable Code in SharePoint

Tim McCarthySenior Technical Architect

Perficient

Page 2: Writing Testable Code in SharePoint

Agenda

• Introduction• Repository Pattern• IoC and Dependency Injection Patterns• Model View Presenter Pattern• Unit Testing strategies• Summary

Page 3: Writing Testable Code in SharePoint

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: Writing Testable Code in SharePoint

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: Writing Testable Code in SharePoint

DemoRepository Pattern

Page 6: Writing Testable Code in SharePoint

What is IoC?

• Inversion of Control• Depend upon abstract types, not concrete

types

Page 7: Writing Testable Code in SharePoint

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: Writing Testable Code in SharePoint

What is DI?

• DI = Dependency Injection• One solution to the problem instantiating

abstract types• Available DI frameworks– Castle– Unity– MEF– Etc.

Page 9: Writing Testable Code in SharePoint

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: Writing Testable Code in SharePoint

DemoIoC and Dependency Injection

Page 11: Writing Testable Code in SharePoint

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: Writing Testable Code in SharePoint

DemoModel View Presenter

Page 13: Writing Testable Code in SharePoint

Unit Testing Strategies

• Test the presenters separately• Use mock views and mock repositories when

testing the presenters• Test the concrete repositories separately

Page 14: Writing Testable Code in SharePoint

DemoUnit Tests

Page 15: Writing Testable Code in SharePoint

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: Writing Testable Code in SharePoint

Get the Source Code!

Source Code Link

Page 17: Writing Testable Code in SharePoint

Contact Info

Tim [email protected]

Page 18: Writing Testable Code in SharePoint

We want your feedback!Use this QR code or visit:http://sps.la/feedback

Silver Sponsors:

Page 19: Writing Testable Code in SharePoint

Victory Lap- social event "SharePoint Victory Lap" Social Event for

SPSLA will be at: 5:30pm to 8pm at Di Piazzas (5205 E. Pacific Coast Hwy, 90804)


Top Related