SharePoint Saturday San Diego - Writing Testable Code in SharePoint

Uploaded on


More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide
  • Many developers forget 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.The vehicle for this goodness is design patterns!
  • Using the repository pattern will allow you to develop outside of the SharePoint environment "up until the last minute"
  • Depend on abstract types (interfaces) instead of concrete classes!Don’t need a big bulky 64-bit Windows 2008 Server virtual machine until much later!
  • MVP is good for web forms projects, which is what SharePoint is based uponModel: Defines the data to be displayed or acted upon in the UIView: UI that displays data from the Model and routes user commands to the Presenter to act upon the dataPresenter: Acts upon the model and the ViewWith MVP, the idea is to strip as much logic and code out of the UI and make it do simple things, like data binding and and acting as a facade to the various UI element properties, i.e. expose TextBox.Text property as FirstName.This pattern really pays for itself when you find bugs in the UI logic…just write a failing unit test and fix the code so it passes the test!Very difficult to use MVC in SharePoint, not worth the effort
  • Doing TDD by the letter takes a lot of discipline. Once you start doing it, it can become a habit. But even if you don't write your tests first, if you use DI + MVP + tests later, you still will benefit from the modularity and freedom to refactor your code.Writing tests can take up time initially, but the payoff happens in spades once you have to identify and fix bugs, or refactor your code with confidence of not breaking anything else.Mock up your views and repositories so you only test what is necessary...that way your tests are really focused. You can (and should) do integration tests later where you use the real repositories. Once you have a pretty good suite of tests developed, you can freely make changes to your code and see if your tests still pass. Refactor, compile, re-test.Rinse, lather, repeat!


  • 1. June 30, 2012San Diego Convention CenterWRITING TESTABLE CODE In SharePoint #SPSSAN
  • 2. AgendaIntroductionRepository PatternIoC and Dependency Injection PatternsModel View Presenter PatternUnit Testing strategiesSummary #SPSSAN
  • 3. IntroductionTightly-coupled SharePoint CodeDifficult to test Web Parts and PagesNeed a better way to organize the codeThe answer: Design Patterns! #SPSSAN
  • 4. What is the Repository Pattern?Isolates the domain model from data accessEncapsulates the domain object persistencePersistence Ignorance is bliss!Works well when used with interfaces… #SPSSAN
  • 5. DemoRepository Pattern #SPSSAN
  • 6. What is IoC?Inversion of ControlDepend upon abstract types, not concrete types #SPSSAN
  • 7. Benefits of Inversion of ControlCan swap out implementations later (such as differentrepositories)Allows parts of the application to be built independentlywith no complicated dependenciesCan work in ASP.NET and switch over to SharePoint later #SPSSAN
  • 8. What is DI?DI = Dependency InjectionOne solution to the problem instantiating abstract typesAvailable DI frameworks Castle Unity MEF Etc. #SPSSAN
  • 9. Benefits of Dependency InjectionCan write more granular unit testsDon’t need to hit the database for testing UI logicAllows parts of an application to be easily swapped outwithout re-compilingUsing a DI framework makes it almost seamless! #SPSSAN
  • 10. DemoIoC and Dependency Injection #SPSSAN
  • 11. Model View Presenter PatternGets the logic out of your UI so it can be tested!Forced separation of concernsCan enable UI logic to be sharedSide Point: Why not use MVC in SharePoint? #SPSSAN
  • 12. DemoModel View Presenter #SPSSAN
  • 13. Unit Testing StrategiesTest the presenters separatelyUse mock views and mock repositories when testing thepresentersTest the concrete repositories separately #SPSSAN
  • 14. Demo Unit Tests#SPSSAN
  • 15. SummaryDesign Patterns are the key to better SharePoint code!Repository, IoC and DI patterns make testing way easierMVP pattern gets the logic out of your UI code #SPSSAN
  • 16. June 30, 2012San Diego Convention Center GET THE SOURCE CODE! Source Code Link #SPSSAN
  • 17. June 30, 2012San Diego Convention Center CONTACT INFO Tim McCarthy #SPSSAN
  • 18. The After-Party: SharePint Karl Strauss Brewing Company 1157 Columbia Street San Diego, CA 92101 Phone: 619-234-2739Immediately 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 #SPSSAN
  • 19. June 30, 2012 San Diego Convention Center THANK OUR SPONSORSPlease be sure to fill out your session evaluation! #SPSSAN