Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Writing Testable Code in SharePoint                           Tim McCarthy                Senior Technical Architect      ...
Agenda•   Introduction•   Repository Pattern•   IoC and Dependency Injection Patterns•   Model View Presenter Pattern•   U...
Introduction•   Tightly-coupled SharePoint Code•   Difficult to test Web Parts and Pages•   Need a better way to organize ...
What is the Repository Pattern?•   Isolates the domain model from data access•   Encapsulates the domain object persistenc...
DemoRepository Pattern
What is IoC?• Inversion of Control• Depend upon abstract types, not concrete  types
Benefits of Inversion of Control• Can swap out implementations later (such as  different repositories)• Allows parts of th...
What is DI?• DI = Dependency Injection• One solution to the problem instantiating  abstract types• Available DI frameworks...
Benefits of Dependency Injection• Can write more granular unit tests• Don’t need to hit the database for testing UI  logic...
DemoIoC and Dependency Injection
Model View Presenter Pattern• Gets the logic out of your UI so it can be  tested!• Forced separation of concerns• Can enab...
DemoModel View Presenter
Unit Testing Strategies• Test the presenters separately• Use mock views and mock repositories when  testing the presenters...
DemoUnit Tests
Summary• Design Patterns are the key to better  SharePoint code!• Repository, IoC and DI patterns make testing  way easier...
Get the Source Code!        Source Code Link
Contact Info             Tim McCarthytim.mccarthy@perficient.com
We want your feedback!                   Use this QR code or visit:                   http://sps.la/feedback              ...
Victory Lap- social event  "SharePoint Victory Lap" Social Event for     SPSLA will be at: 5:30pm to 8pm atDi Piazzas (520...
Upcoming SlideShare
Loading in …5
×

Writing Testable Code in SharePoint

838 views

Published on

Many developers forget about good object-oriented design techniques when developing in SharePoint, most of the time because they get overwhelmed by the framework. Unit testing often gets thrown out of the window, and most of the time the application becomes very tightly coupled to the SharePoint object model. This talk will demonstrate how to overcome these obstacles and build solid SharePoint application code that is much more testable and is easier to maintain.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Writing Testable Code in SharePoint

  1. 1. Writing Testable Code in SharePoint Tim McCarthy Senior Technical Architect Perficient
  2. 2. Agenda• Introduction• Repository Pattern• IoC and Dependency Injection Patterns• Model View Presenter Pattern• Unit Testing strategies• Summary
  3. 3. Introduction• Tightly-coupled SharePoint Code• Difficult to test Web Parts and Pages• Need a better way to organize the code• The answer: Design Patterns!
  4. 4. 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…
  5. 5. DemoRepository Pattern
  6. 6. What is IoC?• Inversion of Control• Depend upon abstract types, not concrete types
  7. 7. 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
  8. 8. What is DI?• DI = Dependency Injection• One solution to the problem instantiating abstract types• Available DI frameworks – Castle – Unity – MEF – Etc.
  9. 9. 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!
  10. 10. DemoIoC and Dependency Injection
  11. 11. 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?
  12. 12. DemoModel View Presenter
  13. 13. Unit Testing Strategies• Test the presenters separately• Use mock views and mock repositories when testing the presenters• Test the concrete repositories separately
  14. 14. DemoUnit Tests
  15. 15. 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
  16. 16. Get the Source Code! Source Code Link
  17. 17. Contact Info Tim McCarthytim.mccarthy@perficient.com
  18. 18. We want your feedback! Use this QR code or visit: http://sps.la/feedback Silver Sponsors:
  19. 19. Victory Lap- social event "SharePoint Victory Lap" Social Event for SPSLA will be at: 5:30pm to 8pm atDi Piazzas (5205 E. Pacific Coast Hwy, 90804)

×